azure-devopsazure-pipelinesazure-pipelines-yaml

Select specific task in Azure DevOps UI pipeline run


I try to edit my pipeline to have an multi-select option for tasks in Azure DevOps UI. I created template to install ansible playbook:

parameters:
  - name: Path
    type: string
    default: ''

  - name: extraVars
    type: string
    default: ''

  - name: targetDir
    type: string
    default: ''

  - name: taskTag
    type: string
    default: 'all'

steps:
  - task: Bash@3
    displayName: 'Playbook'
    inputs:
      targetType: 'inline'
      script: |
        cd ${{ parameters.targetDir }}
        ansible-playbook \
          ${{ parameters.Path }} \
          --extra-vars '{"user_prompt": {"user_input": "${{ parameters.extraVars }}"}}' \
          --tags "${{ parameters.taskTag }}" \

I edited my main pipeline like that:

# Rest of the code
  - name: taskTag
    displayName: 'Select Task'
    type: string
    default: 'all'
    values:
      - all
      - prechecks
      - user_create
      - group_create
      - security_set

Now when I start my pipeline I am able to choose only one specific tasks based on tasks from the ansible playbook. Is it possible to change that list to multi-select list?


Solution

  • You can:

    Use an array parameter

    Changing the taskTag parameter to type: object allows you to set the default value to a string array containing all tasks that can be deployed:

    parameters:
      - name: taskTag
        displayName: 'Select Task'
        type: object
        default: 
          - prechecks
          - user_create
          - group_create
          - security_set
    
    steps:
      - checkout: none
    
      - ${{ each taskTag in parameters.taskTag }}:
        - script: echo ${{ taskTag }}
          displayName: 'Run ${{ taskTag }}'
    

    When queuing a new build all options will be available:

    Queue new build

    Users can choose which tasks to deploy by removing elements in the array:

    Queuing build - removing options

    The downside of this approach is that users can edit the field and put invalid options or an invalid YAML array, which can break the build.

    Use boolean parameters

    Use a boolean parameter for each task:

    parameters:
      - name: deployPrechecks
        displayName: 'Deploy Prechecks task'
        type: boolean
        default: true
      
      - name: deployUserCreate
        displayName: 'Deploy User Create task'
        type: boolean
        default: true
      
      - name: deployGroupCreate
        displayName: 'Deploy Group Create task'
        type: boolean
        default: true
      
      - name: deploySecuritySet
        displayName: 'Deploy Security Set task'
        type: boolean
        default: true
    
    steps:
      # ...
      - template: templates/run-playbook-steps.yaml
        parameters:
          playbooks: 
            - ${{ if parameters.deployPrechecks }}: 
                - prechecks
            - ${{ if parameters.deployUserCreate }}:
                - user_create
            - ${{ if parameters.deployGroupCreate }}:
                - group_create
            - ${{ if parameters.deploySecuritySet }}:
                - security_set
    

    templates/run-playbook-steps.yaml:

    parameters:
      - name: playbooks
        displayName: 'Playbooks'
        type: object
        default: []
    
    steps:
      - ${{ each playbook in parameters.playbooks }}:
        - script: |
            echo Running playbook ${{ playbook }}
          displayName: 'Run playbook: ${{ playbook }}'
    

    Queuing a new pipeline:

    Queue new build with boolean parameters