0
votes

I am working with Azure pipeline templates. I would like the developer that kicks off a pipeline to either set a variable of a specific branch OR leave as the $(Build.SourceBranch)

The reason is to pull down artifacts from different repositories/branches to combine.

So on my yml I added parameters (only showing 1 for simplicity)

parameters:
- name : source_branch
  displayName: Which Branch (e.g. refs/head/foo)
  type: string
  default: $(Build.SourceBranch)

Then I call a template

- template: download_artifact.yml
  parameters:
    artifacts:
    - project: 'XXX'
      pipeline: 291
      artifact: 'artifcat'
      branch: ${{ parameters.source_branch }}
      

I use a template as there are approx 30 different artifacts to combine.

Within the template it downloads extracts and manipulates but I will simplify to only download.

parameters:  
  artifacts: []
steps:
  - ${{ each step in parameters.artifacts }}:
    - task: DownloadPipelineArtifact@2
      displayName: '${{ step.artifact }}'
      inputs:
        source: 'specific'
        project: ${{step.project}}
        pipeline: ${{step.pipeline}}
        runVersion: 'latestFromBranch'       
        runBranch: ${{step.branch}}
        artifact: ${{step.artifact}}           
        path: '$(Pipeline.Workspace)\${{ step.artifact }}'

So the end result is that the variable does not get resolved within the template. I think this is due to templates being expanded at queue time. Does anyone have any workarounds for this scenario?

2

2 Answers

0
votes

Your reference to the source_branch parameter when calling the template from the pipeline needs to be a template expression:

 - template: download_artifact.yml
  parameters:
    artifacts:
    - project: 'XXX'
      pipeline: 291
      artifact: 'artifcat'
      branch: ${{ parameters.source_branch }}

Also, if the $(Build.SourceBranch) doesn't work in your parameter declaration, you can try:

parameters:
- name : source_branch
  displayName: Which Branch (e.g. refs/head/foo)
  type: string
  default: ${{ variables['Build.SourceBranch'] }}
0
votes

The parameters in the template should be expanded at pipeline compile time. And according to my tests and tries, we seem have no any available workaround for your scenario.