Vergil C.
Vergil C.

Reputation: 1104

How can I reference a parameter from inside a parameterized stepList in Azure Devops Yaml Pipelines?

This is the parameters part of my template used from other templates/pipelines:

    parameters:  
    - name: artifactName
      displayName: Name of the app artifact published to the pipeline
      type: string

Parameter preBuildSteps does not exist currently, I am trying to make a parameter of type stepList with a default value. This helps me introduce the ability to have pre-build step list as parameter but at the same time avoid breaking changes for all pipelines/templates using this template currently. And the displayed below is where I want to get to.

    parameters:  
    - name: artifactName
      displayName: Name of the app artifact published to the pipeline
      type: string  
    - name: preBuildSteps
      displayName: 'Pre-Build Steps'
      type: stepList
      default:
      - task: DownloadPipelineArtifact@2
        displayName: Download app artifact
        inputs:
          artifact: '${{parameters.artifactName}}'
          path:  '$(Build.ArtifactStagingDirectory)/app'

The problem is artifact: '${{parameters.artifactName}}' as I am trying to reference a parameter from inside a parameterized step list. The error I get trying to run the pipeline using the template is A template expression is not allowed in this context.

I have tried without single quotes ${{parameters.artifactName}} and I have tried with braces like this $(parameters.artifactName). I have also tried to create a variable assigning its value from the parameter artifactName and then reference that variable in the parameterized section to no avail.

Upvotes: 2

Views: 5919

Answers (1)

WaitingForGuacamole
WaitingForGuacamole

Reputation: 4301

I don't think you can do this directly, but you could work around it, by doing the following:

  1. In the job where you wish to use the stepList parameter, add
variables:
- artifact: ${{ parameters.artifactName }}

before the steps in that job (where you would expand out your preBuildSteps parameter value)

  1. In the stepList parameter default, reference that job-local variable:
      default:
      - task: DownloadPipelineArtifact@2
        displayName: Download app artifact
        inputs:
          artifact: '$(artifact)'
          path:  '$(Build.ArtifactStagingDirectory)/app'

It's conceivable you could also use ${{ variables.artifactName }} in that reference as well, which would expand out the value at compile time - just not 100% sure the reference would be expanded at that point. Worth a try.

Upvotes: 2

Related Questions