stage.stage 定义

阶段是相关作业的集合。 默认情况下,阶段按顺序运行。 除非通过 dependsOn 属性另行指定,否则每个阶段才在上一阶段完成之后启动。

stages:
- stage: string # Required as first property. ID of the stage.
  displayName: string # Human-readable name for the stage.
  pool: string | pool # Pool where jobs in this stage will run unless otherwise specified.
  dependsOn: string | [ string ] # Any stages which must complete before this one.
  condition: string # Evaluate this condition expression to determine whether to run this stage.
  variables: variables | [ variable ] # Stage-specific variables.
  jobs: [ job | deployment | template ] # Jobs which make up the stage.
  lockBehavior: sequential | runLatest # Behavior lock requests from this stage should exhibit in relation to other exclusive lock requests.
  trigger: manual | automatic # Stage trigger manual or automatic (default).
  isSkippable: boolean # Setting false prevents the stage from being skipped. By default it's always true.
  templateContext: # Stage related information passed from a pipeline when extending a template.
stages:
- stage: string # Required as first property. ID of the stage.
  displayName: string # Human-readable name for the stage.
  pool: string | pool # Pool where jobs in this stage will run unless otherwise specified.
  dependsOn: string | [ string ] # Any stages which must complete before this one.
  condition: string # Evaluate this condition expression to determine whether to run this stage.
  variables: variables | [ variable ] # Stage-specific variables.
  jobs: [ job | deployment | template ] # Jobs which make up the stage.
  lockBehavior: sequential | runLatest # Behavior lock requests from this stage should exhibit in relation to other exclusive lock requests.
  templateContext: # Stage related information passed from a pipeline when extending a template.
stages:
- stage: string # Required as first property. ID of the stage.
  displayName: string # Human-readable name for the stage.
  pool: string | pool # Pool where jobs in this stage will run unless otherwise specified.
  dependsOn: string | [ string ] # Any stages which must complete before this one.
  condition: string # Evaluate this condition expression to determine whether to run this stage.
  variables: variables | [ variable ] # Stage-specific variables.
  jobs: [ job | deployment | template ] # Jobs which make up the stage.

引用此定义的定义:阶段

性能

stage 字符串。 必需为第一个属性。
阶段 ID。

displayName 字符串。
阶段的人类可读名称。

pool
在此阶段中运行作业的池,除非另有指定。

dependsOn 字符串 |字符串列表。
必须在此阶段之前完成的任何阶段。 默认情况下,阶段按管道中定义的顺序按顺序运行。 如果某个阶段不应依赖于管道中的上一阶段,请为该阶段指定 dependsOn: []

condition 字符串。
计算此条件表达式以确定是否运行此阶段。

variables 变量
特定于阶段的变量。

jobs 作业
构成阶段的作业。

lockBehavior 字符串。
此阶段的行为锁请求应与其他独占锁请求相关。 顺序 |runLatest。

trigger 字符串。
阶段触发器手动或自动(默认值)。 manual |自动。

isSkippable 布尔
设置 false 可防止跳过阶段。 默认情况下,它始终为 true。

templateContext templateContext。
扩展模板时从管道传递的阶段相关信息。 有关 templateContext的详细信息,请参阅 扩展 YAML 管道模板现在可以传递阶段、作业和部署的上下文信息,模板 - 使用 templateContext 将属性传递给模板

注解

有关 templateContext的详细信息,请参阅 扩展 YAML 管道模板现在可以传递阶段、作业和部署的上下文信息,模板 - 使用 templateContext 将属性传递给模板

使用 审批检查 手动控制何时应运行阶段。 这些检查通常用于控制到生产环境的部署。

检查是 资源所有者可用的机制。 他们控制管道中的阶段何时使用资源。 作为资源(如环境)的所有者,可以定义使用资源的阶段开始之前所需的检查。

目前,环境支持手动审批检查。 有关详细信息,请参阅 审批

独占锁

在 YAML 管道中,检查用于控制 受保护资源的阶段的执行。 可以使用的常见检查之一是 独占锁检查。 此检查仅允许从管道运行单个运行。 当多个运行尝试同时部署到环境时,检查将取消所有旧运行并允许部署最新的运行。

可以使用具有两个值的 lockBehavior 属性配置排他锁检查的行为:

  • runLatest - 只有最新的运行获取资源的锁。 如果未指定 lockBehavior,则为默认值。
  • sequential - 所有运行都会按顺序获取受保护资源的锁。

如果发布是累积的,并且包含以前运行的所有代码更改,则取消旧运行是一个很好的方法。 但是,某些管道中的代码更改不是累积的。 通过配置 lockBehavior 属性,可以选择允许所有运行按顺序继续并按顺序部署到环境,或保留先前取消旧运行并仅允许最新运行的行为。 sequential 值表示所有运行都按顺序获取受保护资源的锁。 runLatest 的值意味着只有最新的运行获取资源的锁。

若要对 sequential 部署或 runLatest使用排他锁检查,请执行以下步骤:

  1. 对环境(或其他受保护的资源)启用排他锁检查。
  2. 在管道的 YAML 文件中,指定名为 lockBehavior的新属性。 可以为整个管道或给定阶段指定此项:

在舞台上设置:

stages:
- stage: A
  lockBehavior: sequential
  jobs:
  - job: Job
    steps:
    - script: Hey!

在管道上设置:

lockBehavior: runLatest
stages:
- stage: A
  jobs:
  - job: Job
    steps:
    - script: Hey!

阶段级别的独占锁

某些用例要求管道在任何给定时间仅访问特定资源一次。 为了支持这种情况,我们具有上一节中所述的独占锁检查。

当只有一个管道运行应该在任何时间点访问某个阶段时,会出现类似的情况。 例如,如果有部署到 Azure 资源组的阶段,可能需要阻止多个管道运行同时更新同一资源组。 目前,实现此要求使用代理资源(例如环境),并在该环境中放置排他锁检查。 这种方法可能非常耗时,增加了复杂性,并增加了维护工作量。

如果需要确保一次只有一个管道运行可以访问某个阶段,则可以在阶段级别指定独占锁。 如果你有一个具有 ID 的阶段并指定其 lockBehavior 属性,则会自动为该阶段创建锁。 对于资源级锁和阶段级锁,顺序行为保持一致。 但是,runLatest 行为有所不同,因为它只取消同一分支的 runLatest 生成,而不是针对管道的所有分支。

例子

此示例一个接一个地运行三个阶段。 中间阶段并行运行两个作业。

stages:
- stage: Build
  jobs:
  - job: BuildJob
    steps:
    - script: echo Building!
- stage: Test
  jobs:
  - job: TestOnWindows
    steps:
    - script: echo Testing on Windows!
  - job: TestOnLinux
    steps:
    - script: echo Testing on Linux!
- stage: Deploy
  jobs:
  - job: Deploy
    steps:
    - script: echo Deploying the code!

此示例并行运行两个阶段。 为简洁起见,省略作业和步骤。

stages:
- stage: BuildWin
  displayName: Build for Windows
- stage: BuildMac
  displayName: Build for Mac
  dependsOn: [] # by specifying an empty array, this stage doesn't depend on the stage before it

另请参阅

详细了解 阶段条件,以及 变量