我正在使用声明性管道和单独的pipline助手。在其中一个帮助器中,我有文件vars / getTriggerCause.groovy with
/**
* Checks for cause of the job trigger and returns respective cause
* @return user, scm, time or other
*/
def String getCause() {
echo "CAUSE ${currentBuild.rawBuild.getCauses().properties}"
def cause = "${currentBuild.rawBuild.getCauses()}"
if (cause =~ "UserIdCause") {
return "user"
}
}
/**
* Checks if trigger cause of the job is the timer
* @return true if trigger is timer
*/
def boolean isTime() {
return this.call() == "time"
}
现在我想在Jenkisfile中使用这个函数
echo getTriggerCause().isTime()
导致NPE:
java.lang.NullPointerException: Cannot invoke method getCause() on null object
当我看到this时,我希望这会有效。与链接示例的唯一区别是我从scm动态加载库。
答案 0 :(得分:0)
删除括号可解决问题,因此可以正常使用
getTriggerCause.isTime()
答案 1 :(得分:0)
使用load时,我也收到类似的错误消息,问题是我忘记从groovy文件中返回{
"name": "systemjs",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"server": "browser-sync start --directory --server --files '*.js,
*.html, *.css'"
},
"author": "",
"license": "ISC",
"devDependencies": {
"browser-sync": "^2.24.5"
}
}
。