user15338350
user15338350

Reputation: 293

Is it possible to add runtime parameters based on condition - Azure Devops Pipeline

My current azure pipeline looks like following -

parameters:
  - name: Deploy
    type: Boolean
    default: false
  - name: Stages
    type: string
    values:
       - Stg A
       - Stg B
       - Stg C
       - Stg D
       - Stg E

I was trying to add a condition in such a way that if user checks Deploy on running pipeline, it should dynamically show up only Stg A, Stg B as values for Stages. And if they uncheck Deploy, it should show Stg C, Stg D and Stg E.

I tried to add conditions in following way -

 parameters:
  - name: Deploy
    type: Boolean
    default: false
  - name: Stages
    type: string
    ${{ if eq(parameters['Deploy'], 'true' ) }}:
     values: 
       - Stg A
       - Stg B
    ${{ if ne(parameters['Deploy'], 'true' ) }}:
     values:
       - Stg C
       - Stg D
       - Stg E

However, the following conditional way worked for variables, but did not work for parameters. Is there any way to achieve this in Azure pipelines.

Upvotes: 18

Views: 37823

Answers (2)

ikhvjs
ikhvjs

Reputation: 5977

No. However, you can pass parameters with conditional template as below.

parameters:
  - name: Deploy
    type: Boolean
    default: false

stages:
  - template: template.yml
    parameters:
        ${{ if eq(parameters['Deploy'], 'true' ) }}:
          stages:
            - "Stg A"
            - "Stg B"
        ${{ else }}:
          stages:
            - "Stg C"
            - "Stg D"
            - "Stg E"

Upvotes: 1

Leo Liu
Leo Liu

Reputation: 76928

Is it possible to add runtime parameters based on condition - Azure Devops Pipeline

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

Because the parameters need to be defined before the pipeline run starts. As stated in the document Runtime parameters

Parameters are only available at template parsing time. Parameters are expanded just before the pipeline runs so that values surrounded by ${{ }} are replaced with parameter values. Use variables if you need your values to be more widely available during your pipeline run.

As workaround, we could use condition in the steps:

 parameters:
  - name: Deploy
    type: Boolean
    default: false

stages:
  - ${{ if eq(parameters['Deploy'], 'true' ) }}:
    - template: template.yml
      parameters:
        stages:
          - "Stg A"
          - "Stg B"
  - ${{ if ne(parameters['Deploy'], 'true' ) }}:
    - template: template.yml 
      parameters:
        stages:
          - "Stg C"
          - "Stg D"
          - "Stg E"

Upvotes: 17

Related Questions