How to get the target branch name using pull_request_review event on github actions

I have multiple jobs in github actions. I want to run jobs based on specific branches only on pull_request_review event. I tried using github.ref, github.head_ref, github.base_ref but none of them worked as they only work on pull_request or push event. As it is described below:

So my question is how to get target branch name?

I created PR from branch “latest” to “master”

I tried following, but does not work for me. It skips the first job Job1

name: Github actions
on: 
    pull_request_review:
      types: [submitted]
jobs:
    Job1:
        name: Job1
        runs-on: ubuntu-latest
        if: github.ref == 'refs/heads/master'
        steps:
            -   name: trigger
                run: echo "Hii"
    Job2:
        name: Job2
        runs-on: ubuntu-latest
        if: github.ref != 'refs/heads/master'
        steps:
            -   name: trigger
                run: echo "Hello"

Then I tried different approach and that also doesn’t work:

job1.yaml

name: Github actions
on: 
    pull_request_review:
      types: [submitted]
    branches:
       - 'master'
jobs:
    Job1:
        name: Job1
        runs-on: ubuntu-latest
        steps:
            -   name: trigger
                run: echo "Hii"

job2.yaml

name: Github actions
on: 
    pull_request_review:
      types: [submitted]
    branches:
       - '*'
       - '!master'
jobs:
    Job2:
        name: Job2
        runs-on: ubuntu-latest
        steps:
            -   name: trigger
                run: echo "Hii"

So after trying these steps, none of them worked. Now I am looking for different approach of how to get target branch name and then use conditions based on branches and then run the jobs. If I can use the same approach then how to achieve it? If anyone has suggestion how to achieve this using same or different approach then please answer. Thanks in advance!

Hi @so-amuk,

For pull_request_review event, please use ${{ github.event.pull_request.base.ref }} to get the target branch name.

The value comes from Github Context, for different event, the value name and location could be different. Please use below code to export the context:

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

Follow the structure of the context, you can find the target branch name.
So your code can be fixed as below:

name: Github actions
on: 
    pull_request_review:
      types: [submitted]
jobs:
  Job1:
    name: Job1
    runs-on: ubuntu-latest
    if: github.event.pull_request.base.ref == 'master'   # check the target branch if it's master
    steps:
        - name: trigger
          run: echo "Hii"
  Job2:
    name: Job2
    runs-on: ubuntu-latest
    if: github.event.pull_request.base.ref != 'master' # check the target branch if it's master
    steps:
        - name: trigger
          run: echo "Hello"

Please check my workflow for your reference.

Thanks

2 Likes

@weide-zhou Thanks for your valuable answer. It worked for me.