Access to target branch on push

Is there any environment variable or context available that will identify the target branch for a push on a pull request? Eg. how would I set up a job that only runs for pushes on a PR with a specific target branch?

In the following example, the target branch would be “master”:

Screen Shot 2019-11-01 at 8.01.12 PM.png

By the way, it would also be useful to have access to diff (eg. a list of paths) generated for the action as described here: https://help.github.com/en/github/automating-your-workflow-with-github-actions/workflow-syntax-for-github-actions#git-diff-comparisons

Is what I want “base.ref” as shown here: https://developer.github.com/v3/activity/events/types/#pullrequestevent ?

If so, I will need access to this information in a push event.

Base ref will give you the information about the base of a pull request - yes.  This information is provided to you in the github context.  So you can use this either as a variable in your workflow, or an environment variable in workflow executions.  For example:

run: |
  echo "Base ref is $GITHUB_BASE_REF"

Or to use this as a test for a step:

run: echo "Hello, world"
if: github.base_ref == 'refs/heads/master'

But if your goal is to create a workflow that only runs for pushes on a PR with a specific target branch then you can codify that in the trigger for the entire workflow:

on:
  pull_request:
    branches:
    - master
1 Like

My original intent was to be able to derive a diff list of files for changed on any PR which is set to merge into a specific branch. I want this to essentially be able to implement my own path filtering logic, similar to how you can restrict actions to specific repository paths, except that I want my action to apply to the entire repository but execute different commands based on the diff list. Your suggestion will allow me to do this, thank you (although it would be nice if Github also provided the list they diff’d against in the context).

One note: when using “pull_request” as the trigger instead of “push”, the SHA I get in ${{ github.sha }} does not seem to point to a valid commit. It seems to be referencing a commit on some internal pull request branch instead of the latest commit that triggered the pull request event (see: https://help.github.com/en/github/automating-your-workflow-with-github-actions/events-that-trigger-workflows#pull-request-event-pull_request) – that doesn’t seem very useful, how can I reference the commit that caused the trigger when using this event type?

Ah, the following works for me:

${{ github.event.pull_request.head.sha }}

A bit confusing but it’s in there. Thanks for the tips!

Came across the same issue. Wouldn’t using pull_request event only trigger during specific actions (https://developer.github.com/v3/activity/events/types/#pullrequestevent)? So if I open PR and then push more commits into that branch it doesn’t seem that the event will fire for those pushes.

@mkorablin you can specify which PR events you care about: https://help.github.com/en/github/automating-your-workflow-with-github-actions/events-that-trigger-workflows#pull-request-event-pull_request

By default, if you don’t specify any types, it will use  openedsynchronize , or  reopened. It’s not very clear from the documentation, but  synchronize  gets triggered when updates are pushed to the PR (eg: a new commit is pushed).

1 Like