简体   繁体   中英

Why does this Groovy script fail in Jenkins to get the job parameters?

I found this sample script ( from https://wiki.jenkins-ci.org/display/JENKINS/Parameterized+System+Groovy+script ) and I wanted to test the Jenkins parameterized build trigger plugin but this script throws an error. I expected this to work, any ideas why it doesn't?

Here is the error I get:

/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

Here is the script:

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

From Groovy plugin documentation:

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.

Obviously, you used the wrong build step ( Execute Groovy script instead of Execute system Groovy script ) and thus do not have access to internal Jenkins' objects.

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