Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting base branch SHA on pull request in Github Action Workflow

In GitHub action on pull request, I need to run some code in the context of the "current master", and later re-run the same code in the context of the PR branch.

I can check out compare a pull request to the base it is being PR-ed against. How would I find the SHA of the base branch (e.g. current master if PR is against the master)?

jobs:
  job_on_base:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: "${{ github.base_ref }}"
      - run: |
          # Seems like I can get it here with $(git log -1 --format="%H")
          echo "My current SHA is ... ?"

  job_on_pr:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          ref: "${{ github.ref }}"
      - run: |
          echo "My current SHA is $GITHUB_SHA"
          echo "The BASE SHA is ?"
like image 696
Yuri Astrakhan Avatar asked Jan 23 '26 07:01

Yuri Astrakhan


2 Answers

If the job runs on a pull_request event the base sha is available as ${{ github.event.pull_request.base.sha }}

like image 143
Jacob Rask Avatar answered Jan 24 '26 20:01

Jacob Rask


What worked for me was: github.event.workflow_run.head_sha.

The top-voted answer suggesting github.event.pull_request.head.sha didn't work for me.

I found this by examining all the possible data in the github.context object using the method suggested here - https://stackoverflow.com/a/70107953

like image 33
Ben Perlmutter Avatar answered Jan 24 '26 20:01

Ben Perlmutter



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!