Home > front end >  Why i cant pass runtime variable in if condition in azure devops yaml template
Why i cant pass runtime variable in if condition in azure devops yaml template

Time:06-02

I think im calling my variable os_name from the powershell step incorrect, each time i test the build it doesnt pass through my if statement. im wondering whats wrong with my code. have 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