I have a github action workflow which is triggered on pull requests. I want to find the files that have been changed in each commit so I'm trying to run git diff-tree --no-commit-id --name-only -r ${{ github.sha }}
However it doesn't return the hash commit of the latest commit in that pull request. I get returned back another hash commit (which I can't find its commit anywhere in my repo).
Anyone know how to fix this or another way I could find all the files that changed in a commit in a pull request?
Edit -- yml file
name: test
on: [pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- checkout my repo
- install python
- run a python script
run: |
CHANGED_FILES=$(git diff-tree --no-commit-id --name-only -r ${{ github.sha }})
python3 .github/scripts/test.py $CHANGED_FILES
My hash commit generated by github.sha (here ^) doens't seem to match the actual hash of my commit.
I've bumped into a similar problem with a different trigger. Turns out, you can print the GitHub variables using the following step:
- name: Dump GitHub context
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
run: echo "$GITHUB_CONTEXT"
In the case of pull_request
the hash of the latest commit can be found in the ${{ github.event.pull_request.head.sha }}
variable, whereas ${{ github.sha }}
refers to the PR merge commit.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With