18

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.

0

2 Answers 2

17

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"
Sign up to request clarification or add additional context in comments.

1 Comment

Please check my answer for an alternative.
2

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"

1 Comment

For me this was better that the selected answer above. Conditions for just the specific parameters that needs to be changes instead of the whole template is more clean.

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.