简体   繁体   中英

Azure DevOps Trigger Pipelines

I have the code below that runs 3 pipelines but I want to make it so it runs the first one and builds the product and then it runs the other two instead of all of them running at once so do the first one and then do the other 2 after the first one was successful.

variables:
- group: ReleaseVariables

name: 5.8$(rev:.r)

jobs:
- job: Ring_Web_Policy_Editor
  timeoutInMinutes: 360

  pool:
    name: DATA-AUTOMATION-WIN10
    demands: Cmd

  steps:
  - task: TriggerPipeline@1
    inputs:
      serviceConnection: 'azure-connection-dev'
      project: '46da8f34-c009-4433-a2f5-1790a09b6055'
      Pipeline: 'Build'
      buildDefinition: 'Web Policy Editor'
      Branch: '$(Build.SourceBranch)'
  - task: TriggerPipeline@1
    inputs:
      serviceConnection: 'azure-connection-dev'
      project: '46da8f34-c009-4433-a2f5-1790a09b6055'
      Pipeline: 'Build'
      buildDefinition: '(Chrome) Web Policy Editor Automation'
      Branch: '$(Build.SourceBranch)'
  - task: TriggerPipeline@1
    inputs:
      serviceConnection: 'azure-connection-dev'
      project: '46da8f34-c009-4433-a2f5-1790a09b6055'
      Pipeline: 'Build'
      buildDefinition: '(Firefox) Web Policy Editor Automation'
      Branch: '$(Build.SourceBranch)'
    

do the first one and then do the other 2 after the first one was successful.

You could add a PowerShell task after the first Trigger Pipeline Task.

Here is Powershell script sample:

$token = "PAT"

$url="https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/build/definitions/{DeefinitionId}?includeLatestBuilds=true&api-version=5.1"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))



$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json

$buildid = $response.latestBuild.id

$success = $false

do{
    try{
    $Buildurl2 = "https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/build/builds/$($buildid)?api-version=5.0"
    $Buildinfo2 = Invoke-RestMethod -Method Get -ContentType application/json -Uri $Buildurl2 -Headers @{Authorization=("Basic {0}" -f $token)}
    $BuildStatus= $Buildinfo2.status 
    $result = $Buildinfo2.result
    echo $result
    echo $BuildStatus
 
   
        if($BuildStatus -eq "completed") {            

            write-output "No Running Pipeline, starting Next Pipeline"
            $success = $true 
                       
      } else {   
            Write-output "Pipeline Build In Progress, Waiting for it to finish!"  
            Write-output "Next attempt in 30 seconds"
            Start-sleep -Seconds 30         

            }
    
      
    }
    catch{
        Write-output "catch - Next attempt in 30 seconds"
        write-output "1"
        Start-sleep -Seconds 30
      # Put the start-sleep in the catch statemtnt so we
      # don't sleep if the condition is true and waste time
    }
    
    $count++
    
}until($count -eq 2000 -or $success -eq $true )
if ($result -ne "succeeded")
{
   echo "##vso[task.logissue type=error]Something went very wrong."
}

if(-not($success)){exit}

Explanation:

This powershell script runs the following two Rest APIs:

Definitions - Get

Builds - Get

The script checks the status of the pipeline(by polling) that has been triggered. If the pipeline is completed and the result is successful, it will trigger the other two pipelines. Or it will wait for the pipeline finishing the build.

Pipeline sample:

 steps:
  - task: TriggerPipeline@1
  - task: PowerShell@2
  - task: TriggerPipeline@1
  - task: TriggerPipeline@1

Result Sample:

在此处输入图像描述

            $Username=${env:USERNAME}
            $PAT=${env:PATTOKEN}
            $Buildurl2 = "https://dev.azure.com/{ORGANISATION}/{Project}/_apis/build/builds/$($buildid)?api-version=5.0"
            $token = -join("$Username", ":", "$PAT")
            $headers = @{ 
                Authorization = "Basic "+ [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($token))
                'Content-Type' = "application/json"
            }
            $Buildinfo2 = Invoke-RestMethod -Method Get -ContentType application/json -Uri $Buildurl2 -Headers $headers
            $BuildStatus= $Buildinfo2.status 
            $result = $Buildinfo2.result 
            echo $result
            echo $BuildStatus

The answer above is correct but I had to add this to authenticate mine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM