1
votes

I'm integrating Squish automation tool and Jenkins pipeline. Everything went smoothly. Now I need to send email report after the job's done. I have a Groovy file in pre-send script, but when this script runs, it throws out exception:

java.lang.NullPointerException: Cannot invoke method getRootDir() on null object

I figured out the "build" object in my Groovy script is Null. Not sure why it is. Please note if I use built-in Squish plugin and Editable Email on Jenkins, everything went smoothly. The problem just happen when I moved to use Pipeline.

@@@ - This is my Groovy script:

List getJenkinsTestResultFiles() {
    File squishResultsPath = new File( build.getRootDir(), "squishResults" )
    if ( !squishResultsPath.exists() || !squishResultsPath.isDirectory() ) {
       throw new GroovyRuntimeException( "Squish results path does not exist at: " + squishResultsPath.getAbsolutePath() )
    }


    File summaryFile = new File( squishResultsPath, "summary.xml" )
    if ( !summaryFile.exists() || !summaryFile.isFile() ) {
        throw new GroovyRuntimeException( "Squish summary file does not exist at: " + summaryFile.getAbsolutePath() )
    }

    List resultFiles = []
    def summaries = new XmlSlurper().parse( summaryFile )
    summaries.summary.each {
        resultFiles.push( new File( squishResultsPath, it.xmlFileName.text() ) )
    }

    return resultFiles
}

@@@ - This is my Pipeline script:

node('Slave_10.133.88.151') {

    stage('Squish Test') {
        step([$class: 'SquishBuilder',
            abortBuildOnError: false,
            extraOptions: '',
            host: '',
            port: '',
            resultFolder: "${WORKSPACE}\\Squish_Report",
            skipTestCases: false,
            snoozeFactor: '1',
            squishConfig: 'Default',
            testCase: '',
            testSuite: "${WORKSPACE}\\${TEST_SUITE}"])
    } 

    stage('Send Email') {
        emailext body: 'Test', 
        postsendScript: '${SCRIPT, template="SquishSummary.groovy"}', 
        subject: 'Pipeline', 
        to: '[email protected]'
    }

}
1

1 Answers

0
votes

The build object is a hudson.model.Build object, and since you are calling a shared library you'll have to import the Build object in your groovy script.

import hudson.model.Build

At the top of your shared library.

If you have already imported the object then the issue could be that you haven't initialized it inside of your shared library.