You need to enable JavaScript to run this app. 如何在Azure Pipeline YAML中让任务仅依赖指定任务失败时执行?
优惠活动
大模型
产品
解决方案
定价
更多

如何在Azure Pipeline YAML中让任务仅依赖指定任务失败时执行?

Azure Pipeline 实现仅特定任务失败时触发后续任务

需求:仅当任务B失败时,才运行任务C和D。之前使用failed()条件会在任务A失败时也触发C、D,不符合预期,需要指定依赖某一特定任务的状态。

现有YAML代码:

- task: A
  inputs:
    command: 
    repository: 
    Dockerfile: 
    tags: 
  displayName: 'Build test image'

- task: B
  inputs:
    fullImageNameAndTag: 
  displayName: 'test image'
  condition: succeeded()

- task: C
  inputs:
    targetPath: 
  displayName: 'Download test image'
  condition: ?

- task: D
  inputs:
    targetPath: 
  displayName: 'Download test2 image'
  condition: ?

解决方案:
Azure Pipeline支持在条件中指定特定任务的状态,通过failed('任务标识')来判断指定任务是否失败。为了避免重名问题,建议给任务B添加唯一的name属性,再在C、D的条件中引用该名称:

修改后的YAML代码:

- task: A
  inputs:
    command: 
    repository: 
    Dockerfile: 
    tags: 
  displayName: 'Build test image'

- task: B
  name: TaskB # 添加唯一名称用于条件判断
  inputs:
    fullImageNameAndTag: 
  displayName: 'test image'
  condition: succeeded()

- task: C
  inputs:
    targetPath: 
  displayName: 'Download test image'
  condition: failed('TaskB') # 仅当TaskB失败时执行

- task: D
  inputs:
    targetPath: 
  displayName: 'Download test2 image'
  condition: failed('TaskB') # 仅当TaskB失败时执行

说明:

  • failed('TaskB')会精准判断任务B的失败状态,不会受其他任务(如A)的状态影响
  • 任务B的condition: succeeded()确保只有任务A成功时才会执行B,因此当B失败时,任务A必然处于成功状态,完全符合需求
  • 如果不想添加name属性,也可以直接使用任务的displayName,语法为failed('test image'),但优先推荐用name避免重名风险

内容的提问来源于stack exchange,提问作者生きがい

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 16:28:27