This is a declarative Jenkins-Pipeline.
With the Lockable Resources Plugin (https://plugins.jenkins.io/lockable-resources/) I want to lock multiple stages dynamically, depending on the environment which was choosed by the user in the parameters section. This is how I wanted this to be done:
pipeline {
parameters {
choice choices: ['---', 'prod', 'test'], description: 'Environment', name: 'environment'
}
stage('MY_APPLICATION') {
options{
lock('resource': "${params.environment}")
}
stages {
stage('TEST') {
when { expression { "${params.environment}" == 'prod' } }
steps { ... }
}
stage('PROD') {
when { expression { "${params.environment}" == 'test' } }
steps { ... }
}
}
}
}
But I have no access to the params in the options block, it always uses the default value. Does anyone have a idea how to lock resources dynamically depending on environment variables?
CodePudding user response:
I managed to solve it like that:
In the options block, there is the $currentBuild variable available and that way it is possible to lock the resource dynamically:
pipeline {
parameters {
choice choices: ['---', 'prod', 'test'], description: 'Environment', name: 'environment'
}
stage('MY_APPLICATION') {
options{
lock('resource': "${currentBuild.getRawBuild().getEnvironment(TaskListener.NULL).environment}")
}
stages {
stage('TEST') {
when { expression { "${params.environment}" == 'prod' } }
steps { ... }
}
stage('PROD') {
when { expression { "${params.environment}" == 'test' } }
steps { ... }
}
}
}
}