4

I would like to have if condition under parameters in Azure pipeline yml file. Please find my code below:

I want to show iPhone 13, iPhone 13 Pro, iPhone SE if product is selected as phone. And show Macbook Pro, Macbook Air if product is selected as computer and so on. But I'm getting an below error if I try to add the conditional statement inside parameters

Pipeline Error:

Encountered error(s) while parsing pipeline YAML:
/azure-pipeline.yml (Line: 17, Col: 5): A template expression is not allowed in this context
/azure-pipeline.yml (Line: 21, Col: 5): A template expression is not allowed in this context

Pipeline code:

trigger:
  - none

parameters:
  - name: product
    displayName: Select Product
    type: string
    default: phone
    values:
      - phone
      - computer
      - watch

  - name: selectProduct
    displayName: Select Product
    type: string
    ${{ if eq(parameters.product, 'phone') }}:
      values:
        - iPhone 13
        - iPhone 13 Pro
        - iPhone SE
    ${{ if eq(parameters.product, 'computer') }}:
      values:
        - Macbook Pro
        - Macbook Air
    ${{ if eq(parameters.product, 'watch') }}:
      values:
        - Apple Watch Series 7
        - Apple Watch SE

Can someone please suggest how can I handle this in azure pipeline, incase if condition is not supported under parameters section - what's the alternative approach.

Any help would he much appreciated. Thanks!

Note: I want to show this option before pipeline job trigger/submit. This options should pop up when I click Run Pipeline button.

1
  • Same question! I would like to render new fields based on user inputs from parameters Commented Jul 9, 2022 at 19:47

1 Answer 1

2

how to specify conditional expression under parameters seciton - Azure DevOps Pipeline

I am afraid it it impossible to runtime parameters conditionally define values based on another parameter value.

You could check the document Template expressions:

Template expressions can expand template parameters, and also variables. You can use parameters to influence how a template is expanded. The parameters object works like the variables object in an expression. Only predefined variables can be used in template expressions.

Expressions are only expanded for stages, jobs, steps, and containers (inside resources). You cannot, for example, use an expression inside trigger or a resource like repositories. Additionally, on Azure DevOps 2020 RTW, you can't use template expressions inside containers.

As workaround, we could try to use condition in the stages, jobs, steps with template, like:

parameters:
  - name: product
    displayName: Select Product
    type: string
    default: phone
    values:
      - phone
      - computer
      - watch

stages:
  - ${{ if eq(parameters.product, 'phone') }}:
    - template: template.yml
      parameters:
        selectProduct:
        - iPhone 13
        - iPhone 13 Pro
        - iPhone SE
  - ${{ if eq(parameters.product, 'computer') }}:
    - template: template.yml 
      parameters:
        selectProduct:
        - Macbook Pro
        - Macbook Air
Sign up to request clarification or add additional context in comments.

1 Comment

In your example Leo, what is referring to "template.yml" ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.