微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么这个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的对象.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐