Azure DevOps Trigger Pipelines

混江龙づ霸主 提交于 2021-01-28 14:18:43

问题


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)'
    

回答1:


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:




回答2:


            $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.



来源:https://stackoverflow.com/questions/65410655/azure-devops-trigger-pipelines

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!