Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Jenkins scripted pipeline disable concurrent builds?

I am switching my Declarative Jenkins pipeline to a scripted Jenkins pipeline. However, the 'options' direction I was using previously to disableConcurrentBuilds() does not seem to be available for scripted pipelines according to the Jenkins documentation.

I've seen some suggestions on SO for using resource locking, but I was wondering if there was a cleaner, more direct way of preventing concurrent builds in the Jenkinsfile of scripted pipelines?

like image 803
A. Dickey Avatar asked Oct 18 '18 19:10

A. Dickey


People also ask

How do I stop parallel builds in Jenkins?

Disable concurrent builds in Jenkins declarative pipeline. Define disableConcurrentBuilds option to disallow concurrent executions.

Which is better scripted or Declarative pipeline?

Basically, declarative and scripted pipelines differ in terms of the programmatic approach. One uses a declarative programming model and the second uses an imperative programming mode. Declarative pipelines break down stages into multiple steps, while in scripted pipelines there is no need for this.

What's the difference between a Declarative and scripted Jenkins pipeline?

Declarative pipelines break down stages into individual stages that can contain multiple steps. Scripted pipelines use Groovy code and references to the Jenkins pipeline DSL within the stage elements without the need for steps.

How do I allow concurrent builds in Jenkins?

Jenkins allows for parallel execution of builds for a Job. Job configuration page has a check box, "Execute concurrent builds if necessary". Also, in the master node configuration set the "# of executors" field to more than 1. Once these two are done, parallel job execution is enabled.


2 Answers

Have you look into the snippet generator from your jenkins server? Address should be like http://jenkinshost/pipeline-syntax/.

This will help you about available options (also based on installed plugins) and here you can find Sample Step: properties: Set job properties and check the box Do not allow concurrent builds. Click on the button Generate pipeline script and you should generate an example how to use it in your scripted pipeline job:

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

Can you try that and check if that works?

You can embed tthe properties section after your Node in your Jenkinsfile:

node {
    properties([
            buildDiscarder(
                    logRotator(..........same snippet as above..
like image 122
Unforgettable631 Avatar answered Sep 21 '22 14:09

Unforgettable631


I've encountered the same problem. I'm using JOB DSL plugin to generate my Jenkins jobs and for pipelines I had to modify generated 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' {}
        }
    }
}

Usage:

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

As a result of DisableConcurrentBuilds the following entries are added to pipeline job configuration:

<?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>
like image 42
Krzysztof Błażełek Avatar answered Sep 22 '22 14:09

Krzysztof Błażełek