40

When an action is set on pull_request in Github Actions, how to get the target branch? The use case is to retrieve the PR- (and hopefully, branch)-specific commits.

Lucas
  • 1,171
  • 9
  • 21
Mouloud85
  • 3,826
  • 5
  • 22
  • 42

3 Answers3

43
  1. When you need the data in expressions (Source):
Property name Type Description
github.base_ref string The base_ref or target branch of the pull request in a workflow run. This property is only available when the event that triggers a workflow run is a pull_request.
github.head_ref string The head_ref or source branch of the pull request in a workflow run. This property is only available when the event that triggers a workflow run is a pull_request.

An example (modified from the documentation):

steps:
  - uses: actions/hello-world-javascript-action@v1.1
    if: ${{ github.base_ref == 'main' }}

  1. When you need the data as environment variables (Source):
Environment variable Description
GITHUB_HEAD_REF Only set for pull request events. The name of the head branch.
GITHUB_BASE_REF Only set for pull request events. The name of the base branch.

An example (modified from the documentation):

steps:
  - name: Hello world
    run: echo Hello world from $GITHUB_BASE_REF!
Nadjib Mami
  • 5,736
  • 9
  • 37
  • 49
Bartleby
  • 1,144
  • 1
  • 13
  • 14
28

You can access the target branch with ${{ github.event.pull_request.base.ref }}.

To know the full list of properties of the github.event object, try to run more $GITHUB_EVENT_PATH.

Lucas
  • 1,171
  • 9
  • 21
1

You can see all GitHub actions pull request event properties here.

Piyush Sonigra
  • 1,423
  • 11
  • 10