【发布时间】:2021-06-29 00:23:32
【问题描述】:
我在 Jenkinsfile 的单独 groovy 文件中有以下代码。一旦新的构建工作被解雇,它应该取消旧的构建工作。它还检查不同的分支名称。
@NonCPS
def cancelPreviousBuilds() {
def buildNumber = env.BUILD_NUMBER.toInteger()
def currentJob = Jenkins.instance.getItemByFullName(env.JOB_NAME)
def currentBranch = env.BRANCH_NAME // Branch value of the current build
// Cancel old jobs that are from the same branch
for (def build : currentJob.builds) {
// parse out the branch name from each job
param = (build.getFullDisplayName().tokenize('»')[2]).tokenize(" ")[0]
if (build.isBuilding() && build.number < buildNumber && currentBranch == param) {
build.doStop()
}
}
}
但是,我的代码失败了
def buildNumber = env.BUILD_NUMBER.toInteger()
詹金斯的错误说:
java.lang.NullPointerException:无法在空对象上调用方法 toInteger()
我不能在这里使用toInteger() 吗?从回显buildNumber 这肯定是提取了内部版本号,所以我很确定它实际上不是空的。
【问题讨论】: