简体   繁体   中英

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. Here is the groovy script I tried:

I tried some SO answers and found groovy.lang.MissingPropertyException: No such property: jenkins for class: groovy.lang.Binding

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

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. As Jayan said the Jenkins variables only available for System groovy scripts and not for plain groovy script. For that reason I could not load Jenkins instances from plain groovy script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM