Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve the commit hash

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?

like image 947
Daniël van den Berg Avatar asked Jan 28 '16 14:01

Daniël van den Berg


People also ask

How do you pull from a commit hash?

To pull up a list of your commits and their associated hashes, you can run the git log command. To checkout a previous commit, you will use the Git checkout command followed by the commit hash you retrieved from your Git log.

How do I restore a commit hash?

# open the git config editor $ git config --global --edit # in the alias section, add ... [alias] lastcommit = rev-parse HEAD ... From here on, use git lastcommit to show the last commit's hash. Show activity on this post.

What is commit hash in git?

The commit hash is an SHA-1 hash made up of a few properties from the commit itself. As mentioned above, it is a lot more complex than this post can go into, but understanding the fundamentals is a great first step. The git hash is made up of the following: The commit message. The file changes.

What is commit hash in bitbucket?

You can get the hash if you click on Commits in the repository. It will not give you the full hash, just the first 11 characters. You can also get the hash if you click on a file in the source view, that will show the latest commit based on the branch you are on.


2 Answers

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} 
like image 176
VivaceVivo Avatar answered Nov 15 '22 06:11

VivaceVivo


The variable you are looking for is CI_COMMIT_SHA (formerly CI_BUILD_REF in GitLab 8.x and earlier) which one of the predefined variables.

All predefined variables are listed here.

like image 32
Fairy Avatar answered Nov 15 '22 06:11

Fairy