@Chadi's answer is certainly correct and the Lockable Resources Plugin seems the only easy workaround as for now. The Jenkins documentation(Example 22. beforeOptions) shows an example that is very similar to what you want to do. Here another possible solution (not tested):
pipeline {
agent none
stages {
stage('build') {
stages {
stage('on master') {
when {
beforeOptions true
branch 'master'
}
options {
lock label: 'master_build'
}
steps {
...
}
}
stage('not on master') {
when {
not {
branch 'master'
}
}
steps {
...
}
}
}
}
}
}
I have used nested stages to group them in the same stage but it is not mandatory, of course. The benefit I see are:
- Locking now belongs to the stage
options
and the steps
declaration contains steps and nothing else
- The whole stage is locked and you do not risk to put some statements outside of the
lock
block
- Personally, too many indentation levels reduce the readability, so if I can get rid of even one of them I am more than happy to do it.
The disadvantage is that you have to add a couple of lines and the beforeOptions
, necessary to evaluate the when
condition before options
.