How to tell which branch a PR was merged on?

My repo has multiple branches that all accept pull requests. I want to be able to see which branch got changes merged to it after a PR is merged and then do some stuff accordingly. Is there a way in my GH Actions yaml file to see which branch got changes merged to it?

I’m thinking something like this but unsure:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    # Fetch all branches
    - uses: actions/checkout@v2
    - run: |
       git fetch --no-tags --prune --depth=1 origin +refs/heads/*:refs/remotes/origin/*
.
.
<do some stuff>
.
.
- name: Publish commits to gh-pages
if: github.ref == 'refs/remotes/origin/development/v2.2' && github.event.pull_request.merged
run: do something

I’m not sure if I can use github.ref for this or if I can string together if expressions. If not, should I be nesting them?

Thanks in advance!

There are several properties of github context you need to know:

  • Using the property github.** head_ref can get the source branch** of PR in a workflow run.
  • Using the property github.** base_ref can get the target branch** of PR in a workflow run.
  • Using the property github.** ref can get the PR merge branch**(refs/pull/:prNumber/merge) that triggered the workflow run.
  • The properties github.** head_ref and github.base_ref are only available when the workflow run is triggered by a pull_request event**.

So, if you want to see the source branch where the commits merged from, call github.head_ref in the workflow. And if you want to see the target branch where the commits merged into, call github.base_ref in the workflow.

More details you can reference here: https://help.github.com/en/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context

1 Like

This was helpful, thank you.