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?
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.
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.
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.
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 }}
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}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With