为什么这个 Groovy 脚本在 Jenkins 中无法获取作业参数?
Why does this Groovy script fail in Jenkins to get the job parameters?
我找到了这个示例脚本(来自 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.
显然,您使用了错误的构建步骤(Execute Groovy script
而不是 Execute system Groovy script
),因此无法访问 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.
显然,您使用了错误的构建步骤(Execute Groovy script
而不是 Execute system Groovy script
),因此无法访问 Jenkins 的内部对象。