Jenkins Declarative Pipeline: Lock multiple stages using NODE_NAME as resource.

1,907 views
Skip to first unread message

Dusan Nikolov

unread,
Mar 7, 2019, 6:58:59 PM3/7/19
to Jenkins Users
Hi, I'm trying to lock multiple stages in a pipeline and would like to use NODE_NAME as the resource. Purpose is to allow multiple parallel executions of the pipeline on different available nodes, but to make a node unavailable while it's used by one running pipeline instance.
Now, the trick is that pipeline needs to be setup to run on two nodes at the same time (please see example below).
I've tried setting it up by using lock in options block of the surrounding stage, but since options gets evaluated/executed before agent is assigned to a stage, this fails to find NODE_NAME.

pipeline {
agent none
stages {
stage ('main') {
parallel {
stage ('server') {
agent { label 'server' }
options { lock("${NODE_NAME}")} // This doesn't work. NODE_NAME not available
steps {
script {
echo "${NODE_NAME}"
sleep(10)
}
}
}
stage ('client') {
agent { label 'client' }
options { lock("${NODE_NAME}")} // This doesn't work. NODE_NAME not available
steps {
script {
echo "${NODE_NAME}"
sleep(10)
}
}
}
}
}
}
}

Mark Lübbehüsen

unread,
Mar 11, 2019, 8:58:00 AM3/11/19
to Jenkins Users
Hi,

after downloading the Lockable Resource Plugin in Jenkins, you have to configure it in the system configuration of jenkins.

Dusan Nikolov

unread,
Mar 11, 2019, 9:56:58 PM3/11/19
to Jenkins Users
Hi, what do you mean configure the plugin? 

If you're referring to adding lockable resources, that's not necessary -  if a resource doesn't exist it will be created on the fly (which I already confirmed).
The problem I'm having is described in this thread exactly: https://issues.jenkins-ci.org/browse/JENKINS-43336

monger_39

unread,
Apr 5, 2019, 9:11:19 AM4/5/19
to jenkins...@googlegroups.com
I have done something similar, albeit in a scripted pipeline.
Install the "lockable resources" plugin, and define as resources each of the nodes that could execute your job.
I.e. some name that includes the name of your node.
Assign a label (same label for all those resources) to these resources.
Within your job, request a lock() on the label. Once a lock is set, derive the name of the node from the name
of the locked resource, then do the rest ...

gr, M

--
You received this message because you are subscribed to the Google Groups "Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jenkinsci-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jenkinsci-users/67e3c848-aa2f-4a92-9551-d4def5a93fe2%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages