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?
Disable concurrent builds in Jenkins declarative pipeline. Define disableConcurrentBuilds option to disallow concurrent executions.
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.
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.
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.
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..
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With