8

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}}
                
Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
Datadog Learning
  • 105
  • 1
  • 1
  • 5

2 Answers2

19

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.

Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
  • I've three different scenarios if the user selected Product and user checked on Generate Test Data then the boolean value is True and if condition return the value product.js Or if Product with Cost and True then value should be productCostWithData.js Or if Product with Attachments' and True then value should be productAttachmentWithData.js - hope I'm clear with my use case – Datadog Learning Jul 12 '21 at 19:11
  • Thank you! I'm getting below error after making your change in pipeline :( Encountered error(s) while parsing pipeline YAML: /azure-pipeline.yml (Line: 24, Col: 5): Exactly 1 parameter(s) were expected following the directive 'if'. Actual parameter count: 4 – Datadog Learning Jul 12 '21 at 19:37
  • 1
    Yeah. Sorry I used wrong syntax. Now it should be fine. – Krzysztof Madej Jul 12 '21 at 19:56
  • 1
    @KrzysztofMadej I am trying to implement this exact same functionality, but when I use the syntax above underneath Original Reply, the value: gets underlined with a squiggly line and the popup is "Duplicate Key". Any suggestions on this issue? I copied the above code and just pasted it to make sure I have the right syntax, so I'm not sure why it's not working. – Mike Oct 15 '21 at 23:15
  • You can ignore that message. The tool used for validation doesn't recognized these expressions. But it works. – Krzysztof Madej Oct 17 '21 at 18:33
  • Indeed it does...what does that say about me as a developer that I didn't try to run it even with validation errors haha. Thanks so much for the help. – Mike Oct 17 '21 at 21:15
  • @Mike for me it says that you believe that tool is consistent in behavior and messages it produces. What's the point of running sth if you have clear message that c'mon it will not work. IMHO the issue is on IDE side, after all we should believe in tools/libs we use otherwise we will never finish anything. – Krzysztof Madej Oct 18 '21 at 07:59
  • I only wished above worked for parameters ... today it only works for variables however azure templates cannot define variables can only define parameters – Scott Stensland Dec 08 '21 at 17:16
  • Well, I use if-else expression for template parameters. So if you have specific issue please post and issue and provide a link here, so I will take a look. – Krzysztof Madej Dec 08 '21 at 17:26
0

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'
Daniel Mann
  • 57,011
  • 13
  • 100
  • 120
  • But this won't work well with my case because I've to select Product and pick the filename based on different parameter conditions Generate Test Data is True or False.. I've to check both Product and True then have value productWithTestData.js or else product.js – Datadog Learning Jul 12 '21 at 18:51