【问题标题】:Can Jenkins scripted pipeline disable concurrent builds?Jenkins 脚本化管道可以禁用并发构建吗?
【发布时间】:2018-10-18 19:38:03
【问题描述】:

我正在将我的声明式 Jenkins 管道切换到脚本化 Jenkins 管道。但是,根据Jenkins documentation,我之前用于 disableConcurrentBuilds() 的“选项”方向似乎不适用于脚本化管道。

我已经看到一些关于 SO 使用资源锁定的建议,但我想知道是否有更清洁、更直接的方法来防止脚本管道的 Jenkinsfile 中的并发构建?

【问题讨论】:

  • 为什么要改用脚本化管道?声明式管道通常被认为更好。
  • 我希望为 Jenkins 流水线代码和使用的 groovy 共享库添加在线测试,这是大多数业务逻辑所在的地方。脚本结构更适合我要进行的测试风格。

标签: jenkins jenkins-pipeline


【解决方案1】:

您是否从您的詹金斯服务器查看过 sn-p 生成器?地址应该是http://jenkinshost/pipeline-syntax/

这将帮助您了解可用选项(也基于已安装的插件),您可以在这里找到Sample Step: properties: Set job properties 并选中Do not allow concurrent builds 框。点击按钮Generate pipeline script,您应该生成一个如何在脚本化管道作业中使用它的示例:

properties([
        buildDiscarder(
                logRotator(
                        artifactDaysToKeepStr: '', 
                        artifactNumToKeepStr: '', 
                        daysToKeepStr: '', 
                        numToKeepStr: '')
        ), 
        disableConcurrentBuilds()
])

你能试试看是否有效吗?

您可以在 Jenkinsfile 中的节点之后嵌入属性部分:

node {
    properties([
            buildDiscarder(
                    logRotator(..........same snippet as above..

【讨论】:

    【解决方案2】:

    我也遇到了同样的问题。我正在使用 JOB DSL 插件来生成我的 Jenkins 作业,而对于管道,我必须修改生成的 xml。

    static void DisableConcurrentBuilds(context) {
        context.with {
            configure {
                def jobPropertyDescriptors = it / 'actions' / 'org.jenkinsci.plugins.workflow.multibranch.JobPropertyTrackerAction' / 'jobPropertyDescriptors'
                jobPropertyDescriptors << {
                    string('org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty')
                }
                def properties = it / 'properties' << 'org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty' {}
            }
        }
    }
    

    用法:

    pipelineJob('example') {
        DisableConcurrentBuilds(delegate)
        definition {
            cps {
                script(readFileFromWorkspace('project-a-workflow.groovy'))
                sandbox()
            }
        }
    }
    

    由于 DisableConcurrentBuilds,以下条目被添加到管道作业配置中:

    <?xml version="1.0" encoding="UTF-8"?><flow-definition>
        <actions>
            <org.jenkinsci.plugins.workflow.multibranch.JobPropertyTrackerAction>
                <jobPropertyDescriptors>
                    <string>org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty</string>
                </jobPropertyDescriptors>
            </org.jenkinsci.plugins.workflow.multibranch.JobPropertyTrackerAction>
        </actions>
        <properties>
            <org.jenkinsci.plugins.workflow.job.properties.DisableConcurrentBuildsJobProperty/>
        </properties>
        ...
    </flow-definition>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-10
      • 2022-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      相关资源
      最近更新 更多