Search code examples
gitdockergithub-actions

How to get the short sha for the github workflow?


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?


Solution

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

          - name: Set outputs
            id: vars
            run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
          - name: Check outputs
            run: echo ${{ steps.vars.outputs.sha_short }}