I am having an issue with the variables in Azure DevOps pipelines. I can not effectively use them in an if statement or as a placeholder in a value argument.
This is a related question but the answer does not work for me.
I have defined a variable like this in the top level pipeline .yml file.
variables:
- name: artifact_source
readonly: true
value: 'ecu'
- name: artifact_getter
readonly: true
value: 'get_artifacts_ecu.yaml'
And in a lower level template file i want to use them like this:
# Get binaries
- template: $(artifact_getter)
parameters:
variant: ${{ parameters.variant }}
Gives me this error: File /.azuredevops/template/$(artifact_getter) not found in repository.
or
jobs:
- job: ${{ parameters.variant }}
pool: ${{ parameters.pool }}
steps:
# Clean checkout
- checkout: self
clean: true
# Get binaries
- ${{ if eq( $(artifact_source), 'ecu') }}:
- template: get_artifacts_ecu.yml
parameters:
variant: ${{ parameters.variant }}
This gives an error: Unrecognized value: '$'. Located at position 5 within expression: eq( $(artifact_source), 'ecu'). For more help, refer to https://go.microsoft.com/fwlink/?linkid=842996
I am wondering about what format i should use to get the value of the variable here. I see different forms how to reference variables:
${{ variables.artifact_source }}
$[variables.var]
$env:artifact_source
$(artifact_source) # only one working for variables
Also documented here. But i can only get $(name)
to work, but not in my case.
How can i use variables to switch to a different template?
In the Expressions document, it mentions as part of an expression, you may access variables using one of two syntaxes:
variables['MyVar']
variables.MyVar
So, you can use eq(variables['artifact_source'], 'ecu')
or eq(variables.artifact_source, 'ecu')
here.
# Get binaries
- ${{ if eq(variables['artifact_source'], 'ecu') }}:
- template: get_artifacts_ecu.yml
parameters:
variant: ${{ parameters.variant }}
or
# Get binaries
- ${{ if eq(variables.artifact_source, 'ecu') }}:
- template: get_artifacts_ecu.yml
parameters:
variant: ${{ parameters.variant }}