Azure DevOps 可选阶段

标签 azure azure-devops yaml devops

我有一个用于构建和部署项目的 yaml 管道。它部署到开发和测试。但我并不总是想部署到 tst。目前我已经通过批准解决了这个问题。当开发阶段完成时,管道等待批准。

这引入了一些问题:

  • 所有开发者每次都会收到一封电子邮件,这可能很烦人。
  • 管道会等待部署到 tst,即使不需要部署到 tst。它使 等待
  • 当管道等待时,新的 ci 构建不会启动

最佳答案

如果有明确定义的方法来确定是否运行 tst 阶段,您可以 specify a custom condition在舞台上或 conditionally include the stage using an expression

指定条件

From the docs:

You can specify the conditions under which each stage, job, or step runs. By default, a job or stage runs if it does not depend on any other job or stage, or if all of the jobs or stages that it depends on have completed and succeeded.

您可以通过多种方式自定义此行为,可以通过内置条件或自定义定义的条件,例如,仅当分支是 main 时才运行 tun tst 阶段:

stages:
- stage: dev
  jobs:
  - job: dev1
    steps:
      - script: echo Hello from dev!

- stage: tst
  condition: and(succeeded(), eq(variables['Build.SourceBranch'], 'refs/heads/main'))
  jobs:
  - job: tst1
    steps:
      - script: echo Hello From tst!

当您打开管道运行时,tst 阶段仍然可见,它将被标记为已跳过

可以更改 tst 阶段下的条件以满足您的需要(它可以包括 variablesparameters 甚至 output from previous stages

条件表达式

From the docs:

You can use if, elseif, and else clauses to conditionally assign variable values or set inputs for tasks. You can also conditionally run a step when a condition is met.

条件插入仅适用于模板语法,该语法在编译管道 yaml 时(即管道启动之前)进行评估。因此它无法评估先前步骤的输出。在下面的示例中,如果分支不是 main

,则在实例化管道(并编译模板)时,tst 阶段将从管道运行中完全删除
stages:
- stage: dev
  jobs:
  - job: dev1
    steps:
      - script: echo Hello from dev!
- ${{ if eq(variables['Build.SourceBranch'], 'refs/heads/main') }}:
  - stage: tst
    jobs:
    - job: tst1
      steps:
        - script: echo Hello From tst!

关于Azure DevOps 可选阶段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69808639/

相关文章:

powershell - 通过 Kudu 命令 API 运行 PowerShell 来编辑文件

ios - 如何使用 Azure DevOps 增加 xcode 项目的内部版本号

mysql - 当通过 Django 的固定装置输入时,值 "NO"在 MySQL 中是否被解析为 "False"?

linux - 如何将yaml文件拆分成多个文件?

xml - 使用 Ruby 和 Hpricot 将 xml 转换为 yaml - 这里出了什么问题?

azure - 无法从 Power BI 连接到 Azure DocumentDB

azure - SQL Azure STDistance 性能

azure - 是否可以为 Azure 应用服务中同一 IP 地址的多个请求创建警报?

c# - Azure DevOps VSTS .netcore 构建失败,但在我的电脑上运行良好

azure - 我可以通过使用测试程序集在构建管道中执行自动化测试来更新测试计划吗