Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github Action pull request doesn't provide last commit by github_sha

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.

like image 475
Oliver Robie Avatar asked Dec 24 '19 15:12

Oliver Robie


1 Answers

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.

like image 140
t3rmian Avatar answered Sep 22 '22 18:09

t3rmian