将管道的一部分作为单独的作业运行 [英] Run Parts of a Pipeline as Separate Job

查看:13
本文介绍了将管道的一部分作为单独的作业运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在考虑将 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 进行交付,并在顶级 Jenkinsfileload 它们;似乎多分支管道作业不允许配置 Jenkinsfile 以使用尚未(https://issues.jenkins-ci.org/browse/JENKINS-35415),因此为单个交付创建工作仍然开放.
  • 为顶级"作业提供一个配置选项,并为 Jenkinsfile 中的所有交付提供 if,以便能够选择应该构建哪个.但是,这会在一个管道中混合不同的构建类型,并且至少会打乱构建时间的估计.
  • Use different Jenkinsfiles for the deliveries and load them in the top-level Jenkinsfile; it seems that the Multibranch Pipeline job does not allow configuring the Jenkinsfile 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 ifs for all deliveries in the Jenkinsfile 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"-guards,如下所示:

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆