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
屬性會設定是否要執行管線。 當 batch
true
時,如果先前的管線執行仍在進行中,新的管線執行將不會因為排程而啟動。 預設值為 false
。
batch
屬性會受到 always
屬性的設定所影響。 當 always
true
時,管線會根據cron排程執行,即使 batch
true
且執行中也一起執行。
永遠 | 批次 | 行為 |
---|---|---|
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