13

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?

A. Dickey
  • 141
  • 1
  • 1
  • 6
  • Why are you changing to scripted pipeline? Declarative pipeline is generally considered better. – Andrew Gray Oct 19 '18 at 03:25
  • 1
    I'm looking to add in-line testing for the Jenkins pipeline code and groovy shared libraries used, which is where most of the business logic is. The scripted structure is more suited to the style of testing I'm going for. – A. Dickey Oct 22 '18 at 13:40

2 Answers2

18

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..
Unforgettable631
  • 940
  • 6
  • 10
1

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>