[workflow plugin] CPS groovy member declaration bug in intended behavior?

19 views
Skip to first unread message

Kenneth Baltrinic

unread,
Feb 8, 2015, 9:44:40 AM2/8/15
to jenkins...@googlegroups.com
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: 'm...@address.com')
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?

Jesse Glick

unread,
Feb 25, 2015, 6:12:08 PM2/25/15
to jenkins...@googlegroups.com
On Sunday, February 8, 2015 at 9:44:40 AM UTC-5, Kenneth Baltrinic wrote:
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?

If some general language construct works from a plain Groovy console but not a flow script then it is probably a bug. File it with a minimal reproducible test case. 
Reply all
Reply to author
Forward
0 new messages