Azure DevOps触发管道 [英] Azure DevOps Trigger Pipelines

查看:73
本文介绍了Azure DevOps触发管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面的代码可以运行3个管道,但是我想使其运行第一个管道并构建产品,然后再运行其他两个管道,而不是同时运行所有其他管道,因此第一个管道然后运行在第一个成功之后再执行另外两个.

 变量:-组:ReleaseVariables名称:5.8 $(rev:.r)职位:-工作:Ring_Web_Policy_EditortimeoutInMinutes:360水池:名称:DATA-AUTOMATION-WIN10要求:Cmd脚步:-任务:TriggerPipeline @ 1输入:serviceConnection:"azure-connection-dev"项目:"46da8f34-c009-4433-a2f5-1790a09b6055"管道:构建"buildDefinition:"Web策略编辑器"分支:"$(Build.SourceBranch)"-任务:TriggerPipeline @ 1输入:serviceConnection:"azure-connection-dev"项目:"46da8f34-c009-4433-a2f5-1790a09b6055"管道:构建"buildDefinition:'(Chrome)Web策略编辑器自动化'分支:"$(Build.SourceBranch)"-任务:TriggerPipeline @ 1输入:serviceConnection:"azure-connection-dev"项目:"46da8f34-c009-4433-a2f5-1790a09b6055"管道:构建"buildDefinition:'(Firefox)Web策略编辑器自动化'分支:"$(Build.SourceBranch)" 

解决方案

做第一个,然后在第一个成功后再做另外2个.

您可以在第一个触发器管道任务之后添加PowerShell任务.

这是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$成功= $假做{尝试{$ Buildurl2 =" https://dev.azure.com/{OrganizationName}/{ProjectName}/_apis/build/builds/$($ buildid)?api-version = 5.0$ Buildinfo2 = Invoke-RestMethod -Method获取-ContentType application/json -Uri $ Buildurl2 -Headers @ {Authorization =(" Basic {0}" -f $ token)}$ BuildStatus = $ Buildinfo2.status$ result = $ Buildinfo2.result回声$结果回声$ BuildStatusif($ BuildStatus -eq"completed"){写输出没有正在运行的管道,正在启动下一个管道".$成功= $ true} 别的 {写入输出正在构建管道,等待其完成!";写输出"30秒内的下一次尝试".开始睡眠-第二30}}抓住{写输出"catch-30秒内的下一次尝试".写输出"1"被写入到输出"1".开始睡眠-第二30#将start-sleep置于catch statemtnt中,这样我们#如果情况为真,请不要入睡,浪费时间}$计数++}直到($ count -eq 2000-或$ success -eq $ true)如果($ result -ne成功"){回声"## vso [task.logissue type = error]出了点问题."}if(-not($ success)){退出} 

说明:

此powershell脚本运行以下两个Rest API:

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:

这篇关于Azure DevOps触发管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆