Retrieve the commit hash

Daniël van den Berg picture Daniël van den Berg · Jan 28, 2016 · Viewed 15.4k times · Source

I'm currently working on a deployment script to run as part of my GitLab CI setup. What I want is to copy a file from one location to another and rename it.

Now I want to be able to find what commit that file was generated with, so I'd like to add the hash of the commit to it.

For that to work I'd like to use something like this:

cp myLogFile.log /var/log/gitlab-runs/$COMMITHASH.log

The output should be a file named eg.

/var/log/gitlab-runs/9b43adf.log

How is this possible to achieve using GitLab CI?

Answer

VivaceVivo picture VivaceVivo · Mar 29, 2017

In your example you used the short git hash that you would get with the predefined variable CI_COMMIT_SHA by building a substring like this:

${CI_COMMIT_SHA:0:8}