How to write if else condition in Azure DevOps Pipeline

Viewed 8766

I have an azure pipeline and run with parameters where I've multiple options like below:

Select Test Execution

  • Product
  • Product with Cost
  • Product with Attachments

If I select Product then I execute product.js file, if I select Product with Cost then execute "productCost.js" and so on. I've written a azure pipeline script to do this. I've another condition "Generate Test Data" checkbox which returns boolean value true or false if the value is true then I've to select a file productWithTestData.js if Product is selected - I don't know how to write if else condition in Azure pipeline code. Please find my pseudo code

Can someone please help me how to write if else condition for my use case - appreciated your help in advance! Thanks!

variables:
  - name: fileName
    ${{ if eq(parameters.selectTestRun, 'Product') }}:
      value: 'product.js'
    else ${{ if eq(parameters.selectTestRun, 'Product') } && { if eq(parameters.testData, True) }}:   
      value: 'productWithTestData.js'

My Pipeline code:

trigger:
  none

parameters:

- name: selectTestRun
  displayName: Select test execution
  type: string
  default: Product
  values:
  - Product
  - Product with Cost
  - Product with Attachments

- name: testData
  displayName: Generate Test Data
  type: boolean
  default: false

variables:
  - name: fileName
    ${{ if eq(parameters.selectTestRun, 'Product') }}:
      value: 'product.js'
    ${{ if eq(parameters.selectTestRun, 'Product with Cost') }}:
      value: 'productCost.js'
    ${{ if eq(parameters.selectTestRun, 'Product with Attachments') }}:
      value: 'productAttachment.js'      
  
jobs:
  - job: testJob
    pool:
      vmImage: ubuntu-latest
    displayName: Run sample tests
    steps:
      - task: Bash@3
        displayName: Test variable
        inputs:
          targetType: inline
          script: |
            echo "Hello world"
            echo ${{variables.fileName}}
                
2 Answers

UPDATE: 09/09/2021

Now we have also if else condition available:

variables:
  ${{ if eq(parameters.os, 'win') }}:
    testsFolder: windows
  ${{ elseif eq(parameters.os, 'linux' }}:
    testsFolder: linux
  ${{ else }}:
    testsFolder: mac

so we could write

variables:
  - name: fileName
    ${{ if eq(parameters.selectTestRun, 'Product') }}:
      value: 'product.js'
    ${{ elseif eq(parameters.selectTestRun, 'Product with Cost') }}:
      value: 'productCost.js'
    ${{ elseif eq(parameters.selectTestRun, 'Product with Attachments') }}:
      value: 'productAttachment.js'   
    ${{ else }}:
      value: 'something-else.js' 

Original reply

You should use notIn expression in this case:

variables:
  - name: fileName
    ${{ if eq(parameters.selectTestRun, 'Product') }}:
      value: 'product.js'
    ${{ if eq(parameters.selectTestRun, 'Product with Cost') }}:
      value: 'productCost.js'
    ${{ if eq(parameters.selectTestRun, 'Product with Attachments') }}:
      value: 'productAttachment.js'   
    ${{ if notIn(parameters.selectTestRun, 'Product', 'Product with Cost', 'Product with Attachments') }}:
      value: 'something-else.js' 

in this case you need to repeat this each time like follows:

 ${{ if and(eq(parameters.selectTestRun, 'Product'), ne(parameters.testData, True)) }}:
      value: 'product.js'
 ${{ if and(eq(parameters.selectTestRun, 'Product'),eq(parameters.testData, True)) }}:
      value: 'productWithTestData.js'

And the same for the other if's.

There is no else. If you're testing something for equality, the "else" would be to test for inequality:

${{ if eq(parameters.selectTestRun, 'Product') }}:
  value: 'product.js'
${{ if ne(parameters.selectTestRun, 'Product') }}:
  value: 'something else'
Related