I am having some issues parsing runtime variables to an azure template.
My template looks like this:
# template.yaml
parameters:
- name: dockerfilePath
type: string
- name: dockerImageName
type: string
- name: tag
type: string
- name: pushAsLatest
type: string
default: false
- name: DependsOn
type: string
default: ""
jobs:
- job: pushtoharbor
dependsOn: ${{ parameters.DependsOn }}
displayName: Push to Harbor
steps:
- checkout: self
- task: Docker@2
displayName: "Build Docker Image \"${{ parameters.dockerImageName }}\"."
inputs:
repository: myrepo/${{ parameters.dockerImageName }}
containerRegistry: harbor_docker
command: build
Dockerfile: ${{ parameters.dockerfilePath }}
addPipelineData: false
arguments: --build-arg PAT=$(System.AccessToken)
tags: |
${{parameters.tag}}
latest
And the call i have from my azure-pipeline looks like this:
repositories:
- repository: templates
type: git
name: ADO-pipeline-templates
ref: 0223b9311140ff6128fe40625ba2b4b252d82a73
variables:
tag: '$(Build.SourceVersion)'
imageName: dummy
isMain: $[eq(variables['Build.SourceBranch'], 'refs/heads/main')]
shorttag: $(git rev-parse --short=8 $tag)
stages:
- stage: PushToHarbor
displayName: Push to Harbor
jobs:
- template: push-to-harbor.yaml@templates
parameters:
dockerfilePath: $(Build.SourcesDirectory)/Dockerfile
dockerImageName: $(imageName)
tag: $(shorttag)
pushAsLatest: $(isMain)
I have found that if i use $(tag) it works, but using $(shorttag) it fails with:
ERROR: invalid tag "registry.mydomain.com/myrepo/dummy:$(git rev-parse --short=8 $tag)": invalid reference format
Looking in microsoft docs, it seems it is because using ${{ }} syntax is evaluated at compile time, and the value of $tag is actually known here while the value of $shorttag is not.
So, calling the changing the template to use:
tags: |
$[parameters.tag]
which should be processed at runtime.
Now the result is:
ERROR: invalid tag "registry.mydomain.com/myrepo/dummy:$[parameters.tag]": invalid reference format
So my question is how can I parse an argument to a template that is evaluated at runtime?
EDIT: This is not a duplicate of Azure Pipelines: Passing a variable as a parameter to a template, as the accepted answer assumes a static value, and in the end is evaluated at compile time (using ${{ }} syntax). I am looking for how to correctly evaluate at runtime