How to Lock a Resource for Concurrency in Jenkins
The lock step queues builds so only one holds a named resource at a time.
Wrap the critical stage in a lock so concurrent builds wait their turn for a shared environment. The Lockable Resources plugin tracks the named lock across the controller.
Serialize a staging deploy
Lock a named resource around the deploy and smoke-test steps.
Jenkinsfile
pipeline {
agent any
stages {
stage('Deploy to staging') {
steps {
lock(resource: 'staging-env') {
sh './deploy.sh staging'
sh './smoke-test.sh staging'
}
}
}
}
}Notes
- Other builds reaching the same lock wait until the holder releases it, preventing clobbered deploys.
- Use a label and quantity for pools of interchangeable resources instead of a single named lock.
Frequently asked questions
How do I lock a Resource for Concurrency in Jenkins?
Wrap the critical stage in a lock so concurrent builds wait their turn for a shared environment. The Lockable Resources plugin tracks the named lock across the controller.
Serialize a staging deploy?
Lock a named resource around the deploy and smoke-test steps.
Related guides
How to Cache Gradle Dependencies in a Jenkins PipelineCache Gradle dependencies in a Jenkins pipeline by pinning GRADLE_USER_HOME to a persistent path, or using th…
How to Pass a Secret File to a Command in JenkinsExpose a stored Secret file credential as a temporary path with withCredentials in Jenkins, so tools like kub…
How to Build a Multi-Arch Docker Image in a Jenkins PipelineBuild a multi-architecture Docker image in a Jenkins pipeline with docker buildx and QEMU, pushing one amd64…