I have recently started an effort to start consolidating some global variables that are set up for our Jenkins scripted pipeline. I am starting small (with a single boolean to control whether log a lot of "extra" stuff to the build log) and wanted to follow the NotificationConfig created by Szymon Stepniak. However, I am unable to get past a "NullPointerException" that is thrown during the initialization of the DebugOptions
singleton.
Shared Library Source Layout
.
├── src
│ └── foo
│ └── utils
│ └── DebugOptions.groovy
└── vars
├── debugOptions.groovy
└── standardPipeline.groovy
src/com/foo/utils/DebugOptions.groovy
#!/usr/bin/env groovy
package com.foo.utils;
@Singleton
class DebugOptions {
// Simple wrapper around a debugging options to prevent argument
// explosion for everything that needs to filter output.
boolean flag = false
}
vars/debugOptions.groovy
#!groovy
import com.foo.utils.DebugOptions
// This DSL ("Domain Specific Language") helps modify and manage configuration objects
// Implement a closure body to be executed in context of DebugOptions
// Defining in vars makes it globally accessible
//
// Inspired by code at:
// https://stackoverflow.com/questions/50962643/best-way-to-set-reusable-property-in-jenkinsfile
def call(body) {
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = DebugOptions.instance
body()
}
/vars/standardPipeline.groovy
#!/usr/bin/env groovy
def call(body) {
boolean PIPELINE_DEBUG = ("${env.PIPELINE_DEBUG}"!="0")
if(PIPELINE_DEBUG){ println "[DEBUG] Pipeline Debug Logging Enabled" }
try{
debugOptions { flag = PIPELINE_DEBUG }
println DebugOptions.instance.flag
} catch (Exception e) {
println e.message
error e.message
}
}
// rest of pipeline implementation
Project Jenkinsfile
#!groovy
library "utils"
standardPipeline()
Build output
[Pipeline] echo
[DEBUG] Pipeline Debug Logging Enabled
[Pipeline] End of Pipeline
[Bitbucket] Notifying commit build result
[Bitbucket] Build result notified
java.lang.NullPointerException
Finished: FAILURE
Questions
- Is there a way to get more information about the nullpointer exception (like a stack trace)? I have tried to
throw e.message
in lieu of theerror e.message
and I get the same output. - Is there a plugin that I need to make this happen? I've got all the Pipeline plugins that I am aware of (especially Pipeline-Groovy)
- In
src/com/foo/utils/DebugOptions.groovy
do I need the semicolon after the package line? - Can someone point out where my problem actually is?
- Lastly, and I know it's subjective, but is there anything I should improve upon with this code? For one, I don't care for the case-sensitive-only difference in D/debugOptions.
Thank you!