【发布时间】:2021-01-13 15:20:33
【问题描述】:
我正在查看 azure triggers documentation,但仍然无法找到合适的解决方案。 如何在流水线 1 的执行过程中触发流水线 2,等待其成功完成或失败,并根据流水线 2 的结果继续执行流水线 1 或失败?
【问题讨论】:
标签: azure azure-devops azure-pipelines devops azure-pipelines-release-pipeline
我正在查看 azure triggers documentation,但仍然无法找到合适的解决方案。 如何在流水线 1 的执行过程中触发流水线 2,等待其成功完成或失败,并根据流水线 2 的结果继续执行流水线 1 或失败?
【问题讨论】:
标签: azure azure-devops azure-pipelines devops azure-pipelines-release-pipeline
您可能正在寻找类似的东西。
# 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
它就在链接中,您已链接,但在文档的兄弟部分。我很惊讶你错过了它。
【讨论】:
在管道 1 执行过程中如何触发管道 2,等待它成功完成或失败,并根据管道 2 的结果继续执行管道 1 或失败?
触发一个又一个的管道,它会在触发管道成功完成后运行你的管道。在管道 1 的执行过程中,我们不能使用它来触发管道 1。
作为一种解决方法:
一个。我们可以添加任务 power shell 并添加脚本来调用 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.添加任务power shell并输入代码Start-Sleep -Seconds 1000休眠管道1
c。在管道 1 中添加任务 power shell,通过REST API, 获取管道 2 构建结果并将结果设置为 env 变量。
d。在下一个任务中配置 condition 以检查 env 变量值。如果值为succeeded,则继续运行管道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)
【讨论】:
如果您可以使用扩展程序,那么您可以在市场上获得的 Trigger Build Task 应该支持您的所有要求。
它允许您触发另一个管道,并提供等待它的选项,以及有关在等待时如何处理该管道故障的选项。因此,您可以使用它来触发构建,等待它,并根据构建是否成功/失败而成功/失败。
【讨论】: