Jenkins 库:java.lang.NullPointerException:无法在 null 对象上调用方法 xxx

2024-02-13

我正在使用声明性管道和单独的管道助手。在其中一个助手中我有文件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"
}

现在我想像这样使用 Jenkinsfile 中的函数

echo getTriggerCause().isTime()

这会导致 NPE:

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

当我看着this https://jenkins.io/doc/book/pipeline/shared-libraries/#defining-global-variables我希望这能起作用。与链接示例的唯一区别是我从 scm 动态加载库。


当我使用 load 时,我遇到了类似的错误消息,问题是我忘记返回this来自 groovy 文件。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Jenkins 库:java.lang.NullPointerException:无法在 null 对象上调用方法 xxx 的相关文章

随机推荐