I created a Jenkins folder which has multiple jobs in it. I've also defined some variables in the folder itself. How can I check if the folder has a variable defined? I tied
import jenkins.model.*
private def getBuildJob(final String folder) {
def jobFolder = null
for (f in Jenkins.instance.getAllItems(AbstractItem.class)) {
if (f.fullName == folder) {
jobFolder = f
break
}
}
return jobFolder
}
node("linux-ubuntu") {
stage("test") {
def folder = getBuildJob("MyFolderName")
println folder.hasVariable("MY_VARIABLE")
}
}
But I get
hudson.remoting.ProxyException: groovy.lang.MissingMethodException: No signature of method: com.cloudbees.hudson.plugins.folder.Folder.hasVariable() is applicable for argument types: (java.lang.String) values: [MY_VARIABLE]
at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onMethodCall(SandboxInterceptor.java:153)
at org.kohsuke.groovy.sandbox.impl.Checker$1.call(Checker.java:161)
CodePudding user response:
If you are using the folder-properties plugin you can use the following script.
@NonCPS
def checkVar() {
def folderName = "Folderycr"
def propToCheck = "YCRPROP"
def folderItem = Jenkins.instance.getAllItems(com.cloudbees.hudson.plugins.folder.AbstractFolder.class).find{ (it.name == folderName) }
println folderItem.getProperties().each { prop ->
if(prop instanceof com.mig82.folders.properties.FolderProperties){
prop.getProperties().each{
if(it.key == propToCheck) {
println "Prop is available. Vale is : " it.value
}
}
}
}
}
node("linux-node") {
stage("Run test") {
checkVar()
}
}