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

3 Likes

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

Thanks for the hint.
Below the revised version for Windows.

      - name: Check for Changes
        id: git_diff
        run: |
          if ( $env:github_base_ref ) {
            # Pull Request
            git fetch origin "${{ github.base_ref }}" --depth=1
            $diff_result = (git diff --name-only "origin/${{ github.base_ref }}" ${{ github.sha }} )
            echo "Diff between origin/${{ github.base_ref }} and ${{ github.sha }}"
          }else {
            # Push
            git fetch origin ${{ github.event.before }} --depth=1
            $diff_result = (git diff --name-only ${{ github.event.before }} ${{ github.sha }} )
            echo "Diff between ${{ github.event.before }} and ${{ github.sha }}"
          }
          # echo $diff_result
          $search_result = [bool] ($diff_result | Select-String -Pattern '(CHANGELOG.md)|(.ps1)' -Quiet)
          # echo $search_result
          echo "::set-output name=diff::$search_result"
1 Like