Short-form $CI_COMMIT_SHA (git rev-parse --short) in .gitlab-ci.yml

Hi

I’m having trouble figuring out how (if it all) to get a short-form version of the $CI_COMMIT_SHA in .gitlab-ci.yml. Often a short-form commit SHA created with git rev-parse --short is used for referencing a commit, for example see how gitlab presents a shortened form of the commit SHA instead of the full-length one in the gitlab UI. I need that shortened form in my CI to name builds and other stuff in my CI/CD flow. The full commit SHA just doesn’t cut it because it’s too long.

Right now I have a before_script that does export SFSHA=$(git rev-parse --short=8 $CI_COMMIT_SHA) which makes the short-form commit SHA available in scripts, but it’s still not available outside of the scripts for use in environment urls and so on.

Is it possible to get a short-form commit SHA that is available in the whole runner? Is there already such a variable that I’ve just failed to discover?

This has been requested many times over and over.
Looks like it may now be possible using:

${CI_COMMIT_SHA:0:8}

1 Like

But due to this issue https://gitlab.com/gitlab-org/gitlab-ce/issues/24256 , it cannot be used in pre-script to create variable. Only works if you use it directly in build step.

There is now a predefined variable CI_COMMIT_SHORT_SHA providing an 8-character Git SHA.

Interestingly, the reportedly equivalent Git command produces only 7 characters, e.g.

$ git rev-parse --short HEAD
d729d11

Also on GitHub and Bitbucket 7-character Git SHAs are displayed.

Does anyone know why only GitLab uses 8 characters for the short Git SHA? Why not 7 as git spits out?

1 Like