4

I'm trying to set up a Github action push event to master (so, a merge). The problem is that for what I want to do, I need the name of the original pull request branch. The Github pull request event makes this easy by calling ${{ github.head_ref }}, but this option doesn't exist on a push event. I originally thought ${{ github.head }} would give me what I want, but it actually just gives me refs/heads/master, not the name of the original PR branch.

In other words, say I opened a PR to the master branch with a branch named foo. In a push event, I want to be able to get foo for what I want to do in the job.

Is this possible?

burt734r
  • 121
  • 1
  • 5

3 Answers3

8

I found an acceptable workaround solution for my use case using a pull request event instead of a push event:

on:
  pull_request:
    types: [closed]
    branches:
      - master

The above says to run only on pull requests to the master branch that have been closed, since merged PRs are closed automatically. But in order to have this action only run on closed PRs that have also been merged and not just any closed PR, in the relevant step(s) in the job, I add a conditional check to make sure the closed PR has also been merged:

if: github.event.pull_request.merged == true

And then I can access the branch name as I mentioned in the question: ${{ github.head_ref }}

burt734r
  • 121
  • 1
  • 5
  • In my case, I need to modify condition a little and have to use below environment variable. condition: if: ${{ github.ref == 'master' && github.event.pull_request.merged == true }} Variable: echo "GITHUB_HEAD_REF are ---> $GITHUB_HEAD_REF" Example: GITHUB_HEAD_REF are ---> release/9.0.0 – Ashish Sharma Dec 22 '21 at 17:38
1

For the first question, how to get source branch name on push action on master? Github action does not provide the way so far. I created an action to help to achieve it but require some prerequisite.

  • This action need to run on main or any (or any merging target branch)
  • The source branch need to be merge from Github Merge button and the auto message must not be change.
  • If the branch is not merged by Github Merge button, you need to specify branch name e.g xxx xxx/branch-name xxx in commit message

Then you can get source name. Example

name: "Log RECENT_MERGED_BRANCH_NAME"
on:
    push:
      branches:
        - "main"
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - uses: tonynguyenit18/github-action-custom-vars@v1
      - run: echo $RECENT_MERGED_BRANCH_NAME

Check more details here: https://github.com/tonynguyenit18/github-action-custom-vars

Tony Nguyen
  • 3,298
  • 11
  • 19
-1

If you just want to get the branch that triggered the pipeline then you can get it from env GITHUB_REF also able to accessed as ${{ github.ref }}. If you are triggering on a tag, then GITHUB_REF won't have the proper branch name so you will need follow process shown here How to get a branch name on GitHub action when push on a tag?

Edward Romero
  • 2,905
  • 1
  • 5
  • 17
  • On push events, isn't this always set to the target branch of the push (e.g. master or main)? – opike Feb 25 '21 at 12:14
  • It is populated to the branch name that triggered the workflow. So if you push to a branch “feature-branch-1” then you’ll get something like “ref/heads/feature-branch-1” from the env. – Edward Romero Feb 25 '21 at 12:20
  • 1
    that's on updating the pull request branch, but on merging with master which creates a "push" to master event, that value is always set to the target branch, i.e. master – opike Feb 25 '21 at 12:47
  • @opike note that in providing information on all events. Here are the GitHub actions docs. This envs are set in all runner envs depending on the trigger. Feel free to look at the definition of github ref https://docs.github.com/en/actions/reference/environment-variables – Edward Romero Feb 25 '21 at 13:47
  • 2
    I never claimed it wasn't being set. What I'm saying is that when merging a pull request with the default branch (we'll say master in this case), a "push" event on the master branch is triggered and GITHUB_REF will be set to "master". The OP is looking for the source branch of the pull request, not the target branch of "master". – opike Feb 25 '21 at 20:15