Tagging best practices in Azure DeVops for docker images deployed on Kubernetes

10/17/2018

Does anyone have experience in building and pushing docker images + deploying them to AKS via Azure DevOps?

When I build and push an image I can use the variable $(Build.Repository.Name):$(Build.BuildId).

But then I have my .yaml files that I have in my release pipeline to deploy the images. I cannot (or don't know how) to refer to that variable $(Build.Repository.Name):$(Build.BuildId).

Does anyone have experience in automating this?

-- bramvdk
azure-aks
azure-devops
azure-pipelines
docker
kubernetes

2 Answers

10/17/2018

How I got it working for me is by using "tokerisation of the yaml file".

During the build fase (build and push of the image to the private repo I use a default variable in Azure Devops, $(Build.BuidId), as tag for the docker image.

Build image task Build image

Push image task

Push image

In the the deployment yaml for the image I refer to:

Deployment yaml

Then for the deployment before I apply the yaml files, with kubectl apply task, I use the task "Replace tokens". You can specify which files to replace tokens in. Since I only used a token for the image I only selected the deployment yaml file.

artifact source name replace token task

What it does it replaces #{Release.Artifacts.acpyaml.BuildId}# with the actual build nr of the last build, so when it starts pulling the image it has the right tag.

See a full example described on Tokenised version of yaml

-- bramvdk
Source: StackOverflow

10/17/2018

If you have on the release pipeline only 1 artifact so you can use the variables also in the release in the same way ($(Build.Repository.Name):$(Build.BuildId)).

If you more then 1 artifact the variables will refer to your primary artifacts:

enter image description here

If you want to use the variables to other artifacts (not the primary) you can use this way:

Release.Artifacts.{alias}.BuildId

The {alias} is the alias name of the artifacts:

enter image description here

-- Shayki Abramczyk
Source: StackOverflow