What is the difference between these two ways to get the triggering commit?

I need to run a workflow when a Pull Request closes, and it depends on the SHA of the most recent commit before the pull request was closed. However, when looking into ways to do that, I’ve found that $GITHUB_SHA did not, in fact, have that - instead, I could only find it by grabbing it from the github context, in the path github.event.pull_request.head.sha.

These are my results from running these two commands in my action:

      - name: sha from context
        run: echo ${{ github.event.pull_request.head.sha }}

      - name: sha from env var
        run: echo $GITHUB_SHA

As you can see, what I get from $GITHUB_SHA does not match the hash displayed under the commit message. Why are these two different?

The GITHUB_SHA of pull_request event is the Last merge commit on the GITHUB_REF branch . The GITHUB_REF is PR merge branch refs/pull/:prNumber/merge .

Pull request head points to the source branch of the pull request.
github.event.pull_request.head.sha is the right way to get SHA of the most recent commit before the pull request was closed.

2 Likes