Azure Release Pipeline: Docker Image Tag will not be replaced even after adding step to replace image tag

2.3k views Asked by At

I am using classic editor to create a release pipeline to deploy to Azure Kubernetes Service, however, I keep getting the error of InvalidImageName after deploying in Azure Kubernetes Service (AKS). The issue is due to aks not being able to find the specified image in Azure Container Registries (ACR) where the docker image tag ${TAG} variable cannot seem to be replaced by the build number from the pipeline $(Build.BuildId).

My Azure Container Registries (ACR) repositories

enter image description here

The error screenshot in aks:

enter image description here

I have tried to create variable in the release pipeline to assign the value to the TAG variable. Thereafter, I went on to create a 'Command line' task step to set the value into the deployment.yml file. It is setting the value as shown below. However, when I run the kubectl apply command to run the deployment.yml file, the TAG is not showing the build number. I would greatly appreciate any help on this.

enter image description here

enter image description here

enter image description here

Prior to seeking help, I have try out some of the stackoverflow answers from this questions, but was unable to do so How to kubernetes "kubectl apply" does not update existing deployments How to provide docker image tag dynamically to docker-compose.yml in Azure Release Pipeline Task?

3

There are 3 answers

0
LoLance On

Instead of running envsubst '${TAG}' <deployment.yml directly, try this one:

echo "$(envsubst '${TAG}' <path\deployment.yml)" > path\deployment.yml

And it's recommended to navigate(cd) to the directory where your depolyment.yml file exists firstly, then run command echo "$(envsubst '${TAG}' <deployment.yml)" > deployment.yml.

More details about your issue:

The < means infile, you still need to output the changes to one file using >.

Check the Note here: If if we want to use the same file as both source file and destination file, we'll have to use something like moreutil's sponge. However sponge is not available in Hosted agent, so I suggest using echo "$(originalCommand)" > outputfile as an workaround.

0
jerboa-io On

in azure-pipelines.yml under the build stage there is a spot for tags to be added.

stages:
- stage: Build
  displayName: Build stage
  jobs:  
  - job: Build
    displayName: Build
    pool:
      vmImage: $(vmImageName)
    steps:
    - task: Docker@2
      displayName: Build and push an image to container registry
      inputs:
        command: buildAndPush
        repository: $(imageRepository)
        dockerfile: $(dockerfilePath)
        containerRegistry: $(dockerRegistryServiceConnection)
        tags: |
          $(tag)
          $(release)

its on line 42 for me. in the tags section you can either add more tag variables or simply hard code them in. my setup above creates 2 tags in acr one with the build number and one with the contents of the $(release) variable that is set on the pipeline.

Both tags reference the same manifest.

2
Krzysztof Madej On

Natively you are not able to do (at least at the moment).

However, you can use for instance HELM and change deployment to chart.

Another approach is to use token replace and definitely less work than with HELM. For that (for default configuration) you need to change syntax to #{TAG}#.

And if you use linux host agent you may also consider envsubst and then

    - script: |
        envsubst '${TAG}' < deployment-template.yaml > deployment.yaml
      displayName: Replace Environment Variables

I assume that you file with ${TAG} is named deployment-template.yaml.