将管道的各个部分作为单独的作业运行 [英] Run Parts of a Pipeline as Separate Job
问题描述
我们正在考虑将Jenkins Pipeline插件用于一个相当复杂的项目,该项目由多个交付组成,这些交付需要在合并之前使用不同的工具(在不同的机器上)构建.不过,使用单个Jenkinsfile
进行完整的构建似乎很容易,而且我喜欢自动发现Pipeline附带的git分支.
We're considering using the Jenkins Pipeline plugin for a rather complex project consisting of several deliveries that need to be build using different tools (on different machines) before being merged. Still, it seems to be easy enough to do a complete build with a single Jenkinsfile
, and I like the automatic discovery of git branches that comes with Pipeline.
但是,在这一点上,我们为每个交货都有工作,并使用基于构建流程的元"工作来协调各个工作.这样做的好处是,如果只进行了很小的更改,它也允许只开始一项单独的工作,以查看交付是否仍然可以编译.
However, at this point, we have jobs for each of the deliveries and use a build-flow based "meta" job to orchestrate the individual jobs. The nice thing about this is that it also allows starting just one individual job if only small changes were made, just to see whether this delivery still compiles.
为了模仿这一点,想到了一些想法:
To emulate this, some ideas came to mind:
- 对交付使用不同的
Jenkinsfile
,并在顶级Jenkinsfile
中使用load
;似乎Multibranch Pipeline作业不允许配置Jenkinsfile
来使用( https: //issues.jenkins-ci.org/browse/JENKINS-35415 ),因此为各个交货创建工作仍处于开放状态. - 为顶级"作业提供配置选项,并且
Jenkinsfile
中的所有交货均具有if
,以便能够选择应构建的内容.但是,这会在一个管道中混合使用不同的构建类型,并且至少会弄乱构建时间的估算.
- Use different
Jenkinsfile
s for the deliveries andload
them in the top-levelJenkinsfile
; it seems that the Multibranch Pipeline job does not allow configuring theJenkinsfile
to use yet (https://issues.jenkins-ci.org/browse/JENKINS-35415), however, so creating the jobs for the individual deliveries is still open. - Provide a configuration option for the "top-level" job and have
if
s for all deliveries in theJenkinsfile
to be able to select which should be build. This would mix different build types in one pipeline, though, and, at the very least, mess up the estimation of the build time.
这些可行的选择,还是有更好的选择?
Are those viable options, or is there a better one?
推荐答案
您可以做的是编写一个流水线脚本,该脚本在单个阶段周围具有"if"防护,例如:
What you could do is to write a pipelining script that has has "if"-guards around the single stages, like this:
stage "s1"
if (theStage in ["s1","all"]) {
sleep 2
}
stage "s2"
if (theStage in ["s2", "all"]) {
sleep 2
}
stage "s3"
if (theStage in ["s3", "all"]) {
sleep 2
}
然后,您可以通过将参数"theStage"设置为"all"来执行使用此脚本并一次运行所有阶段的主"作业.当所有阶段同时运行时,此作业将收集统计信息,并为您提供有用的估计时间.
Then you can make a "main" job that uses this script and runs all stages at once by setting the parameter "theStage" to "all". This job will collect the statistics when all stages are run at once and give you useful estimation times.
此外,您可以创建一个部分运行"作业,该作业使用此脚本,并且已在要运行的阶段进行了参数设置.不过,该估算将不会很有用.
Furthermore, you can make a "partial run" job that uses this script and that is parametrized with the stage that you want to run. The estimation will not be very useful, though.
请注意,正如Martin Ba所建议的,我将阶段本身放在主脚本中,仅将执行代码放入条件脚本中.这样可以确保工作的可视化更可靠
Note that I put the stage itself to the main script and put only the execution code into the conditional, as suggested by Martin Ba. This makes sure that the visualization of the job is more reliable
这篇关于将管道的各个部分作为单独的作业运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!