我最近开始努力整合为我们的Jenkins脚本化管道设置的一些全局变量。我开始时比较小(用一个布尔值来控制是否将大量“额外”的东西记录到构建日志中),并希望遵循Szymon Stepniak创建的NotificationConfig。但是,我无法克服DebugOptions
单例初始化期间引发的“ NullPointerException”。
共享库的源布局
.
├── 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
项目Jenkinsfile
#!groovy
library "utils"
standardPipeline()
构建输出
[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
问题
throw e.message
代替error e.message
,但得到相同的输出。src/com/foo/utils/DebugOptions.groovy
中,包装行后是否需要分号?谢谢!