我找到了这个示例脚本(从
https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+System+Groovy+script开始),我想测试Jenkins参数化构建触发器插件,但是这个脚本会抛出错误.我希望这可以工作,任何想法为什么不呢?
这是我得到的错误:
/app/jenkins/workspace/Example-Parameterized-Trigger1/hudson2425966133354362461.groovy: 10: unable to resolve class ParametersAction @ line 10,column 53. ?.actions.find{ it instanceof Parameters ^ 1 error Build step 'Execute Groovy script' marked build as failure
这是脚本:
import hudson.model.* // get current thread / Executor def thr = Thread.currentThread() // get current build def build = thr?.executable // get parameters def parameters = build?.actions.find{ it instanceof ParametersAction }?.parameters parameters.each { println "parameter ${it.name}:" println it.dump() println "-" * 80 } // ... or if you want the parameter by name ... def hardcoded_param = "FOOBAR" def resolver = build.buildVariableResolver def hardcoded_param_value = resolver.resolve(hardcoded_param) println "param ${hardcoded_param} value : ${hardcoded_param_value}"
解决方法
从
Groovy plugin文档:
The plain “Groovy Script” is run in a forked JVM,on the slave where the build is run. It’s the basically the same as running the “groovy” command and pass in the script.
The system groovy script,OTOH,runs inside the Jenkins master’s JVM. Thus it will have access to all the internal objects of Jenkins,so you can use this to alter the state of Jenkins. It is similar to the Jenkins Script Console functionality.
显然,您使用错误的构建步骤(执行Groovy脚本而不是执行系统Groovy脚本),因此无法访问内部Jenkins的对象.