简体   繁体   English

Azure DevOps 触发管道

[英]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.我有下面的代码运行 3 个管道,但我想让它运行第一个管道并构建产品,然后运行另外两个管道,而不是同时运行所有管道,所以先运行第一个,然后再运行另一个2 第一次成功后。

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.您可以在第一个触发管道任务之后添加 PowerShell 任务。

Here is Powershell script sample:这是 Powershell 脚本示例:

$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:这个 powershell 脚本运行以下两个 Rest API:

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.上面的答案是正确的,但我必须添加它来验证我的身份。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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