3

For often used tasks in azp I created an own repository with a yml file, I'll show you a subpart of that: create-and-upload-docu.yml:

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

- name: Language
  type: string
  default: ''

- name: ArchiveBaseDir
  type: string
  default: ''

steps:
- script: |
    ARCHIVERELPATH=${{parameters.Documentation}}-${{parameters.Language}}.zip
    ARCHIVEDIR=$(echo -n ${{parameters.ArchiveBaseDir}} | sed -e 's@/$@@')/${{parameters.Documentation}}/${{parameters.Language}}
    echo "##vso[task.setvariable variable=archiveRelPath;isOutput=true]$ARCHIVERELPATH"
    echo "##vso[task.setvariable variable=archiveDir;isOutput=true]$ARCHIVEDIR"
  name: ${{parameters.Documentation}}_${{parameters.Language}}_params

- task: DeleteFiles@1
  inputs:
    Contents: '$(Build.ArtifactStagingDirectory)/$(${{parameters.Documentation}}_${{parameters.Language}}_params.archiveRelPath)'

The relevant part is: the "script" has the name which is unique in a job - so I can use this kind of expansion for setting variables within the template:

$(${{parameters.Documentation}}_${{parameters.Language}}_params.archiveRelPath)

This works fine as long as I had called the template with fixed values, like

- template: create-and-upload-docu.yml@templates
          parameters:
            Documentation: 'adocuvalue'
            Language: 'en_US'
            ArchiveBaseDir: '$(Build.ArtifactStagingDirectory)/build/'

But now I want to use a matrix to have a few documentations with a few languages:

jobs:
  - job: Documentation_CI
    displayName: "Docu CI"
    timeoutInMinutes: 30
    strategy:
      matrix:
        main_en_US:
          Documentation: main
          Language: en_US
        main_de_AT:
          Documentation: main
          Language: de_AT
    steps:
      - checkout: self
      - template: create-and-upload-docu.yml@templates
          parameters:
            Documentation: ${{variables.Documentation}}
            Language: ${{variables.Language}}
            ArchiveBaseDir: '$(Ws)/build/'

But at the time where ${{}} expressions are expanded, it seems that the matrix variables are not already set; this means that the template script part is called __params and the pipeline has the following error Publishing build artifacts failed with an error: Input required: ArtifactName

Is there a somewhat simple way to achive what I want (being able to set some variables within templates with a unique naming schema):

  • can I somehow use ${{ expressions but need a different naming to get to the hard-coded matrix style variables
  • can I workaround my problem any simple way?

Additional Info: we run a Azure 2020 on prem.

1 Answers1

2

Is there a somewhat simple way to achive what I want (being able to set some variables within templates with a unique naming schema):

Sorry for any inconvenience.

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

Just as you test, the syntax ${{}} is parsed at compile time. We could not get the value when we use it as name or display name in the task, since it will be parsed at compile time. But the matrix variables have not been set during compilation. That the reason why we get the value _params.

There is a feature request about this. And you could add your request for this feature on our UserVoice site (https://developercommunity.visualstudio.com/content/idea/post.html?space=21 ), which is our main forum for product suggestions:

enter image description here

Leo Liu
  • 71,098
  • 10
  • 114
  • 135
  • thanks for the clarification. Basically I had to copy-paste the template tasks into the azure-pipelines.yml which is very ugly, but at least it works now. – Klemens Altmanninger May 05 '21 at 12:17