简体   繁体   中英

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

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"?

[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. It is also not a property of the current object - WorkflowScript. So it simply does not exist.

In a pipeline script you should have access to "currentBuild". Go to your.jenkins.server.url/pipeline-syntax/globals to see what global variables scripts run via that server can access.

That same page should also show that you have access to a variable called "params" Perhaps what you are looking for is there?

From your question, it is unclear what your end goal is. You already have access to VERSION as demonstrated by the echo statement. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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