Jenkins 文件 GROOVY 错误 -- java.lang.NullPointerException:无法在空对象上调用方法 tokenize()

Jenkins file GROOVY error -- java.lang.NullPointerException: Cannot invoke method tokenize() on null object

我在 Jenkins 中构建项目时遇到一个问题。

实际上最近我们已经将我们的代码从 GITLAB 迁移到 GITHUB。 早些时候我们使用的变量可以直接在 GITLAB 中使用。 但是现在我们在 Jenkins 中通过 post content 参数访问 Webhook 数据。 当我们将代码从 IDE 推送到 GITHUB 时,我的管道工作正常。 但是当我 运行 我的管道使用带参数的构建时。 这行代码正在产生问题

env.gitLabSourceBranch = env.GIT_BRANCH.tokenize('/')[2]

env.GIT_BRANCH 来自 post Webhook 触发器。

我想要一个解决方案,当我们触发管道作为带有参数的构建时,它应该将 GITBRANCH 值设为 null 并绕过第一行。

I want a solution that when we trigger pipeline as build with parameter it should take the GITBRANCH value as null and bypass this firstline.

试试这个:

env.gitLabSourceBranch = env.GIT_BRANCH ? env.GIT_BRANCH.tokenize('/')[2] : null