问题描述
我如何创建一个跨越多个阶段的CI作业来改善并行性?
How can I create a CI job that spans more than one stage, to improve parallelism?
如下图所示:
想法是 slow_build
应该早于 build
开始,但是 test
不依赖于此,因此 test
应该能够在 build
完成后立即启动.
The idea is that slow_build
should start as early as build
, but test
doesn't depend on it, so test
should be able to start as soon as build
is done.
(请注意,这是一种简化:每个阶段都有多个并行运行的进程,否则我可以将 build
和 test
捆绑在一起.)
(Note that this is a simplification: each stage has multiple processes running in parallel, otherwise I could just bundle build
and test
together.)
推荐答案
从Gitlab版本12.2开始,这已经成为可能.通过将关键字 needs
添加到依赖于其他作业的作业,各个阶段现在可以同时运行. needs
关键字的完整文档在这里,但是文档中的示例如下: https://docs.gitlab.com/ee/ci/yaml/#needs
This is now possible as of Gitlab version 12.2. By adding the keyword needs
to jobs that depend on other jobs, stages can now run concurrently. The full documentation for the needs
keyword is here, but an example from the docs follows: https://docs.gitlab.com/ee/ci/yaml/#needs
linux:build:
stage: build
mac:build:
stage: build
lint:
stage: test
needs: []
linux:rspec:
stage: test
needs: ["linux:build"]
linux:rubocop:
stage: test
needs: ["linux:build"]
mac:rspec:
stage: test
needs: ["mac:build"]
mac:rubocop:
stage: test
needs: ["mac:build"]
production:
stage: deploy
由于 lint
作业不需要任何内容,因此它可以立即运行, linux:build
和 mac:build
也一样.但是,如果 linux:build
在 mac:build
之前完成,则 linux:rspec
和 linux:rubocop
都可以启动,甚至在 mac:build
和 build
阶段完成之前.
Since the lint
job doesn't need anything, it runs instantly, as does linux:build
and mac:build
. However, if linux:build
finishes before mac:build
then both linux:rspec
and linux:rubocop
can start, even before mac:build
and the build
stage complete.
与往常一样,没有 needs
关键字, production
作业需要先完成所有先前的作业,然后才能开始.
As usual, without the needs
keyword, the production
job requires all previous jobs to complete before it starts.
在管道中使用需求
时,还可以在管道视图中查看作业的有向无环图.有关更多信息,请参见: https://docs.gitlab.com/ee/ci/directed_acyclic_graph/index.html
When using needs
in your pipelines, you can also view a Directed Acyclic Graph of your jobs in the pipeline view. More on that can be found here: https://docs.gitlab.com/ee/ci/directed_acyclic_graph/index.html
这篇关于CI作业跨越多个阶段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!