You want to have docker tags that match your git branches? Here is how to do it with Gitlab CI.

A lot of my projects have a CI pipeline that builds a docker image. Of course I do not want to always deploy the :latest tag, because that makes reproducibility and rollbacks hard.

I always push to :latest. Also I want to reference by:

  • tags/branches
  • commit hash

For this repo (the blog) it looks like this with Gitlab CI:

  stage: build
  image: docker
  - docker build -t .
  - docker login --password $DCR_PASS --username $DCR_USER
  - docker tag${CI_COMMIT_SHA}
  - docker tag${CI_COMMIT_REF_SLUG}
  - docker tag
  - docker push
  - docker push${CI_COMMIT_SHA}
  - docker push${CI_COMMIT_REF_SLUG}

Since the docker image is always the same(it is only built once) this almost uses no additional disk space.


A nice gimmick with Kubernetes is that I can immediately set the new image after pushing the new code, while the image is still being built. Kubernetes will retry pulling the image until it’s ready and only then tear down old pods.