'Getting current branch and commit hash in GitHub action

I want to build a docker image using a GitHub action, migrating from TeamCity.

In the build script, I want to tag the image with a combination of branch and commit, e.g. master.ad959de. Testing that locally, I get that information like this:

git_branch=`git symbolic-ref --short HEAD`
git_hash=`git rev-parse --short HEAD`
docker_version=${git_branch}.${git_hash}

This is the relevant part of the GitHub action:

name: CI
on: [push]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1

    - name: Create docker image
      run: ./docker-build.sh  

Running that script in that GitHub action results in this error:

fatal: ref HEAD is not a symbolic ref

How can I generate a tag like that inside a GitHub action?



Solution 1:[1]

A handy way to get the currrent branch and commit sha on the workflow could be getting it and saving on a "variable".

  - name: Declare some variables
    id: vars
    shell: bash
    run: |
      echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
      echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"

  - name: Another step
    run: |
      echo "Branch: ${{ steps.vars.outputs.branch }}"
      echo "Sha: ${{ steps.vars.outputs.sha_short }}"

Maybe your docker-build.sh could receive the branch and she as parameter, os the full version as parameter.

  - name: Create docker image
     run: ./docker-build.sh "${{ steps.vars.outputs.branch }}.${{ steps.vars.outputs.sha_short }}"

OR JUST

  - name: Create docker image
     run: ./docker-build.sh "${GITHUB_REF#refs/heads/}.${GITHUB_SHA}"

On this action you can see many tests I done to see what works, what don't.

Solution 2:[2]

Another approach is to use github context.

- name: Create docker image
  run: ./docker-build.sh ${{ github.head_ref }}.${{ github.sha }}

The benefit of this approach is that you don't have to add a step to set the values. Note that it uses the full version of the sha (not the short version).

Solution 3:[3]

Simplest way to get a shortened SHA using Environment variables:

- name: Build Docker Image
    run: ./docker-build.sh alpha.${GITHUB_SHA::6}

Solution 4:[4]

You can get it this way in your sh file -

BRANCH_NAME=$(echo $GITHUB_REF | cut -d'/' -f 3)
GITHUB_SHA_SHORT=$(echo $GITHUB_SHA | cut -c1-7)

Solution 5:[5]

Maybe Like this

name: CI 
on: push 
jobs:   
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1

    - name: Get Branch
      id: branch
      run: echo "git_branch=${GITHUB_REF#refs/heads/}" >> $GITHUB_BRANCH

    - name: Check Branch
      run: echo "${{ env.branch }}"

    - name: Get Hash
      id: hash
      run: echo "git_hash=$(git rev-parse --short "$GITHUB_SHA")" >> $GITHUB_HASH

    - name: Check Hash
      run: echo "${{ env.hash }}"

    - name: Create docker image
      run: ./docker-build.sh ${{ env.branch }} ${{ env.hash }}

Solution 6:[6]

You should be able to access the SHA for the latest Git commit with ${{ github.event.pull_request.head.sha }}, assuming this is an Action triggered by a Pull Request

Solution 7:[7]

Using https://github.com/tj-actions/branch-names provides an output which also works for push and pull_request events


...
    steps:
      - uses: actions/checkout@v2
      - name: Get branch names
        uses: tj-actions/[email protected]
        id: branch-names
      
      - name: Current branch name
        if: github.event_name == 'pull_request'
        run: |
          echo "${{ steps.branch-name.outputs.current_branch }}"
        # Outputs: "feature/test" current PR branch.
      
      - name: Current branch name
        if: github.event_name == 'push'
        run: |
          echo "${{ steps.branch-name.outputs.current_branch }}"
        # Outputs: "main" the default branch that triggered the push event.
      
      - name: Get Ref brach name
        run: |
          echo "${{ steps.branch-name.outputs.ref_branch }}"
        #  Outputs: "main" for non PR branches | "1/merge" for a PR branch

      - name: Get Head Ref branch name
        if: github.event_name == 'pull_request'
        run: |
          echo "${{ steps.branch-name.outputs.head_ref_branch }}"
        # Outputs: "feature/test" current PR branch.

      - name: Get Base Ref branch name
        if: github.event_name == 'pull_request'
        run: |
          echo "${{ steps.branch-name.outputs.base_ref_branch }}"
        # Outputs: "main" for main <- PR branch.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Tiago Gouvêa
Solution 2 ifoukarakis
Solution 3 Azim Saiyed
Solution 4 Artur A
Solution 5 Fauzi Firdaus
Solution 6 bodacious
Solution 7 jackotonye