stage.stage 定義

階段是相關作業的集合。 根據預設,階段會循序執行。 除非另有透過 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: string # 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.
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.

參考此定義的定義: 階段

屬性

stage 字串。 作為第一個屬性的必要專案。
階段的識別碼。

displayName 字串。
階段的人類可讀取名稱。

pool集區
除非另有指定,否則此階段中的作業將會執行所在的集區。

dependsOn string |字串清單。
必須在此階段之前完成的任何階段。 根據預設,階段會依管線中定義的順序循序執行。 dependsOn: []如果階段不應該相依于管線中的上一個階段,請指定階段。

condition 字串。
評估此條件運算式,以判斷是否要執行此階段。

variables變數
階段特定的變數。

jobs作業
組成階段的作業。

lockBehavior 字串。
此階段的行為鎖定要求應該會與其他獨佔鎖定要求相關。 循序 |runLatest。

templateContext templateCoNtext。
擴充範本時,從管線傳遞的階段相關資訊。 如需 的詳細資訊 templateContext ,請參閱 擴充 YAML 管線範本現在可以傳遞階段、作業和部署和範本的內容資訊- 使用 templateCoNtext 將屬性傳遞至範本

備註

使用 核准檢查 以手動控制何時應該執行階段。 這些檢查通常用來控制生產環境部署。

檢查是 資源擁有者可用的機制。 可用來控制管線中的階段何時取用資源。 身為環境等資源的擁有者,您可以定義取用資源的階段可以啟動之前所需的檢查。

目前,環境支援手動核准檢查。 如需詳細資訊,請參閱 核准

獨佔鎖定

在 YAML 管線中,會使用檢查來控制 受保護資源上的階段執行。 您可以使用的其中一個常見檢查是 獨佔鎖定檢查。 這項檢查只允許從管線執行單一執行。 當多次執行嘗試同時部署到環境時,檢查會取消所有舊的執行,並允許部署最新的執行。

您可以使用 屬性來設定獨佔鎖定檢查 lockBehavior 的行為,其具有兩個值:

  • runLatest - 只有最新的執行會取得資源的鎖定。 如果未 lockBehavior 指定 ,則為預設值。
  • sequential - 所有執行都會循序取得受保護資源的鎖定。

當您的版本是累積的,且包含先前執行的所有程式碼變更時,取消舊執行是很好的方法。 不過,有些管線中的程式碼變更不會累積。 藉由設定 lockBehavior 屬性,您可以選擇允許所有執行依序繼續並部署至環境,或保留取消舊執行並只允許最新執行先前的行為。 的值 sequential 表示所有執行都會循序取得受保護資源的鎖定。 的值 runLatest 表示只有最新的執行會取得資源的鎖定。

若要搭配 sequential 部署或 runLatest 使用獨佔鎖定檢查,請遵循下列步驟:

  1. 在環境 (或其他受保護的資源上啟用獨佔鎖定檢查) 。
  2. 在管線的 YAML 檔案中,指定名為 lockBehavior 的新屬性。 這可以指定給整個管線或指定階段:

在階段上設定:

stages:
- stage: A
  lockBehavior: sequential
  jobs:
  - job: Job
    steps:
    - script: Hey!

在管線上設定:

lockBehavior: runLatest
stages:
- stage: A
  jobs:
  - job: Job
    steps:
    - script: Hey!

範例

此範例會依序執行三個階段。 中間階段會平行執行兩個作業。

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

另請參閱

深入瞭解 階段條件變數