不并行执行的CloudBuild作业

时间:2019-11-12 03:23:00

标签: google-cloud-build

我正在学习CloudBuild,并且了解可以使用waitFor影响构建的运行顺序。 job1包含一些睡眠时间以模拟长时间运行的工作。 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模拟一个长时间运行的作业,我当时希望作业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

1 个答案:

答案 0 :(得分:1)

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

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

  

如果没有为waitFor提供任何值,那么在运行之前,构建步骤将等待构建请求中的所有先前构建步骤成功完成。

     

要在构建时立即运行构建步骤,请在waitFor中使用-   字段。

     

步骤字段中构建步骤的顺序与顺序相关   在其中执行步骤。步骤将依次运行或   同时基于其waitFor中定义的依赖项   字段。

在文档中,还有一个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是十。