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

Always Batch 行為
false false 只有在上次成功排程的管線執行有變更時,才會執行管線。
false true 只有在上次成功排程的管線執行有變更時,管線才會執行,而且沒有進行中的排程管線執行。
true false 管線會根據 cron 排程執行。
true true 管線會根據 cron 排程執行。

重要

當 為 truealways ,管線會根據 cron 排程執行,即使 batch 是 亦然 true

always布林值
是否一律執行管線,或只有在自上次排程執行成功後已有原始程式碼變更時;預設值為 false。

備註

如果您未指定任何排程的觸發程式,則不會發生任何排程的組建。

注意

如果您為 指定 exclude 不含 子句的 include 子句 branches ,則相當於在 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

另請參閱