| Any news on this issue? I am having the same problem. (declarative mulitbranch pipeline, Jenkins Version 2.176, Lockable Resources Plugin 2.5) I am trying to dynamically determine the name of the resource which should be locked in an pipeline step before locking the resource. As far as I understand the problem, this is not an issue of using an environment variable but a timing issue. The lockable resources plugin seems to determine the value/name of the resource/label to be locked at the beginning of the pipeline. If the value/name of the resource/label to be locked is created after the start of the pipeline and before executing the lock the value passed to the lockable resources plugin has no effect. Here an example that shows the issue: Pipeline:
def resourceName = 'resource1'
pipeline {
agent any
stages {
stage('change resource') {
steps {
echo "resourceName before update: ${resourceName}"
script{
resourceName = 'resource2'
}
echo "resourceName before update: ${resourceName}"
}
}
stage('lock resource') {
options {
lock resource: "${resourceName}", variable: 'RESOURCE_NAME'
}
steps {
echo "resourceName after lock: ${resourceName}"
echo "env.RESOURCE_NAME after lock: ${env.RESOURCE_NAME}"
}
}
}
}
Console Output:
[Pipeline] Start of Pipeline
[Pipeline] node
Running on BuildSlave
[Pipeline] {
[Pipeline] stage
[Pipeline] { (change resource)
[Pipeline] echo
resourceName before update: resource1
[Pipeline] script
[Pipeline] {
[Pipeline] }
[Pipeline] // script
[Pipeline] echo
resourceName before update: resource2
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (lock resource)
[Pipeline] lock
Trying to acquire lock on [resource1]
Lock acquired on [resource1]
[Pipeline] {
[Pipeline] echo
resourceName after lock: resource2
[Pipeline] echo
env.RESOURCE_NAME after lock: resource1
[Pipeline] }
Lock released on resource [resource1]
[Pipeline] // lock
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
Can you change the lockable resource plugin so that the name of the resource is determined when the plugin is executed and not at the beginning of the pipeline? |