azure devops - yaml pipeline stage to depend on other pipeline completion

I have a pipeline 'A' which has two stages- dev, prod. After dev environment finishes I want prod stage to be triggered only if pipeline 'B' is ran successfully. I want stage 'prod' of pipeline 'A' to be dependent on pipeline 'B'. Is this feasible?

enter image description here

Upvotes: 0

Views: 2199

Answers (2)

Kim Xu-MSFT
Kim Xu-MSFT

Reputation: 2196

1.Intasll this Azure DevOps Extension

enter image description here

2.In your Dev stage, add Trigger Build task to ensure you could trigger pipeline B and check the latest build result of pipeline B.

enter image description here

3.Create the Generic Service Connection.

enter image description here

4.Use Invoke REST API in Environment Approvals and checks.

enter image description here

API: GET https://dev.azure.com/{organization}/{project}/_apis/build/latest/{definition}?api-version=6.0-preview.1

5.After check pass, second stage will depend on the success build of Pipeline B.

trigger:
- none
stages:
 - stage: Dev
   jobs:
    - job: CI
      pool:
        vmImage: windows-latest
      steps:
        - task: TriggerBuild@4
          inputs:
            definitionIsInCurrentTeamProject: true
            buildDefinition: 'PipelineB'
            queueBuildForUserThatTriggeredBuild: true
            ignoreSslCertificateErrors: false
            useSameSourceVersion: false
            useCustomSourceVersion: false
            useSameBranch: true
            waitForQueuedBuildsToFinish: false
            storeInEnvironmentVariable: false
            authenticationMethod: 'Personal Access Token'
            password: 'PAT'
            enableBuildInQueueCondition: false
            dependentOnSuccessfulBuildCondition: false
            dependentOnFailedBuildCondition: false
            checkbuildsoncurrentbranch: false
            failTaskIfConditionsAreNotFulfilled: false
        
 - stage: Prod
   dependsOn: Dev
   jobs:
    - deployment: CD
      environment: {EnvironmentName}
      pool:
        vmImage: windows-latest
      strategy:
        runOnce:
          deploy:
              steps:
                - task: CmdLine@2
                  inputs:
                    script: |
                      echo Write your commands here
                      
                      echo Hello world

enter image description here

Upvotes: 1

wade zhou - MSFT
wade zhou - MSFT

Reputation: 8082

You can get Pipeline B result in stage dev (link here), and set it as variable, in the prod stage, evaluate the variable value to determine the stage to run or not(link here).

Code sample as below:

stages:
- stage: Dev
  jobs:
  - job: DevJob
    steps:
      - task: PowerShell@2
        name: GetpipelineBresult
        inputs:
          targetType: 'inline'
          script: |
            $url = "https://dev.azure.com/{organization}/{pipelineBProject}/_apis/build/builds?definitions={definitionid}&api-version=5.1"
            $personalToken = "$(PAT)"
            $token =   [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($personalToken)"))
            $header = @{authorization = "Basic $token"}
            $buildPipeline= Invoke-RestMethod -Uri $url -Headers $header -Method Get
            $BuildResult= $buildPipeline.value.result | Select-Object -first 1
            Write-Host This is Build Result: $BuildResult
            echo "##vso[task.setvariable variable=Buildresult;isOutput=true]$BuildResult"

- stage: Prod
  condition: eq(dependencies.Dev.outputs['DevJob.GetpipelineBresult.Buildresult'], 'succeeded')
  dependsOn: Dev
  jobs:
  - job:
    steps:
      - script: echo job Prod

Pipeline B result: succeeded enter image description here

Pipeline B result: not succeeded(cancel, failed..) enter image description here

Upvotes: 3

Related Questions