Rachel
Rachel

Reputation: 1284

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?

Upvotes: 3

Views: 1409

Answers (3)

Viktor Eriksson
Viktor Eriksson

Reputation: 6219

This works for me:

resources:
  pipelines:
  - pipeline: buildpipelineCI 
    source: 'Other pipeline-CI' 
    trigger: true
    branch: ${{variables['Build.SourceBranch']}}

Upvotes: 1

Rachel
Rachel

Reputation: 1284

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)

Upvotes: 2

Bowman Zhu
Bowman Zhu

Reputation: 7146

For example, if I have a build pipeline named 'BuildPipelineAndDeployPipeline',

then the below YAML definition can get the latest build pipeline run from a specific branch:

resources:
  pipelines:
  - pipeline: BuildPipelineAndDeployPipeline
    project: xxx
    source: BuildPipelineAndDeployPipeline
    trigger:
      branches:
      - main

pool:
  vmImage: 'windows-latest'


steps:
- task: CmdLine@2
  inputs:
    script: |
      echo Write your commands here
      
      echo Hello world
      
      echo $(resources.pipeline.BuildPipelineAndDeployPipeline.runID)

Upvotes: 0

Related Questions