Issue
I usually lock resources in my declarative pipeline with something like:
lock(resource: "MY_RESOURCE") {
// do something
}
but now I have several different resources I could use, is there a way to check if a resource is locked?
I would like to do something like:
myResources = ["RES1", "RES2", "RES3"]
hasResource = false
for (resource in myResources) {
if (hasresource) {
break
}
if (!isLocked(resource)) {
hasresource = true
lock(resource) {
// do something
}
}
}
(sorry if the syntax is wrong, I don't really program in groovy very often)
Solution
according to the sources of lock plugin this should work:
import org.jenkins.plugins.lockableresources.LockableResourcesManager as LRM
def myResources = ["RES1", "RES2", "RES3"]
def notLocked = myResources.find{rName->
LRM.get().forName(rName).with{ r-> !r.isLocked() && !r.isQueued() }
}
if(notLocked){
lock(notLocked){
//do smth
}
}
Answered By - daggett