Jenkins pipeline: How to force a build to wait for previous build to finish

Viewed 1586

I have a Jenkins pipeline that automatically builds the latest code from a Git repository whenever there's a code update. The problem is that sometimes there are 2 or more code updates happening at (almost) the same time, which causes 2 builds to run in parallel.

I assume this won't happen if I set the number of executors in my node to 1, but I don't want to do that.

Is there any way to make sure a build only starts when previous build (of the same job/pipeline) is finished?

2 Answers

Yes, you can use disableConcurrentBuilds() as given below in your Jenkins file:-

options {
            // Disallow concurrent executions of the Pipeline. Can be useful
            // for preventing simultaneous accesses to shared resources, etc.
            disableConcurrentBuilds()
        }

For scripted (not declarative) pipelines:

properties (
    [disableConcurrentBuilds()]
)
Related