0

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?

Richard Rodjues
  • 207
  • 1
  • 6
  • 23
  • Apart from infile, you also need to specify output file when you're trying to use same file as source file and destination file. Feel free to try my answer and let me know if it helps or not :) – LoLance Sep 28 '20 at 07:41
  • Hi friend, how about the issue? Does the answers below resolve your question, If yes, you could [Accept it as an Answer](https://meta.stackexchange.com/a/5235/515442) , so it could help other community members who get the same issues and we could archive this thread, thanks. – LoLance Sep 30 '20 at 09:42

3 Answers3

0

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.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
  • At the task with display name 'Replace Environment Variables', i run the following command `- script: | envsubst '${TAG}' < deployment-template.yaml > deployment.yaml displayName: Replace Environment Variables` It can change the image tag to the build number, but once it proceed on to the next task, the image tag goes back to ${TAG} – Richard Rodjues Sep 25 '20 at 16:38
  • How do I go about using Helm then to solve this issue? – Richard Rodjues Sep 25 '20 at 16:44
0

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.

LoLance
  • 25,666
  • 1
  • 39
  • 73
0

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.

jerboa-io
  • 121
  • 1
  • 2