[英]groovy.lang.MissingPropertyException: No such property: Jenkins for class: hudson
I am trying to run a groovy script in Jenkins slave node to retrieve child jobs from a folder in Jenkins slave node.我正在尝试在 Jenkins 从节点中运行 groovy 脚本,以从 Jenkins 从节点中的文件夹中检索子作业。 Here is the groovy script I tried:这是我试过的 groovy 脚本:
I tried some SO answers and found groovy.lang.MissingPropertyException: No such property: jenkins for class: groovy.lang.Binding我尝试了一些 SO 答案,发现groovy.lang.MissingPropertyException: No such property: jenkins for class: Z5F19402E7ABCC75AEF0.
But this doesn't solve my problem.但这并不能解决我的问题。
Please find the code that I tried:请找到我尝试过的代码:
import groovy.json.JsonSlurper
import groovy.json.JsonBuilder
import jenkins.model.*
static main(args){
def childJobFolder = "childJob"
def childJobNameList = []
def env = System.getenv()
// Setting the environment properties to variables.
def jenkinsUsername = env.UAT_JENKINS_MY_USER
def jenkinsPassword = env.UAT_JENKINS_MY_PASS
def jsonSlurper = new JsonSlurper()
// Getting the child job names from "childJob" folder
Jenkins.instance.getItemByFullName(childJobFolder).allJobs.each{
def childJobName = it.name.toString()
if(childJobName.startsWith("job-")){
childJobNameList.add(childJobName)
}
}
println "\n" + "Child Jobs Available: " + childJobNameList + "\n"
}
Here is what I got in the console:这是我在控制台中得到的:
Caught: groovy.lang.MissingPropertyException: No such property: Jenkins for class: hudson3067346520259876246 groovy.lang.MissingPropertyException: No such property: Jenkins for class: hudson3067346520259876246 at hudson3067346520259876246.run(hudson3067346520259876246.groovy:17) Build step 'Execute Groovy script' marked build as failure Caught: groovy.lang.MissingPropertyException: No such property: Jenkins for class: hudson3067346520259876246 groovy.lang.MissingPropertyException: No such property: Jenkins for class: hudson3067346520259876246 at hudson3067346520259876246.run(hudson3067346520259876246.groovy:17) Build step 'Execute Groovy script'将构建标记为失败
Can someone help me to fix this error?有人可以帮我解决这个错误吗? Thanks in advance!提前致谢!
Finally, I found out the solution for this error.最后,我找到了这个错误的解决方案。 This is caused by running on plain groovy script instead of system groovy script.这是由于在普通 groovy 脚本而不是系统 groovy 脚本上运行引起的。 As Jayan said the Jenkins variables only available for System groovy scripts and not for plain groovy script.正如 Jayan 所说,Jenkins 变量仅适用于系统 groovy 脚本,不适用于普通 groovy 脚本。 For that reason I could not load Jenkins instances from plain groovy script.出于这个原因,我无法从普通的 groovy 脚本加载 Jenkins 实例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.