Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the short sha for the github workflow?

Tags:

I am creating a workflow in GitHub which creates and uses a docker image. Therefore I have started my workflow file with a global environment variable for this docker image which is visible for all the jobs in my workflow:

name: continuous integration on:   push:     branches:       - '**'  env:   IMAGE: docker.pkg.github.com/${{ github.repository }}/jactor-persistence:${{ github.sha }} 

I want to replace ${{ github.sha }} with the short sha of the head commit, the same as the result of the following command git rev-parse --short HEAD

Is this possible?

like image 964
jactor-rises Avatar asked Jan 19 '20 14:01

jactor-rises


People also ask

What is GitHub short SHA?

short-sha is a GitHub Action than provides an output sha with the shortened commit SHA. ⚠️ On November 16th, 2020 Github removes the set-env command. Version prior to v1. 2 of this action will not work.

How many characters are used in SHA key in GitHub?

Generally, eight to ten characters are more than enough to be unique within a project. One of the largest Git projects, the Linux kernel, is beginning to need 12 characters out of the possible 40 to stay unique. 7 digits are the Git default for a short SHA, so that's fine for most projects.

Is Git Short commit hash unique?

The only requirement for a git short commit hash is that it's unique within that repository, the git client will use variable length (default: 7) while GitLab seems to always use 8 characters.


2 Answers

As VonC mentioned, you can just compute the string yourself in a previous step.

      - name: Set outputs         id: vars         run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"       - name: Check outputs         run: echo ${{ steps.vars.outputs.sha_short }} 
like image 177
peterevans Avatar answered Sep 19 '22 07:09

peterevans


You can also set an environment variable with the short sha:

    - name: Add SHORT_SHA env property with commit short sha       run: echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`" >> $GITHUB_ENV 

SHORT_SHA can then be used like any other environment variable, e.g. like this:

    - name: My step       run: myscript ${SHORT_SHA} 
like image 43
Lars Grammel Avatar answered Sep 21 '22 07:09

Lars Grammel