Failed to load and run groovy script in a pipeline job

17 views
Skip to first unread message

Owen Liu

unread,
Dec 2, 2020, 12:24:13 PM12/2/20
to Jenkins Users

Hi,

I want to load a groovy script and execute methods of that script in a simple scripted pipeline. but it failed to run the groovy script when loading the groovy script, the script is very simple, I tested it on my local computer, it ran successfully, I have the following configuration:
//Jenkinsfile
node("master"){
stage("test1"){

checkout([$class: 'GitSCM', branches: [[name: "refs/heads/master"]],
doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: 'xxx', url:
"git@xxx:xxx.git"]]])

def script=load "${env.WORKSPACE}/scripts/debug1.groovy"
script.hello()
}
}

//scripts/debug1.groovy
def initVariableBindings() {
def tempArr=["WORKSPACE"]
tempArr.each {
if (System.getenv(it) != null) {
this.binding.setProperty(it, System.getenv(it))
} else if (System.getProperty(it) != null) {
this.binding.setProperty(it, System.getProperty(it))
} else {
println "Failed to init variable "+ it;
}
}
}
def hello(){
println "hello ${WORKSPACE}"
}
initVariableBindings()

it throws the following exception:

groovy.lang.MissingFieldException: No such field: binding for class: Script1 at groovy.lang.MetaClassImpl.getAttribute(MetaClassImpl.java:2846) at groovy.lang.MetaClassImpl.getAttribute(MetaClassImpl.java:3782) at org.codehaus.groovy.runtime.InvokerHelper.getAttribute(InvokerHelper.java:147) at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getField(ScriptBytecodeAdapter.java:306) at org.kohsuke.groovy.sandbox.impl.Checker$9.call(Checker.java:437) at org.kohsuke.groovy.sandbox.GroovyInterceptor.onGetAttribute(GroovyInterceptor.java:96) at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetAttribute(SandboxInterceptor.java:438) at org.kohsuke.groovy.sandbox.impl.Checker$9.call(Checker.java:434) at org.kohsuke.groovy.sandbox.impl.Checker.checkedGetAttribute(Checker.java:439) at com.cloudbees.groovy.cps.sandbox.SandboxInvoker.getAttribute(SandboxInvoker.java:37) at com.cloudbees.groovy.cps.impl.AttributeAccessBlock.rawGet(AttributeAccessBlock.java:20) at Script1.initVariableBindings(Script1.groovy:8) at com.cloudbees.groovy.cps.CpsDefaultGroovyMethods.each(CpsDefaultGroovyMethods:2030) at com.cloudbees.groovy.cps.CpsDefaultGroovyMethods.each(CpsDefaultGroovyMethods:2015) at com.cloudbees.groovy.cps.CpsDefaultGroovyMethods.each(CpsDefaultGroovyMethods:2056) at Script1.initVariableBindings(Script1.groovy:4) at Script1.run(Script1.groovy:17) at ___cps.transform___(Native Method)
....


it complains that the List variable tempArr in the function initVariableBindings is not a binding property, but it is a local variable, it should work, is this normal behavior and why or a bug of groovy plugin?

Thanks.
Reply all
Reply to author
Forward
0 new messages