Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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}"
like image 414
djangofan Avatar asked Aug 11 '15 18:08

djangofan


People also ask

How do I list all Jenkins jobs in Groovy?

The easiest way to get a list of all Jenkins jobs is by executing an appropriate Groovy script using a Jenkins script console feature. This note shows two examples of Groovy scripts for listing the Jenkins jobs.

Where are Jenkins hooks written in Groovy?

These scripts are written in Groovy, and get executed inside the same JVM as Jenkins, allowing full access to the domain model of Jenkins. For given hook HOOK, the following locations are searched: WEB-INF/HOOK.groovy in jenkins.war WEB-INF/HOOK.groovy.d/*.groovy in the lexical order in jenkins.war

Is there a way in Groovy to fail the build?

Is there a way in groovy to fail the build? in the "execute Groovy script" plugin. you can write code. where 'checkPromote' returns a true or false value depending on the status of the promote. Show activity on this post. The most elegant way to abort a programm in my opinion is an assertion. assert condition : "Build fails because..."

What happens when Jenkins encounters a fatal problem during boot?

When Jenkins encounters a fatal problem during boot, it’ll invoke "boot-failure" hook script to allow automatic corrective actions to be taken (such as notifying somebody, raising alerts, restarting, and so on.) These scripts get the cause of the problem as the "exception" variable when run.


1 Answers

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.

like image 164
Vitalii Elenhaupt Avatar answered Sep 30 '22 20:09

Vitalii Elenhaupt