How to build Docker images with the commit's hash

I'm managing a GitLab CICD pipeline which builds Docker images in a couple of stages and now I want to include in each Docker image a label with git's commit hash (see label-schema's vcs-ref label).

I've noticed that GitLab already provides convenient env variables with that info (see CI_COMMIT_SHA and CI_COMMIT_SHORT_SHA in GitLab CI/CD environment variables) but I have no idea of how to include the info provided in those env variables in a Docker image. Does anyone know if it's possible to include git's commit hash in a label?

1 answer

  • answered 2019-07-05 10:33 Prakash Krishna

    In your Dockerfile, use LABEL

    LABEL commit_short_sha=YOUR_CI_COMMIT_SHORT_SHA

    Use docker inspect to get the labels from the images

    docker inspect --format='{{.Config.Labels.commit_sha}}' image:tag