Jenkins脚本化管道可以禁用并发构建吗?

A. *_*key 3 jenkins jenkins-pipeline

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

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

Unf*_*631 7

您是否从您的jenkins服务器调查了代码片段生成器?地址应该像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()
])
Run Code Online (Sandbox Code Playgroud)

您可以尝试一下并检查是否可行吗?

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

node {
    properties([
            buildDiscarder(
                    logRotator(..........same snippet as above..
Run Code Online (Sandbox Code Playgroud)


Krz*_*łek 1

我遇到了同样的问题。我正在使用 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' {}
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

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

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

<?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>
Run Code Online (Sandbox Code Playgroud)