CloudBuild 作业未并行执行

CloudBuild jobs not executed in parallel

我正在学习 CloudBuild 并了解我可以使用 waitFor 来影响我的构建 运行 的顺序。 job1 包括一些睡眠时间来模拟长时间的 运行ning 工作。 job2 只是回应了一些东西。 done 等待 job1job2。所以我创建了一个这样的测试版本:我有一个 package.json

{
  "scripts": {
    "job1": "echo \"[job1] Starting\" && sleep 5 && echo \"[job1] ...\" && sleep 2 && echo \"[job1] Done\" && exit 0",
    "job2": "echo \"[job2] Hello from NPM\" && exit 0",
    "done": "echo \"DONE DONE DONE!\" && exit 0"
  },
}

作业 1 模拟了一个很长的 运行ning 作业,我希望作业 2 可以并行执行。但似乎输出显示它不是。 CloudBuild 运行 一次只能执行 1 个步骤吗?

cloudbuild.yaml

steps:
- name: 'gcr.io/cloud-builders/npm'
  args: ['run', 'job1']
  id: 'job1'
- name: 'gcr.io/cloud-builders/npm'
  args: ['run', 'job2']
  id: 'job2'
- name: 'gcr.io/cloud-builders/npm'
  args: ['run', 'done']
  waitFor: ['job1', 'job2']

输出

Operation completed over 1 objects/634.0 B.                                      
BUILD
Starting Step #0 - "job1"
Step #0 - "job1": Already have image (with digest): gcr.io/cloud-builders/npm
Step #0 - "job1": 
Step #0 - "job1": > learn-gcp@1.0.0 job1 /workspace
Step #0 - "job1": > echo "[job1] Starting" && sleep 5 && echo "[job1] ..." && sleep 2 && echo "[job1] Done" && exit 0
Step #0 - "job1": 
Step #0 - "job1": [job1] Starting
Step #0 - "job1": [job1] ...
Step #0 - "job1": [job1] Done
Finished Step #0 - "job1"
Starting Step #1 - "job2"
Step #1 - "job2": Already have image (with digest): gcr.io/cloud-builders/npm
Step #1 - "job2": 
Step #1 - "job2": > learn-gcp@1.0.0 job2 /workspace
Step #1 - "job2": > echo "[job2] Hello from NPM" && exit 0
Step #1 - "job2": 
Step #1 - "job2": [job2] Hello from NPM
Finished Step #1 - "job2"
Starting Step #2
Step #2: Already have image (with digest): gcr.io/cloud-builders/npm
Step #2: 
Step #2: > learn-gcp@1.0.0 done /workspace
Step #2: > echo "DONE DONE DONE!" && exit 0
Step #2: 
Step #2: DONE DONE DONE!
Finished Step #2
PUSH
DONE

如果您希望 job2job1 同时执行,您应该添加行

waitFor: ['-'] 在您的 cloudbuild.yaml 中,紧接在 job2 之后。正如 official documentation:

中所述

If no values are provided for waitFor, the build step waits for all prior build steps in the build request to complete successfully before running.

To run a build step immediately at build time, use - in the waitFor field.

The order of the build steps in the steps field relates to the order in which the steps are executed. Steps will run serially or concurrently based on the dependencies defined in their waitFor fields.

在文档中,还有一个 example 关于如何 运行 并行执行两个作业的 example

如果你想让 job2 与 job1 一起 运行 你应该有这样的东西:

steps:

- name: 'gcr.io/cloud-builders/npm'
  args: ['run', 'job1']
  id: 'job1'
- name: 'gcr.io/cloud-builders/npm'
  args: ['run', 'job2']
  id: 'job2'
  waitFor: ['-']  # The '-' indicates that this step begins immediately.
- name: 'gcr.io/cloud-builders/npm'
  args: ['run', 'done']
  waitFor: ['job1', 'job2']

另请注意,您可以使用的 maximum number of concurent builds 是十个。