是Azure DevOps构建管道,是否可以从另一个作业中取消一个管道作业?

问题描述:

我有一个Azure DevOps构建管道,其中包含两个代理作业,我将它们称为作业A和作业B.我希望这些作业同时运行,但是如果作业A失败,则不需要作业B运行到完成.

I have an Azure DevOps Build Pipeline which contains two Agent Jobs, which I'll call Job A and Job B. I want these jobs to run simultaneously, but if Job A fails, then I don't need Job B to run to completion.

是否有任何方法可以将任务A添加到任务A中,从而在任务A的任何任务失败时取消任务B(或以失败"状态终止整个管道)?

Is there any way to add a task to Job A which will cancel Job B (or, alternately, terminate the entire pipeline with a "Failed" status) if any of Job A's tasks failed?

添加一个PowerShell任务,该任务在任务失败时取消管道:

Add a PowerShell task that cancel the pipeline when a task failed:

steps:
- powershell: |
   Write-Host "Cancel all jobs..."
   $url = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/builds/$($env:BUILD_BUILDID)?api-version=2.0"
    $header = @{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"  }
    $body = @{ 'status'='Cancelling' } | ConvertTo-Json
    Invoke-RestMethod -Uri $url -Method Patch -Body $body -Headers $header -ContentType application/json
  displayName: Cancel the pipeline
  condition: failed()
  env:
       System_AccessToken: $(System.AccessToken)

结果: