Search code examples
azure-devopsazure-pipelines-yaml

Azure Devops yml pipeline if else condition with variables


I am trying to use if else conditions in Azure Devops yml pipeline with variable groups. I am trying to implement it as per latest Azure Devops yaml pipeline build.

Following is the sample code for the if else condition in my scenario. test is a variable inside my-global variable group.

variables:
  - group: my-global
  - name: fileName
    ${{ if eq(variables['test'], 'true') }}:
      value: 'product.js'
    ${{ elseif eq(variables['test'], false) }}:
      value: 'productCost.js'

jobs:
  - job:
    steps:
      - bash:
          echo test variable value $(fileName)

When the above code is executed, in echo statement we don't see any value for filename, i.e. it empty, meaning none of the above if else condition was executed, however when I test the if else condition with the following condition.

  - name: fileName
    ${{ if eq('true', 'true') }}:
       value: 'product.js'

Filename did echo the correct value, i.e. product.js. So my conclusion is that I am not able to refer the variables from the variable group correctly. So any suggestion will be helpful and appreciated. Thanks!


Solution

  • After detailed investigation I realized that if else doesnt work with variables in Az Devop yaml pipelines, it only works with parameters. However the solution posted by @Tejas Nagchandi is a workaround and might be able to accomplish the same logic of if else setting variable value with replace commands. Hats off to TN.