Last active
August 7, 2023 23:42
-
-
Save alexandrebrg/f19c526df9c0331a3101b8a27fde9dce to your computer and use it in GitHub Desktop.
Github Actions - Get list of files in diff between a pull request branch and target branch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Github Workflow | |
| name: 'Read diff' | |
| on: | |
| pull_request_target: | |
| branches: | |
| - main | |
| paths: | |
| - 'some_folder/**/*.yaml' | |
| # For security purposes, it preferred to lower permissions to the minimal, always, and then add job by job | |
| permissions: {} | |
| jobs: | |
| read-diff: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| pull-requests: read | |
| steps: | |
| - uses: actions/checkout@v3 | |
| name: Checkout Repository | |
| with: | |
| ref: ${{ github.event.pull_request.head.ref }} | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Execute read-diff | |
| env: | |
| # Needed for Github CLI to work | |
| GH_TOKEN: ${{ github.token }} | |
| TEMP_FILE: "${{runner.temp}}/pr-${{github.event.number}}.diff" | |
| run: | | |
| export PR_ID=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH") | |
| # Gives a GIT Diff file | |
| # check: https://git-scm.com/docs/git-diff#_combined_diff_format | |
| gh pr diff ${PR_ID} > $TEMP_FILE | |
| # We filter only names of files | |
| files_changed="$(sed -nr 's/[\-\+]{3} [ab]\/(.*)/\1/p' $TEMP_FILE | sort | uniq)" | |
| # If you want to get all files changed, you can use variable $files_changed | |
| # but if you want to get upper directories, use customize and use $some_specific_dir_changed | |
| # Adding || true to avoid "Process exited with code 1" errors | |
| some_specific_dir_changed="$(echo "$files_changed" | xargs dirname | grep -o "templates/[^/]*" | sort | uniq || true)" | |
| for dir in ${some_specific_dir_changed}; do | |
| echo "Detected $dir changed" | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment