在 Azure DevOps 中,我有两个管道:
现在,在 PipelineA 中,我想触发 PipelineB,并等待 PipelineB 完成。如果 PipelineB 成功,PipelineA 将继续,如果 PipelineB 失败,PipelineA 也将失败。
这是一个示例,其中一个管道作业将依赖于另一个管道作业。我想要相同的机制,但依赖于另一个管道的运行。
jobs:
- job: Debug
steps:
- script: echo hello from the Debug build
- job: Release
dependsOn: Debug
steps:
- script: echo hello from the Release build
正如建议的那样,使用 DevOps API 是正确的选择。
这是一个 PowerShell 脚本,它调用另一个管道
param(
[int]$pipelineId = 1234, # references https://dev.azure.com/a-comany/a-project/_build?definitionId=1234
[string]$invokingPipelineName,
[string]$invokingProject,
[int]$invokingBuildId
)
# You need a token (which might be System.AccessToken), which requires the authorization "Build" -> "Read & execute".
$authToken = "1234abcdef0987..."
function Invoke-Pipeline
{
$organization = "a-comany"
$project = "a-project"
$pipelineRunsUrl = "https://dev.azure.com/$organization/$project/_apis/pipelines/$pipelineId/runs?api-version=7.1"
Write-Host "##[group] Invoking pipeline..."
Write-Host "##[debug] PipelineId '$pipelineId' in $project"
Write-Host "##[debug] Pipeline url: https://dev.azure.com/$organization/$project/_build?definitionId=$pipelineId"
Write-Host "##[debug] Pipeline runs url: $pipelineRunsUrl"
Write-Host "##[endgroup]"
$base64AuthToken = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$authToken"))
$headers = @{
Authorization = "Basic $base64AuthToken"
"cache-control" = "no-cache"
"content-type" = "application/json"
}
$body = @{
resources = @{
repositories = @{
self = @{
refName = "refs/heads/main"
}
}
}
templateParameters = @{
invokingPipelineName = $invokingPipelineName
invokingProject = $invokingProject
invokingBuildId = $invokingBuildId
}
} | ConvertTo-Json -Depth 3 -Compress
$createRunResponseJson = Invoke-RestMethod -Uri $pipelineRunsUrl -Method Post -Headers $headers -Body $body -ErrorAction Stop
$pipelineRunUrl = $createRunResponseJson.url
$runId = $createRunResponseJson.id
Write-Host "Started pipeline, runId: $runId"
Write-Host "##[group] Pipeline run web url: $( $createRunResponseJson._links.web.href )"
Write-Host "##[debug] $createRunResponseJson"
Write-Host "##[endgroup]"
while ($true)
{
$getStatusResponseJson = Invoke-RestMethod -Uri $pipelineRunUrl -Headers $headers -ErrorAction Stop
$pipelineRunUrl = $getStatusResponseJson.url
Write-Host "##[group] Pipeline state: $( $getStatusResponseJson.state ) ($( Get-Date -Format "HH:mm:ss" ) GMT)"
Write-Host "##[debug] Run web url: $( $getStatusResponseJson._links.web.href )"
Write-Host "##[debug] $getStatusResponseJson"
Write-Host "##[endgroup]"
if ($getStatusResponseJson.state -ne "inProgress")
{
break
}
Start-Sleep -Seconds 5
}
Write-Host "result at: https://dev.azure.com/$organization/$project/_build/results?buildId=$runId"
if ($getStatusResponseJson.result -ne "succeeded")
{
Write-Error "Invoking did not succeed" -ErrorAction Stop
}
Write-Host "Invoking finished successfully"
}
Invoke-Pipeline
在Azure DevOps管道中,可以像这样执行:
- task: PowerShell@2
displayName: Invoke pipeline
inputs:
filePath: invoker.ps1
arguments: >
-invokingPipelineName "$(Build.DefinitionName)"
-invokingProject "$(System.TeamProject)"
-invokingBuildId "$(Build.BuildId)"
调用的管道具有参数,以便在管道运行时显示调用者/“返回方式”:
parameters:
- name: invokingPipelineName
type: string
default: ' '
- name: invokingProject
type: string
default: ' '
- name: invokingBuildId
type: number
default: 0
steps:
- script: |
echo Invoking pipeline name: ${{ parameters.invokingPipelineName }}
echo Invoking pipeline run: https://dev.azure.com/a-company/${{ parameters.invokingProject }}/_build/results?buildId=${{ parameters.invokingBuildId }}
displayName: Print invoker (if given)
condition: ne('${{ parameters.invokingPipelineName }}', ' ')