Azure Devops Pipeline: Second parameter dependent on the first parameter
Asked Answered
C

2

9

Is it possible in azure devops that the choice of the first parameter by the user, determines the second parameter (type, displayName etc.)?

For example:

parameters:
- name: parametr1
  displayName: example1
  type: string
  default: first
  values:
  - first
  - second
  - third

And if the user selects "first" when starting the pipeline, the second parameter to enter:

- name: parametr2.1
  displayName: example2
  type: number

But if the user selects "second" when starting the pipeline, the second parameter to enter:

- name: parametr2.2
      displayName: example2.2
      type: boolean

   

Thanks for any help :)

Cohosh answered 28/9, 2020 at 21:55 Comment(3)
i could be wrong here, but where exactly is the user supposed to enter and provide input in YAML. are you executing a power shell script or something, before a pipeline is triggered? Or, are the values being set in the project before pipeline beings its work? I am pretty sure YAML is not an interactive thing.Dimitry
When I trigger the pipeline manually via the "Run Pipeline" button. A window pops up: "Run pipeline Select parameters below and manually run the pipeline" and there I can select and enter parametersCohosh
wow. alright. you know, thank you very much. i never used these advanced features but got to know because of you - learn.microsoft.com/en-us/azure/devops/pipelines/process/…Dimitry
P
0

Azure Devops Pipeline: Second parameter dependent on the first parameter

I am afraid there is no such out of way to resolve this question at this moment.

That because the conditions does not support for parameters at present. We could not add condition to the second parameter to set the value based on the first parameter.

As we know, the Runtime parameters is used to let you have more control over what values can be passed to a pipeline.

Since the second parameter is depend on the first parameter, does not require us to manually control it.

So, as workaround for this question, we could use the Logging Command with condition to set the second parameter based on the value of first parameter:

parameters:
- name: parametr1
  displayName: example1
  type: string
  default: first
  values:
  - first
  - second
  - third

trigger: none

jobs:
- job: build
  displayName: build
  pool: 
    name: MyPrivateAgent

  steps:
  - task: InlinePowershell@1
    displayName: 'SetVariableV1'
    inputs:
      Script: 'Write-Host "##vso[task.setvariable variable=parametr2.1;]123456"'
    condition: and(succeeded(), eq('${{ parameters.parametr1 }}', 'first'))

  - task: InlinePowershell@1
    displayName: 'SetVariableV2'
    inputs:
      Script: 'Write-Host "##vso[task.setvariable variable=parametr2.2;]true"'
    condition: and(succeeded(), eq('${{ parameters.parametr1 }}', 'second'))

I test it on my side, it works fine.

Posthorse answered 29/9, 2020 at 7:38 Comment(2)
Thanks for the nice answer. Unfortunately, my goal is that the first parameter changes the type and displayName of the second accordingly. To facilitate the work of dovelopers who can type the second parameter but with a specific type. So I suspected it couldn't be done dynamically.Cohosh
@Rozmaryn, Yes, you are right. It couldn't be done dynamically at this moment. You could submit this user voice to our main forum for product suggestions: developercommunity.visualstudio.com/content/idea/…Posthorse
S
0

Variables can be used here like this:

parameters:
  - name: name
    type: string
    values:
      - name1
      - name2

variables:
  - ${{ if eq(parameters.name, 'name1') }}:
      - name: var1
        value: '111'
      - name: var2
        value: 'string1'

  - ${{ if eq(parameters.name, 'name2') }}:
      - name: var1
        value: '222'
      - name: var2
        value: 'string2'
Santamaria answered 7/6, 2023 at 10:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.