![](/img/trans.png)
[英]How to trigger one pipeline stage from another pipeline in Azure DevOps?
[英]Trigger Azure DevOps pipeline from another pipeline
我正在查看 azure triggers 文檔,但仍然無法找到合適的解決方案。 如何在流水線 1 的執行過程中觸發流水線 2,等待它成功完成或失敗,並根據流水線 2 的結果繼續執行流水線 1 或失敗?
如何在管道 1 執行期間觸發管道 2,等待它成功完成或失敗,並根據管道 2 結果繼續執行管道 1 或失敗?
觸發一個又一個管道,它會在觸發管道成功完成后運行您的管道。 我們不能在管道 1 的執行中使用它來觸發管道 1。
作為解決方法:
一種。 我們可以添加任務電源外殼並添加腳本來調用 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
灣添加任務電源外殼並輸入代碼Start-Sleep -Seconds 1000
使管道1休眠
C。 在管道1中添加任務power shell,通過REST API獲取管道2的構建結果,並將結果設置為env變量。
d. 在下一個任務中配置條件以檢查 env 變量值。 如果該值succeeded
,則繼續運行管道 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
它就在鏈接中,您已鏈接,但在文檔的兄弟部分。 我很驚訝你錯過了它。
所以這是我基於上述建議的解決方案:
- 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)
如果您可以使用擴展,那么您可以在市場上獲得的觸發器構建任務應該支持您的所有需求。
它允許您觸發另一個管道,並提供等待它的選項,以及有關如何在等待時處理該管道故障的選項。 所以你可以使用它來觸發構建,等待它,並根據構建是否成功/失敗來成功/失敗。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.