加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 16:31:23 所属栏目:大数据 来源:网络整理
导读:我找到了这个示例脚本(从 https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+System+Groovy+script开始),我想测试Jenkins参数化构建触发器插件,但是这个脚本会抛出错误.我希望这可以工作,任何想法为什么不呢? 这是我得到的错误: /app/jenkins/wo
我找到了这个示例脚本(从 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的对象.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读