設定建構步驟的順序

您可以指定建構步驟的執行順序。根據預設,建構步驟會按順序執行,但您可以將其設定為並行執行。

本頁面說明如何設定建構步驟的順序。

建構步驟順序與依附元件

使用建構步驟的 waitFor 欄位,指定在執行建構步驟之前,「必須執行」哪些步驟。如果沒有提供 waitFor 的值,建構步驟會等待建構要求中「先前所有」建構步驟都成功完成才會執行。

如要在建構階段「立即」執行建構步驟,waitFor 欄位請使用 -

steps 欄位中的建構步驟順序與執行步驟的順序相關。步驟將根據其 waitFor 欄位中定義的依附元件依序或並行執行。

步驟的順序取決於其 waitFor 中的每個 id,並且在每個依附元件成功完成之前,不會啟動。

沒有選用 waitFor 欄位 (或 waitFor 為空白) 的步驟會等到「所有先前步驟成功完成」之後才執行。因此,如果沒有一個步驟的 id 欄位包含 waitFor,則所有步驟都將「依照定義的順序執行」

waitFor 僅包含 -,步驟便可取決於建構的啟動。透過宣告某一步驟僅取決於 -,該步驟會「在建構啟動時立即執行」。定義的第一個步驟就間接地取決於啟動。

以下程式碼片段示範的建構設定具有兩個依序執行的步驟:

YAML

steps:
- name: foo
- name: bar

JSON

{
    "steps": [{
        "name": "foo"
    },
    {
        "name": "bar"
    }
    ]
}

下列程式碼片段示範的兩個並行步驟皆取決於啟動;第三個步驟會等到前兩個步驟成功完成後才啟動。此並行建構會在建構一開始,就執行步驟 AB。第三個步驟將會間接地等到前兩個步驟完成之後才啟動。這個範例可透過省略未在後續 waitFor 中參照的 id 欄位來簡化。

YAML

steps:
- name: foo
  id: A
- name: bar
  id: B
  waitFor: ['-']
- name: baz

JSON

{
    "steps": [
    {
        "name": "foo",
        "id": "A"
    },
    {
        "name": "bar",
        "id": "B",
        "waitFor": ["-"]
    },
    {
        "name": "baz"
    }
    ]
}

下列程式碼片段示範取決於上一個步驟的並行步驟。步驟 A 會在建構啟動時立即執行。步驟 BC 會在 A 成功完成之後並行執行。請注意,您可以省略步驟 B 中的 idwaitFor 欄位,和步驟 C 中的 id 欄位,而無需變更執行順序。

YAML

steps:
- name: foo
  id: A
- name: bar
  id: B
  waitFor:
  - A
- name: baz
  id: C
  waitFor:
  - A

JSON

{
    "steps": [
    {
        "name": "foo",
        "id": "A"
    },
    {
        "name": "bar",
        "id": "B",
        "waitFor": [
            "A"
        ]
    },
    {
        "name": "baz",
        "id": "C",
        "waitFor": [
            "A"
        ]
    }
    ]
}

範例

以下範例會並行呼叫 gsutilwget 步驟。這些步驟完成之後,會呼叫 ubuntu 步驟。

YAML

steps:
# Download the binary and the data in parallel.
- name: 'gcr.io/cloud-builders/wget'
  args: ['https://example.com/binary']
- name: 'gcr.io/cloud-builders/gsutil'
  args: ['cp', 'gs://$PROJECT_ID-data/rawdata.tgz', '.']
  waitFor: ['-']  # The '-' indicates that this step begins immediately.

# Run the binary on the data, once both are downloaded.
- name: 'ubuntu'
  args: ['./binary', 'rawdata.tgz']

JSON

{
  "steps": [
    {
      "name": "gcr.io/cloud-builders/wget",
      "args": [
        "https://example.com/binary"
      ]
    },
    {
      "name": "gcr.io/cloud-builders/gsutil",
      "args": [
        "cp",
        "gs://$PROJECT_ID-data/rawdata.tgz",
        "."
      ],
      "waitFor": [
        "-"
      ]
    },
    {
      "name": "ubuntu",
      "args": [
        "./binary",
        "rawdata.tgz"
      ]
    }
  ]
}

以下範例會使用 id 欄位識別特定建構步驟。id 中的值可在 waitFor 中使用,藉以定義建構步驟順序:

  • 首先,fetch-resources 步驟會使用 gsutil 從 Cloud Storage 複製本機資源。同時,go 會產生、測試及安裝原始碼。
  • 然後,docker 建構步驟會在其他所有步驟都完成之後建構映像檔。

YAML

steps:
- name: 'gcr.io/cloud-builders/go'
  args: ['generate']
- name: 'gcr.io/cloud-builders/go'
  args: ['test', './...']
- name: 'gcr.io/cloud-builders/go'
  args: ['install', 'mytarget']
  id: 'go-install'

- name: 'gcr.io/cloud-builders/gsutil'
  args: ['cp', '-r', './somefiles', 'gs://my-resource-bucket/somefiles']
  waitFor: ['-']  # The '-' indicates that this step begins immediately.
  id: 'fetch-resources'

- name: 'gcr.io/cloud-builders/docker'
  args: ['build', '-t', 'gcr.io/$PROJECT_ID/mytarget', '.']
  waitFor: ['go-install', 'fetch-resources']

images: ['gcr.io/$PROJECT_ID/mytarget']

JSON

{
  "steps": [
    {
      "name": "gcr.io/cloud-builders/go",
      "args": [
        "generate"
      ]
    },
    {
      "name": "gcr.io/cloud-builders/go",
      "args": [
        "test",
        "./..."
      ]
    },
    {
      "name": "gcr.io/cloud-builders/go",
      "args": [
        "install",
        "mytarget"
      ],
      "id": "go-install"
    },
    {
      "name": "gcr.io/cloud-builders/gsutil",
      "args": [
        "cp",
        "-r",
        "./somefiles",
        "gs://my-resource-bucket/somefiles"
      ],
      "waitFor": [
        "-"
      ],
      "id": "fetch-resources"
    },
    {
      "name": "gcr.io/cloud-builders/docker",
      "args": [
        "build",
        "-t",
        "gcr.io/$PROJECT_ID/mytarget",
        "."
      ],
      "waitFor": [
        "go-install",
        "fetch-resources"
      ]
    }
  ],
  "images": [
    "gcr.io/$PROJECT_ID/mytarget"
  ]
}

後續步驟

傳送您對下列選項的寶貴意見...

這個網頁
Cloud Build