I want not to allow two jobs of the same type (same repository) not to run in parallel on the same node.
How can I do this using groovy inside Jenkinsfile?
I want not to allow two jobs of the same type (same repository) not to run in parallel on the same node.
How can I do this using groovy inside Jenkinsfile?
The answer provided in https://stackoverflow.com/a/43963315/6839445 is deprecated.
The current method to disable concurrent builds is to set options:
Detailed description is available here: https://jenkins.io/doc/book/pipeline/syntax/#options
You got at the disableConcurrentBuilds property:
Then the job would wait the older one to finish first
Example using options block in the declarative pipeline syntax:
Install Jenkins Lockable Resources Plugin.
In your pipeline script wrap the part in the lock block and give this lockable resource a name.
Use the name of whatever resource you are locking. In my experience its usually a test server or test database.
I think there are more than just one approach to this problem.
Pipeline
lock
step, as suggested in other answer.Execute concurrent builds if necessary
.node
orlabel
for each project.Jenkins
1
?Plug-ins
The "Throttle Concurrent Builds Plugin" now supports pipeline since
throttle-concurrents-2.0
. So now you can do something like this:From the pipeline stage view you'll be able to appreciate this:
However, please be advised that this only works for
node
blocks within thethrottle
block. I do have other pipelines where I first allocate a node, then do some work which doesn't need throttling and then some which does.In this case the
throttle
step doesn't solve the problem because thethrottle
step is the one inside thenode
step and not the other way around. In this case the lock step is better suited for the task