我有一个具有以下结构的CircleCI作业。
jobs:
test:
steps:
- checkout
- run #1
...<<install dependencies>>
- run #2
...<<execute server-side test>>
- run #3
...<<execute frontend test 1>>
- run #4
...<<execute frontend test 2>> 我想先执行步骤1,然后并行执行步骤2-4.
1、2、3和4分别占4 min、1 min、1 min和1 min左右。
我尝试将步骤拆分到不同的作业,并使用工作空间将已安装的工件从#1传递到#2-4。但是,由于工件的尺寸很大,大约需要2分钟。若要持久化&附加工作区,则拆分作业的优势被取消。
有没有一种聪明的方法可以并行运行#2-4,而不需要大量的开销?
发布于 2022-03-05 12:36:04
如果希望并行运行这些命令,则需要将这些命令移动到新作业中,否则,CircleCI将遵循步骤的结构,只有在最后一个步骤完成时才运行这些命令。让我给你举个例子。我创建了一个包含4个作业的基本配置。
npm installtest1 (它将与test2同时运行),但只有在npm install完成时才能运行test2 (它将与test1同时运行),但只有在npm install完成时才能运行deploy (只在完成两个测试之后才会运行)基本上,您需要在作业之间拆分命令,并根据需要设置依赖项。
请参阅我的配置文件:
version: 2.1
jobs:
install_deps:
docker:
- image: circleci/node:14
steps:
- checkout
- setup_remote_docker:
docker_layer_caching: true
- run: echo "running npm install"
- run: npm install
- persist_to_workspace:
root: .
paths:
- '*'
test1:
docker:
- image: circleci/node:14
steps:
- checkout
- setup_remote_docker:
docker_layer_caching: true
- attach_workspace:
at: .
- run: echo "running the first test and also will run the test2 in parallel"
- run: npm test
test2:
docker:
- image: circleci/node:14
steps:
- checkout
- setup_remote_docker:
docker_layer_caching: true
- attach_workspace:
at: .
- run: echo "running the second test in parallel with the first test1"
- run: npm test
deploy:
docker:
- image: circleci/node:14
steps:
- checkout
- setup_remote_docker:
docker_layer_caching: true
- attach_workspace:
at: .
- run: echo "running the deploy job only when the test1 and test2 are finished"
- run: npm run build
# Orchestrate our job run sequence
workflows:
test_and_deploy:
jobs:
- install_deps
- test1:
requires:
- install_deps
- test2:
requires:
- install_deps
- deploy:
requires:
- test1
- test2现在看上面的逻辑,install_dep将在没有依赖项的情况下运行,但是test1和test2在install_dep完成之前不会运行。
而且,在两个测试完成之前,deploy都不会运行。
我已经运行了这个配置,在第一个映像中,我们可以看到其他作业正在等待第一个任务完成,在第二个映像中,我们可以看到两个测试并行运行,而deploy作业正在等待它们完成。在第三个图像中,我们可以看到deploy作业正在运行。



https://stackoverflow.com/questions/71285551
复制相似问题