groovyjenkins-scriptler

No such property for Jenkins while running groovy script from command line


Using script console I'm able to execute the below script without any issues. However whenever I run the script from command line I got the below error. Added a "Execute Groovy script" to the job throwing the same error. "Execute system Groovy script" works fine. Is there anything wrong I'm doing? Can somebody help me plz

import  jenkins.model.Jenkins.*;
jenkins = jenkins.model.Jenkins.instance

def item = jenkins.getItem("3rd-libraries")
def build = item.getLastBuild()

println build.isInProgress()

Caught: groovy.lang.MissingPropertyException: No such property: jenkins for class: job
groovy.lang.MissingPropertyException: No such property: jenkins for class: job
        at job.run(job.groovy:2)

Jenkins:2.18

Groovy: Groovy Version: 2.4.6 JVM: 1.7.0_40 Vendor: Oracle Corporation OS: Linux


Solution

  • I think you're missing the def declaration for jenkins.

    The second line should read;

    def jenkins = jenkins.model.Jenkins.instance