jenkinsci / jenkinsci/lockable-resources-plugin
Conditional stage lock in declarative pipeline
- Dominant language
- Java
- Stars
- 99
- Forks
- 205
- Avg merge
- 2d 13h
- Merged PRs (30d)
- 8
Description
I'm looking for a way to conditionally lock a stage in declarative pipeline.
Using scripted pipeline, it's quite easy to wrap the lock call in a conditional:
```groovy
def lockIf(condition, name, closure) {
if (condition) {
lock(name, closure)
} else {
closure.call()
}
}
lockIf(env.BRANCH_NAME == "master", "myResource") {
stage ("Run") {
// ...
}
}
````
However, in declarative I lack that flexibility as I can only achieve something like that in the `steps` scope, when I'd like to run an entire stage (and its sub-stages) in a lock.
My current workaround is to lock on a conditionally unique string, though this has a side-effect of creating a lot of lockable resources which I need to clean up:
```groovy
stage ('Run') {
options {
lock resource: "${env.BRANCH_NAME == 'master' ? "myResource" : "${env.BUILD_TAG}"}"
}
// ...
}
```
Am I overlooking a way to conditionally lock? If not, would you be willing to accept a contribution that makes it possible to pass a boolean to `lock`, which skips locking behaviour when it's false? e.g.:
```groovy
stage ('Run') {
options {
lock resource: "myResource", condition: env.BRANCH_NAME == "master"
}
// ...
}
```
Contributor guide
Assessment
This issue has not been assessed yet.