从另一个Azure DevOps管道触发流水线

5

我正在查看Azure触发器文档,但仍然无法找到合适的解决方案。 在管道1执行期间如何触发管道2,并等待其成功完成或失败,并根据管道2的结果继续执行管道1或失败?

4个回答

5
在执行管道1期间,如何触发管道2并等待其成功完成或失败,并根据管道2的结果继续执行管道1或使其失败?
可以在一个管道成功完成后触发另一个管道,它将在触发管道成功完成后运行您的管道。我们无法在执行管道1期间触发管道1。
解决方法: a. 我们可以添加任务PowerShell并添加脚本调用REST API来排队构建。
$connectionToken="PAT"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$PipelineUrl = "https://dev.azure.com/{Org name}/{project name}/_apis/pipelines/{Pipeline ID}/runs?api-version=6.0-preview.1" 

$body ="{ 
 `"resources`":{
        `"repositories`":{
            `"self`":{`"refName`":`"refs/heads/master`"
            }
         }
    }
}"
$Pipelines = Invoke-RestMethod -Uri $PipelineUrl -ContentType "application/json" -Body $body -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST
b. 添加任务PowerShell,并输入代码Start-Sleep -Seconds 1000使流水线1休眠。

c. 在流水线1中添加任务PowerShell,通过REST API获取流水线2的构建结果,并将结果设置为环境变量。 d. 配置下一个任务中的条件,以检查环境变量的值。如果值是succeeded,则继续运行流水线1。

我简直不敢相信这是实现这个目标的唯一方法。在我的看法中,Azure DevOps 中实现依赖管道的方式是反向的 - 为什么依赖管道需要知道它的父级?依赖关系应该反过来才对。 - David Masters
@David Masters - 我们一直在使用“Trigger Build Task”扩展,效果非常好(https://marketplace.visualstudio.com/items?itemName=benjhuser.tfs-extensions-build-tasks)。作者在这方面做得很出色,并且对 GitHub 上的问题进行了积极回应。我同意你对依赖关系的看法是相反的,而这个扩展解决了这个问题。 - dcp

1

所以这是我基于上面的建议提出的解决方案:

- task: PowerShell@2
  displayName: Running second pipeline
  inputs:
       targetType: 'inline'
       script: |
        Write-Host "Triggering pipeline..."
        $connectionToken= "$(PAT)"

        $base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
        $PipelineUrl = "https://dev.azure.com/YourOrganization/yourProject/_apis/pipelines/${{ parameters.pipelineId }}/runs?api-version=6.0-preview.1" 
        Write-Host "Pipeline url: $PipelineUrl"
        $body ="{ 
         `"resources`":{
                `"repositories`":{
                    `"self`":{`"refName`":`"refs/heads/${{ parameters.branch }}`"
                    }
                 }
            }
        }"       
        $response = Invoke-RestMethod -Uri $PipelineUrl -ContentType "application/json" -Body $body -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method POST
        Write-Host "Response: $response"
        $BuildUrl = "https://dev.azure.com/YourOrganization/yourProject/_apis/build/builds/$($response.Id)?api-version=6.1-preview.6"
        Write-Host  $BuildUrl
        
        $TimeoutAfter = New-TimeSpan -Minutes 15
        $WaitBetweenPolling = New-TimeSpan -Seconds 10
        $Timeout = (Get-Date).Add($TimeoutAfter)
        do
        {
        $Response = Invoke-RestMethod -Uri $BuildUrl -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
        Write-Host $Response.status
        Start-Sleep -Seconds $WaitBetweenPolling.Seconds
        }
        while ((($Response.status -eq "notStarted") -or ($Response.status -eq "inProgress")) -and ((Get-Date) -lt $Timeout))
        
        if ($Response.result -ne "succeeded")
        {
            Write-Host $Response.result
            exit 1
        }

管道 ID 的参数:pipelineId: $(resources.pipeline.resource.pipelineId)


1
你可能正在寻找这样的东西。
# this is being defined in app-ci pipeline
resources:
  pipelines:
  - pipeline: securitylib   # Name of the pipeline resource
    source: security-lib-ci # Name of the pipeline referenced by the pipeline resource
    trigger: 
      branches:
      - releases/*
      - master

在你提供的链接中,它就在那里,但在文档的兄弟部分。我很惊讶你错过了它。

https://learn.microsoft.com/en-us/azure/devops/pipelines/process/pipeline-triggers?view=azure-devops&tabs=yaml


2
感谢您的回复,Jay。我看了那个选项,但这个解决方案不能停止当前管道的执行,并且只有在执行完成后才会触发第二个管道。根据文档:“触发器在触发管道成功完成后运行您的管道”。我的需求是要停止管道A,直到管道B执行完成。然后根据管道B的结果,继续执行管道A或停止。 - Roman Svitukha
1
啊,对了,当然,我假设有两个管道的情况,文章也是这样。问题是,据我所知,YAML不是JavaScript。它更像是从山上流下来的河流。你可以引导事物,但不能回路。如果我是你,我会建立3个管道。管道1触发管道2,然后根据管道2的输出,我触发下一个管道3(你的第一个管道,分解为管道1和3或更多部分,如果你有更多事情要做)。 - Jay

0

如果您可以使用扩展程序,那么您可以在市场上获取的触发构建任务应该支持您的所有要求。

它允许您触发另一个流水线,并提供等待选项以及有关如何处理该流水线失败的选项(如果您选择等待)。因此,您可以使用它来触发构建,等待它完成,并根据构建成功/失败来判断是否成功/失败。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接