为什么这个Groovy脚本在Jenkins中失败以获取作业参数?

我找到了这个示例脚本(从 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的对象.

相关文章

背景:    8月29日,凌晨4点左右,某服务告警,其中一个...
https://support.smartbear.comeadyapi/docs/soapui/steps/g...
有几个选项可用于执行自定义JMeter脚本并扩展基线JMeter功能...
Scala和Java为静态语言,Groovy为动态语言Scala:函数式编程,...
出处:https://www.jianshu.com/p/ce6f8a1f66f4一、一些内部...
在运行groovy的junit方法时,报了这个错误:java.lang.Excep...