rp346
rp346

Reputation: 7068

jenkins pipeline stop concurrent builds on repo

I have Multi branch build Jenkins pipeline which uses docker containers. The issue I am having is I don't want more than one branch to triggered the build at a time. Because I use postgres DB in a container and when more than one branch starts localhost port 5432 gets occupied with branch build which kicks in first then second branch fails.

Is there a way to avoid this in Jenkinsfile or any other way ?

Upvotes: 0

Views: 409

Answers (2)

Ram
Ram

Reputation: 1224

pipeline { 
options { lock resource: 'build-lock' } 
stages {...} 
}

use this in your pipelines. At any given point in time, only one instance of the pipeline will execute, even in a multi-branch pipeline.

For more info Lockable resources

Upvotes: 2

Sam
Sam

Reputation: 2882

I would perhaps tackle your postgres db rather than try and solve it this way. Could your build pick a random port and spin up the required db on alt ports instead?

If you do want to try limit concurrent builds...

1) You could limit the agent this repo runs on and provide it only 1 executor. This would cause builds to queue whilst they waited

2) If you wanted to do it programmatically, you would need to put in a check in the pipeline to abort/wait the build if it finds current executions that match.. I don't recommend this, if your running in sandbox you will likely need to approve script access. Plus it seems like you would be digging under the hood and it might cause issues with upgrade paths if the interface gets refactored... but you would be digging around in https://javadoc.jenkins-ci.org/hudson/model/Executor.html getCurrentExecutable() or perhaps something like this https://github.com/cloudbees/jenkins-scripts/blob/master/get-build-information.groovy#L24

Upvotes: 1

Related Questions