Gitlab Kubernetes CD automatic tag change

9/13/2020

My team developing a kubernetes app in Gitlab using Helm. As part of our review process we deploy an instance of our app into a namespace on MR. Docker images are built and tagged either as latest (for current master stable) or with the branch of the changes.

I would like for the review app to automatically use the latest of the branch if it exists else use latest, hopefully without needing intervention to modify the image tags by hand.

What is the cleanest way to do this? I've looked a number of places

-- VV1
gitlab-ci
kubernetes
kubernetes-helm
kustomize

1 Answer

9/13/2020

You can pass docker image tag value when deploying helm using --set. So for different branches - master and non-master - in a pipeline you can have different stages where in one you'll pass --set dockerImageTag=latest and for MR branches it'll be --set dockerImageTag=<branch-name>.

mr-build-deploy:
  only:
    - merge_requests
  script:
    - docker build ...
    - docker tag <docker-tag-name>
    - helm install --set dockerImageTag=<docker-tag-name> ...
 ...

master-build-deploy:
  only:
    - master
  script:
    - docker build ...
    - docker tag latest
    - helm install --set dockerImageTag=latest ...
 ...

Or in values.yaml for the lem chart you can set default value of dockerImageTag to latest so for master step you won't need to specify it.

Speaking of best practices it's not recommended to use latest tag, rather use version number (for example in a form of SemVer) it so you're sure which changes in docker image broke the app if it happens.

-- Anna Slastnikova
Source: StackOverflow