Home > Back-end >  Why can I not pass a runtime variable in if condition in azure devops yaml template
Why can I not pass a runtime variable in if condition in azure devops yaml template

Time:06-03

I think I'm calling my variable os_name from the powershell step incorrectly; each time I test the build it doesn't pass through my if statement. I'm wondering what's wrong with my code. Has anyone tried using runtime variables in azure devops expression similar to this?

      - powershell: |
          $os_name = 'linux'
          echo "##vso[task.setvariable variable=os_name;]$os_name"

      - ${{ if eq( variables.os_name , 'linux') }}:
        - powershell: |
            echo "variables.os_name = linux"

CodePudding user response:

This should work for you:

- task: PowerShell@2
  displayName: set variable
  inputs:
    targetType: 'inline'
    script: |
      "##vso[task.setvariable variable=os_name;]linux"

- task: PowerShell@2
  displayName: Linux case
  inputs:
    targetType: 'inline'
    script: |
        Write-Host "Linux"
  condition: eq(variables.os_name, 'linux')

Run for Linux:

enter image description here

Run for non Linux:

enter image description here

  • Related