schedules.cron 定义

计划触发器指定生成分支的计划。

schedules:
- cron: string # Required as first property. Cron syntax defining a schedule in UTC time.
  displayName: string # Optional friendly name given to a specific schedule.
  branches: # Branch names to include or exclude for triggering a run.
    include: [ string ] # List of items to include.
    exclude: [ string ] # List of items to exclude.
  batch: boolean # Whether to run the pipeline if the previously scheduled run is in-progress; the default is false.
  always: boolean # Whether to always run the pipeline or only if there have been source code or pipeline settings changes since the last successful scheduled run. The default is false.
schedules:
- cron: string # Required as first property. Cron syntax defining a schedule in UTC time.
  displayName: string # Optional friendly name given to a specific schedule.
  branches: # Branch names to include or exclude for triggering a run.
    include: [ string ] # List of items to include.
    exclude: [ string ] # List of items to exclude.
  always: boolean # Whether to always run the pipeline or only if there have been source code or pipeline settings changes since the last successful scheduled run. The default is false.

引用此定义的定义:计划

性能

cron 字符串。 必需为第一个属性。
Cron 语法,以 UTC 时间定义计划。

displayName 字符串。
为特定计划提供可选友好名称。

branches includeExcludeFilters
要包含或排除的分支名称以触发运行。

batch 布尔
batch 属性配置是否在之前计划的运行正在进行时运行管道。 当 batchtrue时,如果以前的管道运行仍在进行中,则由于计划,新的管道运行不会启动。 默认值为 false

batch 属性受 always 属性设置的影响。 always true时,即使 batchtrue 且运行正在进行中,管道也按 cron 计划运行。

始终 批次 行为
false false 仅当上次成功的计划管道运行发生更改时,管道才会运行。
false true 仅当上次成功的计划管道运行发生更改且没有正在进行的计划管道运行时,管道才会运行。
true false 管道根据 cron 计划运行。
true true 即使有正在进行的运行,管道也根据 cron 计划运行。

always 布尔
是始终运行管道,还是仅当自上次成功计划运行以来源代码发生更改时才运行管道;默认值为 false。

注解

如果未指定计划触发器,则不会发生计划生成。

注释

如果为 branches指定 exclude 子句而不指定 include 子句,则它等效于在 include 子句中指定 *

重要

使用管道设置 UI 定义的计划触发器优先于 YAML 计划触发器。

如果 YAML 管道同时具有 YAML 计划触发器和 UI 定义的计划触发器,则仅运行定义的计划触发器的 UI。 若要在 YAML 管道中运行 YAML 定义的计划触发器,必须删除管道设置 UI 中定义的计划触发器。 删除所有 UI 计划触发器后,必须进行推送,以便 YAML 计划触发器开始评估。

若要从 YAML 管道中删除 UI 计划触发器,请参阅 UI 设置替代 YAML 计划触发器

Build.CronSchedule.DisplayName 变量

当管道由于 cron 计划触发器而运行时,预定义的 Build.CronSchedule.DisplayName 变量包含触发管道运行的 cron 计划的 displayName

YAML 管道可能包含多个 cron 计划,你可能希望管道基于哪个 cron 计划运行不同的阶段或作业。 例如,你有一个夜间生成和每周生成,并且你想要仅在夜间生成期间运行特定阶段。 可以使用作业或阶段条件中的 Build.CronSchedule.DisplayName 变量来确定是运行该作业还是阶段。

- stage: stage1
  # Run this stage only when the pipeline is triggered by the 
  # "Daily midnight build" cron schedule
  condition: eq(variables['Build.CronSchedule.DisplayName'], 'Daily midnight build')

有关更多示例,请参阅以下 示例 部分。

例子

以下示例定义了两个计划。

第一个计划,每日午夜生成,仅在自上次成功计划运行以来代码已更改时,每天午夜运行管道。 它为 main 和所有 releases/* 分支运行管道,但除 releases/ancient/*下这些分支外。

第二个计划,每周星期天生成,在周日中午运行管道的所有 releases/* 分支。 无论代码自上次运行以来是否已更改,它都会执行此作。

schedules:
- cron: '0 0 * * *'
  displayName: Daily midnight build
  branches:
    include:
    - main
    - releases/*
    exclude:
    - releases/ancient/*
- cron: '0 12 * * 0'
  displayName: Weekly Sunday build
  branches:
    include:
    - releases/*
  always: true

若要根据计划触发器是否计划阶段或作业有条件地运行阶段或作业,请在条件中使用 Build.CronSchedule.DisplayName 变量。 在此示例中,仅当管道由 Daily midnight build 计划触发时,stage1 运行,并且仅当管道由 Weekly Sunday build 计划触发时 job3 运行。

stages:
- stage: stage1
  # Run this stage only when the pipeline is triggered by the 
  # "Daily midnight build" cron schedule
  condition: eq(variables['Build.CronSchedule.DisplayName'], 'Daily midnight build')
  jobs:
  - job: job1
    steps:
    - script: echo Hello from Stage 1 Job 1

- stage: stage2
  dependsOn: [] # Indicate this stage does not depend on the previous stage
  jobs:
  - job: job2
    steps:
    - script: echo Hello from Stage 2 Job 2
  - job: job3 
    # Run this job only when the pipeline is triggered by the 
    # "Weekly Sunday build" cron schedule
    condition: eq(variables['Build.CronSchedule.DisplayName'], 'Weekly Sunday build')
    steps:
    - script: echo Hello from Stage 2 Job 3

另请参阅

  • 详细了解 计划触发器
  • 详细了解一般 触发器以及如何指定触发器。