Skip to content

Post diff between two directories to a comment in GitHub Actions

License

Notifications You must be signed in to change notification settings

int128/diff-action

Repository files navigation

diff-action ts

This is an action to compute a diff between head and base, and post it to a comment.

Getting Started

To post a comment of the diff between old-directory and new-directory,

- uses: int128/diff-action@v1
  with:
    base: old-directory
    head: new-directory

If no difference, it post a comment of "No diff" by default.

Show diff of generated manifests

If you use kustomize build in your CI/CD pipeline, it would be useful if you can see the diff on a pull request.

To build manifests with int128/kustomize-action and show diff of it:

jobs:
  diff:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      pull-requests: write # required to post a comment to a pull request
    steps:
      - uses: actions/checkout@v4
      - uses: actions/checkout@v4
        with:
          ref: main
          path: main
      - uses: int128/kustomize-action@v1
        id: kustomize-head
        with:
          kustomization: config/default/kustomization.yaml
          write-individual-files: true
      - uses: int128/kustomize-action@v1
        id: kustomize-base
        with:
          base-directory: main
          kustomization: config/default/kustomization.yaml
          write-individual-files: true
      - uses: int128/diff-action@v1
        with:
          base: ${{ steps.kustomize-base.outputs.directory }}
          head: ${{ steps.kustomize-head.outputs.directory }}

Here is an example.

image

Label to indicate the change

To add label(s) if there is difference or remove it if not:

- uses: int128/diff-action@v1
  with:
    base: ${{ steps.kustomize-base.outputs.directory }}
    head: ${{ steps.kustomize-head.outputs.directory }}
    label: manifest-changed

No diff comment

To change the comment of when no difference,

- uses: int128/diff-action@v1
  with:
    comment-body-no-diff: No diff of kustomize build

To suppress any comment when no difference,

- uses: int128/diff-action@v1
  with:
    comment-body-no-diff: ''

Comment strategy

This action supports the following strategies:

  • create: Create a new comment.
  • replace: Replace the body of existing comment if exists. (default)
  • append: Append the diff to the existing comment if exists.
  • recreate: Delete the existing comment and create a new one.

You can change the strategy by update-if-exists.

This action identifies the existing comment by both workflow name and job name. You can set a custom key to update-if-exists-key.

Specification

This action posts a comment on pull_request or pull_request_target event only.

Inputs

Name Default Description
base (required) Path(s) of base (multiline)
head (required) Path(s) of head (multiline)
label - Label(s) to add or remove to indicate the diff (multiline)
comment-body-no-diff No diff Comment body when no difference
comment-header - Header of a comment to post
comment-footer - Footer of a comment to post
update-if-exists (optional) Either create, replace, append or recreate
update-if-exists-key ${{ github.workflow }}/${{ github.job }} Key for update-if-exists
token github.token GitHub token to post a comment

Outputs

Name Description
different If there is any difference, true. Otherwise, false