Search code examples
azureazure-devopsyamlazure-pipelines

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.


Solution

  • 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"