I have the following script in by global cps library.
Cookbook.groovy:
package chef
Throwable exception = null
boolean validate() {
validated = false
try {
sh 'validate'
validated = true
} catch (Throwable ex) {
exception = ex
}
return (boolean)validated
}
boolean publish() {
published = false
try {
sh 'publish'
published = true
} catch (Throwable ex) {
exception = ex
}
return (boolean)published
}
def emailResults(Map args) {
if (exception != null) {
exceptionDetails = """<p>The job failed with the following
error:</p>
<pre style=\"padding:7px;background-color:#E0E0E0;\">${exception.toString()}
${exception.getStackTrace().join('\n')}</pre>"""
}
mail to: args.to,
subject: "Validation Job ${env['JOB_NAME']} ${subject}",
mimeType: 'text/html',
body: """<html><body>
<p>The alidation job <strong>${env['JOB_NAME']}</strong> ${message}</p>
<p>This job run is viewable on Jenkins at: </p>
<p style=\"padding:10px\"><a
href=\"${env['BUILD_URL']}\">${env['BUILD_URL']}</a></p>
${exceptionDetails}
<body></html>"""
}
def done() {
if (exception != null) {
echo """The job failed with the following error:
${exception.toString()}
${exception.getStackTrace().join('\n')}
"""
throw exception
}
}
This script fail on the if (exception != null) lines with
a groovy.lang.MissingPropertyException: No such property: exception for
class: chef.Cookbook when invoked from a job script similar to the
following:
cookbook = new chef.Cookbook()
if(cookbook.validate()) {
cookbook.publish()
}
cookbook.setEmail(to: '[email protected]')
cookbook.done()
The problem goes away and the code works as expected if I assign null to
exception at the top of the validate function, like so:
boolean validate() {
exception = null // Must assign to this WITHIN a method, else
accessing it within a method
// results in the an exception:
// groovy.lang.MissingPropertyException
validated = false //signals that we are at least trying to validate
...
Experimenting around with things, I found that any *executable* code (vrs
pure declarations) that I put outside of functions in such class files (eg.
echo steps, throw new Exception('boom!') etc. do not seem to execute.
If I put similar code in a regular groovy console I don't have this
problem, it only seems to be in the CPS version of groovy, but I am no
groovy expert, so I must ask, is this intended behavior and if so why, or
should I file a bug for this?
--
You received this message because you are subscribed to the Google Groups
"Jenkins Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/jenkinsci-users/fb534476-168e-475c-8eed-b1063d9fed37%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.