Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot correctly substitute multiline Docker tags variable in Azure DevOps Pipelines

I am adopting a Docker-semver compliant build model.

I need to build several tags for each image, respecting the semver model with replaceable tags.

I use Azure DevOps for building.

I have created a Node script, not shared for brevity, especially because it works, to compute all the tags according to the project version in package.json and writing them to a file.

I need to pass the computed tags to the Docker task, but I am having troubles. The following involves setting a multiline pipeline variable to the requested tags, and using it in the Docker task

      - task: CmdLine@2
        displayName: Import version into pipeline variable
        inputs:
          script: echo "##vso[task.setvariable variable=dockerTags;isOutput=true]$(cat dist/docker.tags | sed ':a;N;$!ba;s/\n/%0D%0A/g')"


      - task: Docker@2
        displayName: Docker build
        inputs:
          command: build
          repository: $(containerRepositoryName)
          tags: $(dockerTags)

When running the pipeline, the variable $(dockerTags) is not substituted correctly

invalid argument "***/amlc/aml-controlli-fe:$(dockerTags)" for "-t, --tag" flag: invalid reference format

What is wrong in my setvariable task? I ran that code into a Docker container, to make sure the substitution occurred

/app # echo "##vso[task.setvariable variable=dockerTags;isOutput=true]$(cat dist/docker.tags | sed ':a;N;$!ba;s/\n/%0D%0A/g')"
##vso[task.setvariable variable=dockerTags;isOutput=true]0.1.0-RELEASE%0D%0A0.1-RELEASE%0D%0A0-RELEASE%0D%0Alatest-RELEASE

But obviously that output is suppressed by the agent

Any idea?

like image 424
usr-local-ΕΨΗΕΛΩΝ Avatar asked Oct 20 '25 02:10

usr-local-ΕΨΗΕΛΩΝ


1 Answers

I was able to resolve this by setting the variable as follows:

  variables:
    imageTags: |
      latest
      $(Build.BuildId)
      any_other_tags

And then using it in the docker step like this:

      - task: Docker@2
        displayName: Build image ${{imageName}}
        inputs:
          command: build
          ...
          tags: $(imageTags)
like image 136
Simon Clough Avatar answered Oct 22 '25 06:10

Simon Clough