30

During a GitHub action, I'd like to know the name of the branch:

  • for a push action: name of the current branch
  • for a pull_request action: name of the target branch

I need a string like develop, master or feature/xxx (and not refs/pull/…).

The ${{ github.ref }} var gives me refs/heads/develop. How can I get only the develop?

Edric
  • 24,639
  • 13
  • 81
  • 91
Sylvain
  • 2,742
  • 5
  • 21
  • 34
  • 2
    Does this answer your question? [How to get current branch within github actions](https://stackoverflow.com/questions/58033366/how-to-get-current-branch-within-github-actions) – Cloudkollektiv Nov 25 '20 at 10:58
  • When you say "for a pull_request action: name of the target branch", do you mean source branch? For example, if you create a PR to merge branch `topic-1` into `master`, are you wanting the source branch `topic-1` or the target branch `master`? – Michael R Sep 05 '21 at 18:46

5 Answers5

43

Update: GitHub added the context variable ${{ github.ref_name }}, returning "The branch or tag name that triggered the workflow run."

Original Answer:

You can create a step output of the last part of GITHUB_REF like the following.

on: push
jobs:
  example:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

      - name: Set output
        id: vars
        run: echo ::set-output name=short_ref::${GITHUB_REF#refs/*/}

      - name: Check output
        run: echo ${{ steps.vars.outputs.short_ref }}
peterevans
  • 34,297
  • 7
  • 84
  • 83
  • 4
    This value does not match the name of the target branch during a PR, as the OP requested – Michael R Sep 05 '21 at 18:43
  • 1
    In the meantime (?) GitHub added another context variable to the `github` context: `github.ref_name` which returns "The branch or tag name that triggered the workflow run." according to https://docs.github.com/en/actions/learn-github-actions/contexts#github-context. – Michael Mar 28 '22 at 12:03
  • 13
    `github.ref_name` does not return the branch name on a PR... it returns something like this `144/merge` – ReenigneArcher Jun 11 '22 at 16:31
  • @Archer that's true. what the heck is this?! – Bright Lee Dec 27 '22 at 16:05
11

This creates an environment variable GIT_BRANCH which has the following behavior:

  • Outside of a PR, this is set to the branch short name (master, not refs/heads/master)
  • Inside a PR, this is set to the target branch short name (master, not refs/pull/123/merge)
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Set environment variables
        run: |
          # Short name for current branch. For PRs, use target branch (base ref)
          GIT_BRANCH=${GITHUB_BASE_REF:-${GITHUB_REF#refs/heads/}}
          echo "GIT_BRANCH=$GIT_BRANCH" >> $GITHUB_ENV

How this works:

  • GITHUB_BASE_REF is the short branch name for the target branch during a PR trigger, and it is empty otherwise.
  • GITHUB_REF always has a value, but the value changes based on the context. Outside of a PR, GITHUB_REF is the "full" branch name (refs/heads/master). Inside of a PR, it is the PR branch name (refs/pull/123/merge).

So this script uses GITHUB_BASE_REF if set, otherwise it uses GITHUB_REF and removes the "refs/heads/" prefix.

Michael R
  • 1,753
  • 20
  • 18
  • 1
    The OP is asking for how to get feature branch as well. This will return the branch that the PR is targeting, not the PR branch itself. – CWSites Sep 01 '21 at 13:54
  • The OP is asking for the target branch, not the source branch. I personally think the source branch makes more sense here, but the OP specifically says "for a pull_request action: name of the target branch". – Michael R Sep 05 '21 at 18:36
  • 3
    To get the source branch instead of a target branch during the PR (which makes more sense to me), you can use the env var `GITHUB_HEAD_REF` instead of `GITHUB_BASE_REF` – Michael R Sep 05 '21 at 18:39
4

You can get it from github object like ${{ github.head_ref }}

Hossam Ghareeb
  • 7,063
  • 3
  • 53
  • 64
  • 11
    `head_ref` is only set for pull requests, so although this will work for pull request actions, it won't work for branch push actions (the OP asks about both). For branch push actions, the branch name would need to be extracted out from `$GITHUB_REF`or `${{ github.ref }}` – Tom Mar 20 '21 at 17:12
  • It's probably `github.base_ref` you were looking for. See https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions#github-context – Zach Jun 07 '21 at 02:17
  • 1
    Use `${{ github.head_ref || github.ref_name }}` so it works inside and outside of pull requests. – Etienne Dec 16 '22 at 16:13
3

Another option is to have it as an environment variable:

  steps:
    - name: Set env.BRANCH
      run: echo "BRANCH=$(echo $GITHUB_REF | cut -d'/' -f 3)" >> $GITHUB_ENV

    - name: Is main?
      if: env.BRANCH == 'main'
      run: echo "This is 'main' branch"
dhilt
  • 18,707
  • 8
  • 70
  • 85
  • This technique would not have the desired value when in a pull request (GITHUB_REF would be "refs/pull/123/merge" instead of the target branch as requested) – Michael R Sep 05 '21 at 19:11
0

You can get the branch name using the following syntax -

name: build

on: [push, pull_request]

jobs:
  dummy-fake:
    runs-on: ubuntu-latest
    steps: 
      - name: Checkout repo
        uses: actions/checkout@v2
      - name: Verify run id
        run : echo Run Id $GITHUB_RUN_ID 
      - name: Output the branch name
        run: echo $(echo $GITHUB_REF | cut -d'/' -f 3)
Abhinav Manchanda
  • 6,546
  • 3
  • 39
  • 46