Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the target branch of the GitHub pull request from an Actions?

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.

like image 779
Mouloud88 Avatar asked Jun 11 '20 19:06

Mouloud88


People also ask

How do I change a pull request target branch?

Go to the pull request and click the ellipsis button and select Edit. From here you can change the target branch for the pr.

What is GitHub Base_ref?

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 .

What is base branch in pull request?

Base Branch: The "Base Branch" is the branch where your new changes should be applied / integrated into. Head Branch: The "Head Branch" is the branch that contains the changes you want to integrate. Title: A short title that describes what this Pull Request is about.


2 Answers

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.

like image 197
Lucas Avatar answered Nov 13 '22 23:11

Lucas


  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/[email protected]
    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_HEAD_REF!
like image 30
Bartleby Avatar answered Nov 13 '22 22:11

Bartleby