階段是相關作業的集合。 根據預設,階段會循序執行。 除非透過 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.
參考此定義的定義:階段
性能
stage 字串。 做為第一個屬性的必要屬性。
階段的標識碼。
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使用獨佔鎖定檢查,請遵循下列步驟:
- 在環境上啟用獨佔鎖定檢查(或其他受保護的資源)。
- 在管線的 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 資源群組的階段,您可能會想要防止多個管線同時更新相同的資源群組。 目前,若要達成此目標,需要使用 Proxy 資源,例如環境,並將獨佔鎖定檢查放在該環境上。 這種方法可能很耗時、增加複雜度,以及增加維護工作。
如果您需要確保一次只有單一管線執行可以存取階段,您可以在階段層級指定獨佔鎖定。 如果您有具有標識碼的階段並指定其 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