How does `lock`, `milestone` and `disableConcurrentBuilds` help with concurrency?
AdvancedConceptJenkins
Answer
lock('staging-env') (Lockable Resources plugin) serialises access to a shared resource, so two pipelines do not deploy to staging simultaneously. milestone() makes older builds abort when a newer build passes the same milestone, so you never deploy an older commit over a newer one. disableConcurrentBuilds(abortPrevious: true) is the simple version for a whole job.
stage('Deploy staging') {
steps {
milestone(10)
lock(resource: 'staging', inversePrecedence: true) {
sh './deploy.sh staging'
}
}
}
More on Jenkins
What is Jenkins and how is its architecture organised?BeginnerWhat are executors, nodes and labels in Jenkins?IntermediateWhy should you not run builds on the Jenkins controller?BeginnerHow do inbound (JNLP/WebSocket) and SSH agents connect to the controller?IntermediateWhat is a Jenkinsfile and where does it live?BeginnerDeclarative versus Scripted Pipeline: what are the differences?Intermediate