skip to Main Content

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


  1. I can reproduce your problem.
    I can partly solve it by adding the mandatory type to the parameter in the template:

    parameters:
    - name: TF_VERSION
      type: number
    
    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"
    
    

    demo2

    The only down side seems to be that I’m not able to pass a variable from the calling YAML due to this error.

    not a number

    This issue can be overcome by setting the number directly in the parameter like this:

    trigger:
    - main
        
    pool:
      vmImage: ubuntu-latest
    
    resources:
      repositories:
        - repository: templates
          type: git
          name: YAML-Templates      
          ref: refs/heads/main
    
    steps:
    - template: if/if.yml@templates
      parameters:    
        TF_VERSION: 1.5
    

    Like you mentioned, 1.3.3, a version number, is still not a number.

    Alternative

    Alternatively you could go for startsWith like so:

    
    parameters:
    - name: TF_VERSION
    
    steps:
    - ${{ if startsWith(parameters.TF_VERSION, '0.') }}:
      - powershell: Write-Host "I'm running 0.x"  
    - ${{ if startsWith(parameters.TF_VERSION, '1.') }}:
      - powershell: Write-Host "I'm running 1.x"
    
    

    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.

    Login or Signup to reply.
  2. I have tried only to change the TF_VERSION: $(TF_VERSION) to TF_VERSION: ${{ variables.TF_VERSION }} in the main yaml.

    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: ${{ variables.TF_VERSION }}
    

    Then run the pipeline with the template it seems work and get this:
    enter image description here

    Please kindly try whether it works on your side. And hope it could do some help:>

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search