2

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?

gumby
  • 49
  • 7

2 Answers2

0

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'] }}
WaitingForGuacamole
  • 3,744
  • 1
  • 8
  • 22
  • "A template expression is not allowed in this context" when replacing the default. Also updated OP as I had your first suggestion – gumby Mar 18 '21 at 02:33
  • Hi @gumby, 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. – Bright Ran-MSFT Mar 18 '21 at 08:38
0

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.

Bright Ran-MSFT
  • 5,190
  • 1
  • 5
  • 12