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

w1jd8yoj  于 2023-10-17  发布在  Jenkins
关注(0)|答案(3)|浏览(216)

我正在将我的声明式Jenkins管道切换到脚本化Jenkins管道。然而,根据Jenkins文档,我之前使用的禁用ConcurrentBuilds()的“选项”方向似乎不适用于脚本管道。
我在SO上看到了一些使用资源锁定的建议,但我想知道是否有一种更干净,更直接的方法来防止脚本管道的Jenkinsfile中的并发构建?

js4nwp54

js4nwp541#

你有没有从你的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()
])

你能试试看能不能用吗?
你可以在你的Jenkins文件中的Node后面嵌入属性部分:

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

fd3cxomn2#

我也遇到过同样的问题。我使用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>
xmjla07d

xmjla07d3#

你可以这样使用

node {
    // This limits build concurrency to 1 per branch
    properties([disableConcurrentBuilds()])
  
    //do stuff
    ...
}

相关问题