Search code examples
azure-devopsazure-pipelinescicdazure-pipelines-yaml

Manually triggering Devops pipeline with pipeline resource should use latest resource pipeline run for that branch


I have 2 pipelines in the same repo:

  1. Build
  2. Deploy

The Build pipeline is declared as a pipeline resource in the Deploy pipeline:

resources:
  pipelines:
  - pipeline: Build 
    source: BuildPipelineName
    trigger: true

When I run the Build pipeline, the Deploy pipeline is correctly triggered on the same branch. However, when I run the Deploy pipeline manually, it does not use the latest pipeline run from same branch.

I tried adding a couple of variations of the line below to the to the pipeline resource, but the variable does not expand:

branch: ${{ variables.Build.SourceBranchName }}

Is there any way to make this work?


Solution

  • Workaround that achieves the result I am looking for, but is not very elegant:

              - ${{ if ne(variables['Build.Reason'], 'ResourceTrigger')  }}:
                - task: DeleteFiles@1
                  displayName: 'Remove downloaded artifacts from pipeline resource'
                  inputs:
                    SourceFolder: $(Pipeline.Workspace)
    
                - task: DownloadPipelineArtifact@2
                  displayName: 'Download artifacts for branch'
                  inputs:
                    source: 'specific'
                    project: 'myProject'
                    pipeline: <BuildPipelineId>
                    runVersion: 'latestFromBranch'    
                    runBranch: $(Build.SourceBranch)