How to extract branch name in the PR workflow?

Viewed 599

Here is my sample GitHub workflow:

name: APP Build
on:
  pull_request:
    types: [opened, synchronize, reopened]
  push:
    branches:
      - feature/test

jobs:
  check-environment:
    runs-on: ubuntu-latest
    steps:
      - name: Extract branch name
        shell: bash
        run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/}| sed 's/\//-/g')"
        id: extract_branch
  test:
    runs-on: ubuntu-latest
    needs: check-environment
    steps:
      - name: echo
        run: echo ${{ steps.extract_branch.outputs.branch }}

I have these 3 problem:

  • Once PR is created and I push some changes, this triggered 2 builds. One for the pull_request: and one for push:. How to make sure when both is on: condition is met then trigger only 1 build?

  • When build is trigger by the push on branch. I get the output of echo ${{ steps.extract_branch.outputs.branch }}: feature-test. Which is expected but when build is trigger by pull request same echo ${{ steps.extract_branch.outputs.branch }} becomes refs-pull-2-merge. How to still get branch name for the pull request?

  • How to make job name test run only for Pull request build. For Push only check-environment should be run and for pull request both the job should run. I know there is if condition but how to make it run for pull request(new PR and update on PR)

1 Answers

Once PR is created and I push some changes, this triggered 2 builds. One for the pull_request: and one for push:. How to make sure when both is on: condition is met then trigger only 1 build?

Your workflow is triggered multiple times because on conditions work as OR and not as AND. Therefore it will trigger for each event condition that is configured.

Example: You cannot create a pull request without pushing your commits into the source branch, so both events conditions will be met in that case.

To trigger the workflow only for a specific event, you would need to add types to the configured events (here are the available ones for PR).

You can also keep the same triggers, but add conditions for each job (I give some examples in the 3rd point below) according to your context.


When build is trigger by the push on branch. I get the output of echo ${{ steps.extract_branch.outputs.branch }}: feature-test. Which is expected but when build is trigger by pull request same echo ${{ steps.extract_branch.outputs.branch }} becomes refs-pull-2-merge. How to still get branch name for the pull request?

The refs/pull/<number>/merge is a reference created by GitHub to keep track of what would happen if a pull request was merged. It references the merge commit between refs/pull/<number>/head and the destination branch (e.g. master).

In your case here, one way to get the base branch on a PR event could be by doing something like this:

- name: Step that prints name of pull request's base branch
  if: github.event_name == 'pull_request'
  run: echo "Pull request's base branch is: ${BASE_BRANCH}"
  env:
    BASE_BRANCH: ${{ github.base_ref }}

For more details, see the documentation on the github context


How to make job name test run only for Pull request build. For Push only check-environment should be run and for pull request both the job should run. I know there is if condition but how to make it run for pull request(new PR and update on PR)

You could use something similar to condition used on the answer above, at a step level or at the job level, according to what you want to do. Example:

If event is PR

   if: github.event_name == 'pull_request'

If event is PUSH

   if: github.event_name == 'push'

If event is PUSH AND PR (use || for OR)

   if: github.event_name == 'pull_request' && github.event_name == ‘push’ 
Related