简体   繁体   English

jenkins的groovy脚本中的参数访问引发异常

[英]Parameter access with in a groovy script in jenkins throws exception

I have a string parameter called VERSION which is set to example - 0000 Below is what i am trying in groovy pipeline in jenkins 我有一个名为VERSION的字符串参数,该参数设置为example-0000以下是我在jenkins的groovy管道中尝试的操作

import hudson.model.*
/*// get parameters
def parameters = build?.actions.find{ it instanceof ParametersAction 
}?.parameters
parameters.each {
println "parameter ${it.name}:"
println it.dump()
}
*/
print ("VERSION is ${VERSION}")
def version_value = build.buildVariableResolver.resolve("VERSION")

It throws the below exception what is wrong with accessing "build"? 它引发以下异常访问“ build”有什么问题?

[Pipeline] echo
VERSION is 0000
[Pipeline] End of Pipeline
groovy.lang.MissingPropertyException: No such property: build for class: WorkflowScript
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.unwrap(ScriptBytecodeAdapter.java:53)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.getProperty(ScriptBytecodeAdapter.java:458)
at com.cloudbees.groovy.cps.sandbox.DefaultInvoker.getProperty(DefaultInvoker.java:34)
at com.cloudbees.groovy.cps.impl.PropertyAccessBlock.rawGet(PropertyAccessBlock.java:20)
at WorkflowScript.run(WorkflowScript:12)
at ___cps.transform___(Native Method)

I think you are trying to access hudson.model.build, but I believe that is a class and not an object. 我认为您正在尝试访问hudson.model.build,但我相信这是一个类,而不是对象。 It is also not a property of the current object - WorkflowScript. 它也不是当前对象-WorkflowScript的属性。 So it simply does not exist. 因此它根本不存在。

In a pipeline script you should have access to "currentBuild". 在管道脚本中,您应该有权访问“ currentBuild”。 Go to your.jenkins.server.url/pipeline-syntax/globals to see what global variables scripts run via that server can access. 转到your.jenkins.server.url / pipeline-syntax / globals以查看通过该服务器运行的全局变量脚本可以访问的内容。

That same page should also show that you have access to a variable called "params" Perhaps what you are looking for is there? 在同一页面上还应该显示您可以访问名为“ params”的变量。也许您在寻找什么?

From your question, it is unclear what your end goal is. 从您的问题来看,不清楚您的最终目标是什么。 You already have access to VERSION as demonstrated by the echo statement. 如echo语句所示,您已经可以访问VERSION。 So why are you trying to get at it from a build object? 那么,为什么要尝试从构建对象中获取它呢? You may get more help if you update to explain what the end goal is. 如果您更新以解释最终目标,则可能会获得更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM