Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get commit SHA in Github actions

In a Github action you can get the commit SHA using ${GITHUB_SHA}, which is a default env variable.. However, this commit SHA seems to be a merge commit!? which does not equal the commit SHA displayed on PR's Github UI. Any thoughts on how I can get the SHA that is displayed in PRs (on Github UI)?

like image 974
Hamed Avatar asked Sep 12 '25 16:09

Hamed


2 Answers

To understand what happens

Reference: Github community post with the weide-zhou (Github Partner) answer.

When you can create a pull request, github will execute workflow based on a fake merge branch: refs/pull/:prNumber/merge, the merge_commit_sha doesn’t exist on base or head branch, but points to that surrogate merge commit, and there is a mergeable key to show the status of the test commit.

Therefore, here, the github.sha stands for the actual merge commit.

Github Variables

Tip: you can print the GitHub variables using the following step:

- name: Show GitHub context
  env:
    GITHUB_CONTEXT: ${{ toJson(github) }}
  run: echo "$GITHUB_CONTEXT"

It seems that what you want here is the ${{ github.event.pull_request.head.sha }} value.

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.

Note that if the pull_request has been opened for a fork repo, the github.event.pull_request variable will be empty (don't know if it's a bug or something they are working on).

like image 199
GuiFalourd Avatar answered Sep 15 '25 06:09

GuiFalourd


It's not a direct answer to the question, but google suggested me this question when I was looking for a universal solution for getting SHA, whether it's push or pull_request, so my answer could be helpful for someone.

- name: Setup Environment (PR)  
  if: ${{ github.event_name == 'pull_request' }}  
  shell: bash  
  run: |  
    echo "LAST_COMMIT_SHA=${{ github.event.pull_request.head.sha }}" >> ${GITHUB_ENV}  
- name: Setup Environment (Push)  
  if: ${{ github.event_name == 'push' }}  
  shell: bash  
  run: |  
    echo "LAST_COMMIT_SHA=${GITHUB_SHA}" >> ${GITHUB_ENV}

So now we can use the LAST_COMMIT_SHA from the ENV to get the last commit SHA. If we need it in multiple workflows, we can put these steps to a custom action

# .github/actions/prepare/action.yml
name: 'Preparation'
description: ''
runs:
  using: "composite"
  steps:
    - name: Setup Environment (PR)  
      if: ${{ github.event_name == 'pull_request' }}  
      shell: bash  
      run: |  
        echo "LAST_COMMIT_SHA=${{ github.event.pull_request.head.sha }}" >> ${GITHUB_ENV}  
    - name: Setup Environment (Push)  
      if: ${{ github.event_name == 'push' }}  
      shell: bash  
      run: |  
        echo "LAST_COMMIT_SHA=${GITHUB_SHA}" >> ${GITHUB_ENV}

and call it one of the first steps in each workflow

# .github/workflows/ci.yml
# ...
    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
      - name: Prepare
        uses: ./.github/actions/prepare
# ...
      - name: Set build tag
        shell: bash
        run: |
          echo "BUILD_TAG=${LAST_COMMIT_SHA:0:7}" >> $GITHUB_ENV
like image 35
MrModest Avatar answered Sep 15 '25 04:09

MrModest