从另一个管道触发 Azure DevOps 管道
Trigger Azure DevOps pipeline from another pipeline
我正在查看 azure 触发器 documentation,但仍然找不到合适的解决方案。
在管道 1 执行期间如何触发管道 2,等待它成功完成或失败,并根据管道 2 的结果继续执行管道 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
它就在 link 中,您已经 link 编辑了,但是在文档的兄弟部分。我很惊讶你错过了它。
How during the execution of pipeline 1 can you trigger pipeline 2, wait for it to successfully finish or fail, and based on pipeline 2 results either continue execution of pipeline 1 or fail?
一个接一个地触发管道,它会在触发管道成功完成后运行你的管道。我们不能在管道1的执行中用它来触发管道1。
解决方法:
一个。我们可以添加任务权限 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。添加任务功率shell并输入代码Start-Sleep -Seconds 1000
休眠管道1
c。在pipeline 1中添加task power shell,通过REST API,获取pipeline 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 您可以在市场上获得的应该支持您的所有要求。
它可以让你触发另一个管道,有一个等待它的选项,以及如果你等待的话如何处理该管道故障的选项。因此,您可以使用它来触发构建、等待它,并根据构建成功/失败来判断成功/失败。
我正在查看 azure 触发器 documentation,但仍然找不到合适的解决方案。 在管道 1 执行期间如何触发管道 2,等待它成功完成或失败,并根据管道 2 的结果继续执行管道 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
它就在 link 中,您已经 link 编辑了,但是在文档的兄弟部分。我很惊讶你错过了它。
How during the execution of pipeline 1 can you trigger pipeline 2, wait for it to successfully finish or fail, and based on pipeline 2 results either continue execution of pipeline 1 or fail?
一个接一个地触发管道,它会在触发管道成功完成后运行你的管道。我们不能在管道1的执行中用它来触发管道1。
解决方法:
一个。我们可以添加任务权限 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。添加任务功率shell并输入代码Start-Sleep -Seconds 1000
休眠管道1
c。在pipeline 1中添加task power shell,通过REST API,获取pipeline 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 您可以在市场上获得的应该支持您的所有要求。
它可以让你触发另一个管道,有一个等待它的选项,以及如果你等待的话如何处理该管道故障的选项。因此,您可以使用它来触发构建、等待它,并根据构建成功/失败来判断成功/失败。