是Azure DevOps构建管道,是否可以从另一个作业中取消一个管道作业? [英] Is an Azure DevOps build pipeline, is there a way to cancel one pipeline job from another job?

查看:46
本文介绍了是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)

结果:

这篇关于是Azure DevOps构建管道,是否可以从另一个作业中取消一个管道作业?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆