trilom / file-changes-action

This action can be added, and you will get outputs of all of the files that have changed in your repository for you to use.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

file-changes-action

codecov code style: prettier Integration Tests

Like my work? Hire me!

Please reach out if you need something built!

This action will take the information from the Push/Pull Request and output some variables and write files that will let you know what was changed, removed, or added.

Inputs

githubRepo

Optional - string - the github repository you want to compare changes from, defaults to the github.repository.

githubToken

Optional - string - specific github token, github.token is used by default (Github Action Runner)

output

Optional - string - type of output for output variables, default is json. Use ',' for comma separated values, or ' ' for space delimited values. You can also create your own delimiter for example ' |FILE:' will output 'file1.yml |FILE:file2.yml |FILE:file3.yml'.

fileOutput

Optional - string - type of output for file output, default is json. Use ',' for comma separated values, or ' ' for space delimited values. You can also create your own delimiter for example \ |FILE: will output:

file1.yml |FILE:file2.yml |FILE:file3.yml

If you select json then the file format will be .json, if you select ',' then the file format will be .csv, anything else will output the files as .txt

pushBefore

Optional - string - pass in a specific sha to compare to as a before, required if using pushAfter. (push payload after github.payload.before)

pushAfter

Optional - string - pass in a specific sha to compare to as an after, required if using pushBefore. (push payload after github.payload.after)

prNumber

Optional - string - pass in a specific PR number to get file changes from.

Outputs

files

steps.file_changes.outputs.files - string - The names all new, updated, and removed files. The output is dependant on the output input, default is a json string.

files_added

steps.file_changes.outputs.files_added - string - The names of the newly created files. The output is dependant on the output input, default is a json string.

files_modified

steps.file_changes.outputs.files_modified - string - The names of the updated files. The output is dependant on the output input, default is a json string.

files_removed

steps.file_changes.outputs.files_removed - string - The names of the removed files. The output is dependant on the output input, default is a json string.

Example usage

# bare minimal
name: changes
on: push
jobs:
  changes:
    runs-on: ubuntu-latest
    steps:
      - id: file_changes
        uses: trilom/file-changes-action@v1.2.3

### full
name: changes
on: [push, pull_request] # push or pull, or any event with custom pr number or before/after commit sha
jobs:
  changes:
    runs-on: ubuntu-latest
    steps:
      - id: file_changes
        uses: trilom/file-changes-action@v1.2.3
        with:
          # optional target repo
          githubRepo: trilom/file-changes-action
          # optional token
          githubToken: ${{ secrets.BOT_TOKEN }}
          # optional output format
          output: 'json'
          # optional fileoutput format
          fileOutput: 'csv'
          # optional push before SHA (need both before and after)
          pushBefore: 79eeec74aebc3deb0a2f6234c5ac13142e9224e5
          # optional push after SHA (need both before and after)
          pushAfter: 1c5a2bfde79e2c9cffb75b9a455391350fe69a40
          # optional PR number to compare
          prNumber: 36

How to Use

In order to make those decisions we need to know what files have changed and that is where this action comes in. In the example below we are checking out our repository code, and then running the trilom/file-changes-action@v1 action. The only thing you need to provide is a GITHUB_TOKEN so that Octokit can make it's API calls.

If a PR is made then it will look at all of the files included in the PR. If a push is made then it will compare commits from the SHA github.payload.before to the SHA github.payload.after of the push.

After gathering this information it will output the files in 2 ways.

  • As an output variable, you can use this variable by using steps.file_changes_outputs_files, steps.file_changes.outputs.files_modified, steps.file_changes.outputs.files_added, steps.file_changes.outputs.files_removed.

  • As a file on the container stored at $HOME/files.json, $HOME/files_modified.json, $HOME/files_added.json, $HOME/files_removed.json.

  • NOTE: If you set a custom delimiter in output or fileOutput inputs then you will receive different files. For example a delimiter of ',' will output at $HOME/files.csv instead of $HOME/files.json. Likewise, anything other than 'json' or ',' delmiters will output $HOME/files.txt files instead of $HOME/files.json by default.

Use Cases

I have a process where I have AWS Cloudformation templates stored in one directory that might be named PRODUCT-ROLE, and mappings for these templates that span the PRODUCT. For example mappings/wordpress.mappings.yml, templates/wordpress-database.yml, templates/wordpress-webserver.yml, and some of the templates might use different Lambda functions defined in for example functions/wordpress-webserver/.

In the example below we have a workflow that on push to the develop branch we can perform some actions based on the files. In my use case I look for changes on the develop branch of this repository for every push that happens. When a push happens and a change is made to any of the paths below the workflow will trigger. With this action you are able to know exactly which files changed so that you can make decisions later in your CI/CD.

In this case, if a templates/*.yml file is changed, then we want to update the Cloudformation stack. We can also write specifics for related templates. For example, if templates/wordpress-database.yml changes then we want to deploy templates/wordpress-webserver.yml as well after.

Another case is if the mappings/wordpress.mappings.yml changes, we want to deploy all template/wordpress-*.yml files.

More examples

name: push-develop
on: [push]
jobs:
  changes:
    runs-on: ubuntu-latest
    steps:
      - id: file_changes
        uses: trilom/file-changes-action@v1.2.3
      - name: test
        run: |
          cat $HOME/files.json
          cat $HOME/files_modified.json
          cat $HOME/files_added.json
          cat $HOME/files_removed.json
          echo '${{ steps.file_changes.outputs.files}}'
          echo '${{ steps.file_changes.outputs.files_modified}}'
          echo '${{ steps.file_changes.outputs.files_added}}'
          echo '${{ steps.file_changes.outputs.files_removed}}'

You can set the output and fileOutput to ',' for csv output.

name: push-develop
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - id: file_changes
        uses: trilom/file-changes-action@v1.2.3
        with:
          output: ','
          fileOutput: ','
      - name: test
        run: |
          cat $HOME/files.csv

You can set the output and fileOutput to ' ' for txt output. We also used a specific token, and got info for the PR that this push came from.

name: push-develop
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@0.6.0
        id: pr
        with:
          github-token: ${{env.BOT_USER_TOKEN}}
          result-encoding: string
          script: |
            const result = await github.repos.listPullRequestsAssociatedWithCommit({
              owner: context.payload.repository.owner.name,
              repo: context.payload.repository.name,
              commit_sha: context.payload.head_commit.id
            })
            return result.data[0].number;
      - id: file_changes
        uses: trilom/file-changes-action@v1.2.3
        with:
          githubToken: ${{ env.BOT_USER_TOKEN }}
          prNumber: ${{ steps.pr.outputs.results }}
          output: ' '
          fileOutput: ' '
      - name: test
        run: |
          cat $HOME/files.txt

About

This action can be added, and you will get outputs of all of the files that have changed in your repository for you to use.

License:MIT License


Languages

Language:TypeScript 93.1%Language:Shell 6.1%Language:JavaScript 0.8%