How to get the current branch within Github Actions?

GithubGithub Actions

Github Problem Overview


I'm building Docker images with Github Actions and want to tag images with the branch name.

I found the GITHUB_REF variable, but it results in refs/heads/feature-branch-1 and I need only feature-branch-1.

Github Solutions


Solution 1 - Github

I added a separate step for extracting branch name from $GITHUB_REF and set it to the step output

- name: Extract branch name
  shell: bash
  run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
  id: extract_branch

after that, I can use it in the next steps with

- name: Push to ECR
  id: ecr
  uses: jwalton/gh-ecr-push@master
  with:
    access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
    secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    region: us-west-2
    image: eng:${{ steps.extract_branch.outputs.branch }}

Solution 2 - Github

I believe GITHUB_REF is the only environment variable that includes the branch name.

You can extract just the branch name from the rest of that string like this:

${GITHUB_REF##*/}

Example:

$ GITHUB_REF=refs/heads/feature-branch-1
$ echo ${GITHUB_REF##*/}
feature-branch-1

Update: Added a complete workflow example.

Workflow

name: CI
on: push
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Git checkout
        uses: actions/checkout@v1
      - name: Branch name
        run: echo running on branch ${GITHUB_REF##*/}
      - name: Build
        run: docker build -t tedmiston/tag-example:${GITHUB_REF##*/} .

Source: https://github.com/tedmiston/x/blob/master/.github/workflows/workflow.yml

Sample output - master branch

Run docker build -t tedmiston/tag-example:${GITHUB_REF##*/} .
  docker build -t tedmiston/tag-example:${GITHUB_REF##*/} .
  shell: /bin/bash -e {0}
Sending build context to Docker daemon  146.9kB

Step 1/1 : FROM alpine
latest: Pulling from library/alpine
9d48c3bd43c5: Pulling fs layer
9d48c3bd43c5: Verifying Checksum
9d48c3bd43c5: Download complete
9d48c3bd43c5: Pull complete
Digest: sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb
Status: Downloaded newer image for alpine:latest
 ---> 961769676411
Successfully built 961769676411
Successfully tagged tedmiston/tag-example:master

Log: https://github.com/tedmiston/x/commit/cdcc58a908e41d3d90c39ab3bf6fef1ad2c4238a/checks#step:4:16

Sample output - non-master branch

Run docker build -t tedmiston/tag-example:${GITHUB_REF##*/} .
  docker build -t tedmiston/tag-example:${GITHUB_REF##*/} .
  shell: /bin/bash -e {0}
Sending build context to Docker daemon  144.9kB

Step 1/1 : FROM alpine
latest: Pulling from library/alpine
9d48c3bd43c5: Pulling fs layer
9d48c3bd43c5: Verifying Checksum
9d48c3bd43c5: Download complete
9d48c3bd43c5: Pull complete
Digest: sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb
Status: Downloaded newer image for alpine:latest
 ---> 961769676411
Successfully built 961769676411
Successfully tagged tedmiston/tag-example:branch-name-test

Log: https://github.com/tedmiston/x/commit/4e8d31259f861aaa2c30375756fc081c3659bddf/checks#step:4:16


See this answer for more on parameter expansion syntax.

For reference the page Virtual environments for GitHub Actions lists all of the environment variables available in the execution environment.

Solution 3 - Github

Be aware that if you are executing your GitHub action on pull request trigger, then GITHUB_REF variable will contain something like refs/pull/421/merge so if you will try to git push to that name it will most likely fail.

What you can use though are references on the GitHub context in your YAML. Something like: ${{ github.head_ref }}

https://help.github.com/en/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#github-context

Github action context refs

Solution 4 - Github

Using setenv is now deprecated. It is advised to use environment files. Building on @youjin's answer, while still allowing feature/ branches (replacing all occurences of / with -), I am now using this:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Get branch name (merge)
        if: github.event_name != 'pull_request'
        shell: bash
        run: echo "BRANCH_NAME=$(echo ${GITHUB_REF#refs/heads/} | tr / -)" >> $GITHUB_ENV

      - name: Get branch name (pull request)
        if: github.event_name == 'pull_request'
        shell: bash
        run: echo "BRANCH_NAME=$(echo ${GITHUB_HEAD_REF} | tr / -)" >> $GITHUB_ENV

      - name: Debug
        run: echo ${{ env.BRANCH_NAME }}

Solution 5 - Github

You could use https://github.com/rlespinasse/github-slug-action

# Just add this  => 
- name: Inject slug/short variables
  uses: rlespinasse/[email protected]



# And you get this  => 
- name: Print slug/short variables
  run: |
    echo "Slug variables"
    echo " - ${{ env.GITHUB_REF_SLUG }}"    
    echo " - ${{ env.GITHUB_HEAD_REF_SLUG }}"
    echo " - ${{ env.GITHUB_BASE_REF_SLUG }}"
    echo " - ${{ env.GITHUB_REPOSITORY_SLUG }}"
    # output e.g. : master feat-new-feature v1.0.0 product-1.0.0-rc.2 new-awesome-product
    echo "Slug URL variables"
    echo " - ${{ env.GITHUB_REF_SLUG_URL }}"
    echo " - ${{ env.GITHUB_HEAD_REF_SLUG_URL }}"
    echo " - ${{ env.GITHUB_BASE_REF_SLUG_URL }}"
    echo " - ${{ env.GITHUB_REPOSITORY_SLUG_URL }}"
    # output e.g. : master feat-new-feature v1-0-0 product-1-0-0-rc-2 new-awesome-product
    echo "Short SHA variables"
    echo " - ${{ env.GITHUB_SHA_SHORT }}"
    # output e.g. : ffac537e

Solution 6 - Github

> How to get the current branch within Github Actions?

Assuming ${{ github.ref }} is something like refs/heads/mybranch, you can extract the branch name using the following method:

steps:
  - name: Prints the current branch name
    run: echo "${GITHUB_BRANCH##*/}"
    env:
      GITHUB_BRANCH: ${{ github.ref }}

If your branch includes slashes (such as feature/foo), use the following syntax:

steps:
  - name: Prints the current branch name
    run: echo "${GITHUB_REF#refs/heads/}"

Credits: @rmunn comment

Or use the method from the accepted answer, here is much shorter version (lint friendly):

steps:
  - name: Get the current branch name
    shell: bash
    run: echo "::set-output name=branch::${GITHUB_REF#refs/heads/}"
    id: myref

Then refer in other steps as ${{ steps.myref.outputs.branch }}.

Notes:

Solution 7 - Github

The GitHub Action FranzDiebold/github-env-vars-action exposes several useful environment variables, such as current branch name and their slug values. I made this action exactly for this use case.

Usage
steps:
  - uses: FranzDiebold/[email protected]
  - name: Print environment variables
    run: |
      echo "GITHUB_REPOSITORY_SLUG=$GITHUB_REPOSITORY_SLUG"
      echo "GITHUB_REPOSITORY_OWNER=$GITHUB_REPOSITORY_OWNER"
      echo "GITHUB_REPOSITORY_OWNER_SLUG=$GITHUB_REPOSITORY_OWNER_SLUG"
      echo "GITHUB_REPOSITORY_NAME=$GITHUB_REPOSITORY_NAME"
      echo "GITHUB_REPOSITORY_NAME_SLUG=$GITHUB_REPOSITORY_NAME_SLUG"
      echo "GITHUB_REF_SLUG=$GITHUB_REF_SLUG"
      echo "GITHUB_REF_NAME=$GITHUB_REF_NAME"
      echo "GITHUB_REF_NAME_SLUG=$GITHUB_REF_NAME_SLUG"
      echo "GITHUB_SHA_SHORT=$GITHUB_SHA_SHORT"

enter image description here

A demo for all Operating systems (Linux, macOS and Windows) is also available in the demo workflows file of the repository!

Solution 8 - Github

Just use:

env:
 BRANCH_NAME: ${{ github.head_ref || github.ref_name }} 

The trick is that github.head_ref is only set when the workflow was triggered by a pull_request and it contains the value of the source branch of the PR. github.ref_name will than only be used if the workflow was not triggered by a pull_requestand it also just contains the branch name.

Solution 9 - Github

To set it as an environment variable, I use this syntax:

- name: Extract branch name
  shell: bash
  run: echo "::set-env name=BRANCH_NAME::$(echo ${GITHUB_REF#refs/heads/} | sed 's/\//_/g')"
- name: Test
  run: echo "${BRANCH_NAME}"

I found this syntax here: Github actions - starter worflows#How to define env variable? #68

Rmq: the sed 's/\//_/g' is to replace / by _ in the branch name

Solution 10 - Github

Update

GitHub now supports the GITHUB_REF_NAME, which stands for: The branch or tag name that triggered the workflow run.

GitHub docs on this https://docs.github.com/en/actions/learn-github-actions/environment-variables

Solution 11 - Github

I just made a simple test within GitHub Actions using a bash script:

#!/bin/bash

echo Reserved for REPO_NAME=${GITHUB_REPOSITORY##*/}
echo GITHUB_REF=${GITHUB_REF}
echo EXTRACT_GITHUB_REF=${GITHUB_REF##*/}
echo EXTRACT_GITHUB_REF_HEADS=$(echo ${GITHUB_REF#refs/heads/})

cd $REPO_NAME
git checkout ${GITHUB_REF##*/}
git checkout $(echo ${GITHUB_REF#refs/heads/})

Here is screenshot of the output:

enter image description here So both ${GITHUB_REF##*/} and $(echo ${GITHUB_REF#refs/heads/}) are correct

Solution 12 - Github

Heres a complete workflow that works for both push and pull_request events

name: whichBranch
on: [pull_request, push]

jobs:
  which_branch:
    runs-on: ubuntu-latest
    steps:
      - name: Extract branch name on push
        if: github.event_name != 'pull_request'
        shell: bash
        run: echo "::set-env name=BRANCH_NAME::$(echo ${GITHUB_REF#refs/heads/})"
        id: extract_branch

      - name: Extract branch name on pull request
        if: github.event_name == 'pull_request'
        run: echo "::set-env name=BRANCH_NAME::$(echo ${GITHUB_HEAD_REF})"

      - name: Print branch name
        run: echo 'The branch name is' $BRANCH_NAME

Solution 13 - Github

${{ github.ref_name }}

Seems to work fine for for pushes at least.

Solution 14 - Github

For those just finding this thread, you can now use GITHUB_REF_NAME e.g. ${{ github.ref_name }}. https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables

So if your triggered action workflow branch is main this variable will be set to main. This is useful if you have multiple repos with release and main branches, for instance.

Solution 15 - Github

If you're using V2 of actions/checkout then you can always just run git branch --show-current to just get the name of the branch currently checked out.

Solution 16 - Github

Now ${{github.ref}} is the correct way to get branch name. Please remember ${{github.ref}} has refs/heads/.. prefix

Solution 17 - Github

Solution to deal with both pull_request and push events. Implements the workaround to save obtained branch name for further steps, since set-env is deprecated. Doesn't require third-party actions.

name: CI
on: [ pull_request, push ]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: "Get branch name and save to env"
        env:
          IS_PR: ${{ github.EVENT_NAME == 'pull_request' }}
        run: |
          if ${IS_PR}; then
            BRANCH_NAME="${GITHUB_HEAD_REF}"
          else
            BRANCH_NAME="${GITHUB_REF##*/}"
          fi
          echo "BRANCH_NAME=${BRANCH_NAME}" >> $GITHUB_ENV

      - name: "Another step uses branch name"
        run: echo "Branch name is ${{ env.BRANCH_NAME }}"

Runtime Variables in GitHub Actions

Solution 18 - Github

To also deal with the pull_request event (in which case the $GITHUB_REF contains something not useful like refs/pull/519/merge) you can use this one liner:

   - name: Set branch name
     run: echo "::set-output name=branch_name::$(echo ${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}})"

Solution 19 - Github

Use branch name on GitHub actions

Convenience action for using current branch name. Usage

name: build
on: push

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - run: npm ci
    - uses: nelonoel/branch-name@v1
    # Use branch name for whatever purpose
    - run: echo ${BRANCH_NAME}

Solution 20 - Github

For people using Windows image to run actions, a few key points to know about:

  1. It's incorrect to assume that GitHub actions use CMD shell. They use PowerShell by default.
  2. You can specify the shell to use the following way:
- run: |
    ...
  shell: cmd
  1. You can use the value 'bash' to execute a command in the context of a bash shell.

So, all in all, you don't need to waste potentially hours trying to figure out how to do things in dilapidated cmd way (like I did).

And for the simple purpose of getting the name of the current branch, you can either use the popular solutions while setting the shell to 'bash', or use for example the following simple way to set a variable in the default PowerShell shell:

$branchName = $Env:GITHUB_REF -replace "refs/heads/", ""

Solution 21 - Github

I made an action that gets the branch name regardless of pull_request trigger or not. https://github.com/EthanSK/git-branch-name-action

Solution 22 - Github

Just repeating here for better visibility what others have written as simple comments in previous replies:

https://docs.github.com/en/actions/learn-github-actions/environment-variables

The branch name for pull requests only is exposed in this environment variable:

GITHUB_HEAD_REF Only set for pull request events. The name of the head branch.

In GitHub actions, the corresponding context key is:

github.head_ref

Solution 23 - Github

if: github.ref == 'refs/heads/integration' && github.event_name == 'push' 

You can use the above command and replace whatever branch or event you want to run for.

Solution 24 - Github

Running on Windows?. Windows default command is a PowerShell terminal.

  - name: SET CURRENT_BRANCH
    run: |
      $branchName = "${{github.ref}}".Split("/")["${{github.ref}}".Split("/").Length -1]
      echo "::set-env name=CURRENT_BRANCH::$(echo $branchName)"

Solution 25 - Github

Here's a snippet which set's an environment variable based on $GITHUB_REF, which defaults to dev if not present.

Adjust the sed command as per your requirements.

export GIT_BRANCH=$(echo ${GITHUB_REF:-dev} | sed s/.*\\///g)

Solution 26 - Github

Normally, I always have a script written in either nodejs or python, which gets invoked from the workflow.yaml. The script normally takes care of works like getting proper branch reference.

I have a function like below, in a prepare-deployment.js script-

const VALID_REF_PREFIX = 'refs/heads/';
...

function getBranchRef(isProd = false) {
  let branchRef = 'origin/master';

  if (isProd) {
    return branchRef;
  }
  /**
   * When the workflow is invoked from manual flow, the branch name
   * is in GITHUB_REF, otherwise, we have to look into GITHUB_BASE_REF
   */
  if (GITHUB_REF.startsWith(VALID_REF_PREFIX)) {
    // coming from a manual workflow trigger
    branchName = `origin/${GITHUB_REF.replace(VALID_REF_PREFIX, '')}`;
  } else {
    // coming from a PR
    branchRef = `origin/${GITHUB_HEAD_REF}`;
  }

  return branchRef;
}

This takes care of the following scenarios-

  1. I want to deploy changes from a PR to my dev env
  2. I want to deploy changes from any branch I want into my dev env, via a manual trigger
  3. I want to deploy changes from master into my prod env

Solution 27 - Github

There's a very simple git command to get the current branch:

git rev-parse --abbrev-ref HEAD

To get the output in an env file variable simply put:

      - name: Set CURRENT_BRANCH
run: echo "CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_ENV
To get the output from the env variable:
      - name: Get CURRENT_BRANCH
run: echo ${{ env.CURRENT_BRANCH}}

Source: https://www.techiedelight.com/determine-current-branch-name-git/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionaborilovView Question on Stackoverflow
Solution 1 - GithubaborilovView Answer on Stackoverflow
Solution 2 - GithubTaylor D. EdmistonView Answer on Stackoverflow
Solution 3 - GithubDusan PlavakView Answer on Stackoverflow
Solution 4 - GithubsshowView Answer on Stackoverflow
Solution 5 - GithubAntoineView Answer on Stackoverflow
Solution 6 - GithubkenorbView Answer on Stackoverflow
Solution 7 - GithubFranz DieboldView Answer on Stackoverflow
Solution 8 - GithubysfaranView Answer on Stackoverflow
Solution 9 - GithubLE GALL BenoîtView Answer on Stackoverflow
Solution 10 - GithubFederico MoyaView Answer on Stackoverflow
Solution 11 - GithubeQ19View Answer on Stackoverflow
Solution 12 - GithubyoujinView Answer on Stackoverflow
Solution 13 - GithubNaeView Answer on Stackoverflow
Solution 14 - GithubChristopher CookView Answer on Stackoverflow
Solution 15 - Githubsid_devView Answer on Stackoverflow
Solution 16 - GithubPietrekView Answer on Stackoverflow
Solution 17 - GithubDmitryView Answer on Stackoverflow
Solution 18 - GithubaisoView Answer on Stackoverflow
Solution 19 - GithubVAIBHAV GOURView Answer on Stackoverflow
Solution 20 - GithubLeakyView Answer on Stackoverflow
Solution 21 - GithubEthan SKView Answer on Stackoverflow
Solution 22 - GithubMarcello RomaniView Answer on Stackoverflow
Solution 23 - GithubTarun GuptaView Answer on Stackoverflow
Solution 24 - GithubDJ.View Answer on Stackoverflow
Solution 25 - GithubNick GrealyView Answer on Stackoverflow
Solution 26 - GithubMunimView Answer on Stackoverflow
Solution 27 - GithubGlenn TroncquoView Answer on Stackoverflow