Checking a version number in a yaml pipeline with an if statement and wanting to run a particular task dependent on version. Works fine directly but I’m using templates and passing the value through it doesn’t seem to work.
This works:
trigger:
- main
pool:
vmImage: ubuntu-latest
parameters:
- name: TF_VERSION
default: 1.3.3
steps:
- ${{ if lt(parameters.TF_VERSION, 1) }}:
- powershell: Write-Host "I'm running lt 1.0"
- ${{ if ge(parameters.TF_VERSION, 1) }}:
- powershell: Write-Host "I'm running ge 1.0"
This doesn’t with the first condition always returning true:
trigger:
- main
pool:
vmImage: ubuntu-latest
resources:
repositories:
- repository: templates
type: git
name: YAML-Templates
ref: refs/heads/main
variables:
- name: TF_VERSION
value: 1.3.3
steps:
- template: if/if.yml@templates
parameters:
TF_VERSION: $(TF_VERSION)
template:
parameters:
- name: TF_VERSION
steps:
- ${{ if lt(parameters.TF_VERSION, 1) }}:
- powershell: Write-Host "I'm running lt 1.0"
- ${{ if ge(parameters.TF_VERSION, 1) }}:
- powershell: Write-Host "I'm running ge 1.0"
Anyone know why???
2
Answers
I can reproduce your problem.
I can partly solve it by adding the mandatory type to the parameter in the template:
The only down side seems to be that I’m not able to pass a variable from the calling YAML due to this error.
This issue can be overcome by setting the number directly in the parameter like this:
Like you mentioned, 1.3.3, a version number, is still not a number.
Alternative
Alternatively you could go for startsWith like so:
I hope this answers the question, why it is not working and what a possible alternative is.
Edit-2
The solution of Antonia Wu-MSFT’s also work, the why is still a bit unclear. But I think it has to do with: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/variables?view=azure-devops&tabs=yaml%2Cbatch#template-expression-syntax
But to be honest, still don’t understand why a type is mandatory according to the documentation, but not used in this case.
I have tried only to change the
TF_VERSION: $(TF_VERSION)
toTF_VERSION: ${{ variables.TF_VERSION }}
in the main yaml.Then run the pipeline with the template it seems work and get this:
Please kindly try whether it works on your side. And hope it could do some help:>