Muneeb
Muneeb

Reputation: 191

Is there a way to dynamically assign names to tasks in yaml pipelines for Azure Devops?

I am using the DownloadPipelineArtifact@2 task and looping over an array of pipelines to download the artifacts for each of them. I would like to use the $(BuildNumber) output of each of these tasks in subsequent tasks.

${{ each p in pipelines }}:
  - task: DownloadPipelineArtifact@2
    displayName: 'Download ${{ p.alias }} Artifact - ${{ p.artifact }}'
    name: 'Download_${{ p.alias }}'
    inputs:
      buildType: specific
      project: $(System.TeamProjectId)
      pipeline: ${{ p.id }}
      preferTriggeringPipeline: false
      runVersion: latestFromBranch
      runBranch: 'refs/heads/${{ p.branch }}'
      artifact: ${{ p.artifact }}
      patterns: ${{ p.patterns }}
      targetPath: '$(Pipeline.Workspace)/artifacts/${{ p.alias }}/${{ p.artifact }}'

Unfortunately, I am unable to do so as I would need to refer to the task by it's name and I am not able to assign dynamic names to the tasks. I get an error saying

Valid names may only contain alphanumeric characters and '_' and may not start with a number.

I tried using runtime variables, expressions and a counter for the name but all fail with the same error. I am assuming here the name field is not dynamically assignable.

All works well without the name field. But from what I understand I would need the name of the task to refer to it and extract it's output. Something like:

{{ taskName.BuildNumber }}

If anyone knows of any way in which I can assign the name dynamically or through which I can extract the $(BuildNumber) for each task in the loop, please let me know. Thanks!

Upvotes: 2

Views: 2491

Answers (2)

YoniXw
YoniXw

Reputation: 585

Looks like you can do it, this guy has it working 1 ... as long as the array of iteration is known at the YAML compile time.

Here what was working for me (Had to santize the name to get /A-z0-9_/):

        - ${{ each environmentFull in parameters.environmentFullList }}:
            - bash:  |
                echo "##vso[task.setvariable variable=environment]$(echo ${{ environmentFull }} | cut -d. -f1)"
                echo "##vso[task.setvariable variable=namespace]$(echo ${{ environmentFull }} | cut -d. -f2)"
              failOnStderr: true
              displayName: Important pipeline variables

            - bash:  |
                echo "Namespace: $(namespace)"
                echo "Environment: $(environment)"
                echo "Task name: kubectl_${{ replace(replace(environmentFull,'.','_'),'-','_') }}"
              failOnStderr: true
              displayName: Print info
              
            - task: Kubernetes@1
              name: kubectl_${{ replace(replace(environmentFull,'.','_'),'-','_') }}
              inputs:
                connectionType: 'Kubernetes Service Connection'
                kubernetesServiceEndpoint: '${{ environmentFull }}'
                namespace: '$(namespace)'
                command: 'get'
                arguments: 'pods'
                secretType: 'dockerRegistry'
                containerRegistryType: 'Azure Container Registry'
                outputFormat: 'jsonpath=''{range .items[*].status.containerStatuses}{@}{end}'''
              displayName: "Get all pods"

            - bash:  |
                export TASKNAME_LC="kubectl_${{ replace(replace(environmentFull,'.','_'),'-','_') }}_KubectlOutput"
                export TASKNAME_UC=${TASKNAME_LC^^}
                echo "====Using name: $TASKNAME_UC"
                export TASK_VALUE=${!TASKNAME_UC}
                echo "$TASK_VALUE"

Image of worked run: Image of worked

1) Azure Pipeline dynamic parameters to template file from YAML pipeline

Upvotes: 0

Leo Liu
Leo Liu

Reputation: 76700

Is there a way to dynamically assign names to tasks in yaml pipelines for Azure Devops?

I am afraid there is no such way to dynamically assign names to tasks in yaml pipelines for Azure Devops Because the task itself does not contain the Name field, the Nameof the downloaded artifact is determined by the name of the artifact generated during building. This task only needs to download the artifact, so this task does not include the field to rewrite the name. When we use any variable in the value of this field, the compiler will parse this field, then it will throw us the error mentioned in your question:

enter image description here

enter image description here

Upvotes: 1

Related Questions