Retrieve the commit hash
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.
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}