Azure 多级管道:有条件地跳过一个阶段但不跳过下一阶段

Azure multistage pipelines: conditionally skip one stage but not the next

我有一个 Azure 多阶段 CI/CD 管道。它具有测试和 UAT 部署阶段。

如果测试成功或被跳过,我希望 UAT 发布到 运行,但如果失败则不发布。

我不能。无论我尝试什么,如果跳过测试,也会跳过 UAT。除非我使用 always(),但即使测试失败,UAT 也会 运行。

  ...
  - stage: Test
    condition: and(succeeded(), ne(variables['build.sourceBranchName'], 'DoUAT')) # Skip for UAT deployment tests
    ...

  - stage: UAT
    condition: and(succeeded(), in(variables['build.sourceBranchName'], 'master', 'DoUAT')) # Only deploy off master branch and branch to test UAT deploys.
    ...

如何跳过一个阶段而不跳过下一阶段?

我认为这是因为阶段没有 运行 它没有获得状态(例如成功、失败、取消等)。 skipped.

没有状态函数

因此,我认为您需要在 Test 之前添加对阶段的依赖,以便进行此评估。假设该阶段称为 Build

我认为这个条件应该有效:(换行符只是为了便于阅读)

# run the stage if build is successful 
# and test succeeded or skipped 
# AND the branch is correct
and(
  and(succeeded('Build'), not(failed('Test'))), 
  in(variables['build.sourceBranchName'], 'master', 'DoUAT')
)

failed

  • For a job:
    • With no arguments, evaluates to True only if any previous job in the dependency graph failed.
    • With job names as arguments, evaluates to True only if any of those jobs failed.

由于此文档,我认为有必要添加 Test 参数以专门针对该阶段。但是,我不确定这是否需要将 Test 名称添加到 UAT 阶段的 dependencies 部分。

您可以使用not(failed('Test'))条件,请尝试以下条件。

- stage: UAT
    condition: and(not(failed('Test')), in(variables['build.sourceBranchName'], 'master', DoUAT')) # Only deploy off master branch and branch to test UAT deploys.
    ...

我测试过并且有效,请查看下面的屏幕截图。

我在寻找类似的信息,发现您可以对依赖结果执行“IN”子句。在 Microsoft Docs 关于表达式

中找到了这个
- job: c
  dependsOn:
  - a
  - b
  condition: |
    and
    (
      in(dependencies.a.result, 'Succeeded', 'SucceededWithIssues', 'Skipped'),
      in(dependencies.b.result, 'Succeeded', 'SucceededWithIssues', 'Skipped')
    )

归功于 https://github.com/MicrosoftDocs/azure-devops-docs/issues/7738#issuecomment-611815486

@EdH

condition: not(or(failed(), canceled()))

与前面的多个阶段一起工作,并按您的需要工作。 运行 如果前面的所有阶段都是 succeededskipped,则该阶段,但如果它们是 failedcancelled,则不是。

备注:

  • 跳过不会导致 failed() 或 cancelled() 为真。
  • 跳过确实会导致 succeeded() 为假。