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 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 changes since the last successful scheduled run; the default is false.

引用此定义的定义: 计划

属性

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

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

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

batch布尔值
如果先前计划的运行正在进行,是否运行管道;默认值为 false。 这与管道存储库的版本无关。

下表介绍了 和 batch 的交互方式always

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

重要

当 为 truealways,管道根据 cron 计划运行,即使 batchtrue

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

注解

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

注意

如果指定 exclude 子句而不包含 branchesinclude 子句,则等同于在 include 子句中指定 *

重要

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

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

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

Build.CronSchedule.DisplayName 变量

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

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 变量。 在此示例中, stage1 仅当管道由 Daily midnight build 计划触发时运行,仅 job3 当管道由 Weekly Sunday build 计划触发时才运行。

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

请参阅