我得到了这个空指针异常,并且锁没有被释放,它已经正常工作了很长时间,突然开始抛出该异常
有人有什么主意吗?
stage('Deploy to iDev') {
steps {
script {
lock(resource: "$DEV_LOCK", inversePrecedence: true) {
milestone (15)
ansiColor('xterm') {
ansibleTower credential: '',
extraVars: "$DEV_ANSIBLE_PARAMS",
importTowerLogs: true,
importWorkflowChildLogs: false,
inventory: '',
jobTags: '',
jobTemplate: "$DEV_ANSIBLE_ID",
jobType: 'run',
limit: '',
removeColor: false,
skipJobTags: '',
templateType: 'job',
towerServer: "$TOWER_SERVER",
verbose: true
}
}
if ("$DEV_CONTAINER_JOB" != 'NA') {
build job: "$DEV_CONTAINER_JOB"
}
if ("$DEV_TEST_JOB" != 'NA') {
build job: DEV_TEST_JOB, parameters: [[$class: DEV_TEST_PARAMS_CLASS, name: DEV_TEST_PARAMS_NAME, value: DEV_TEST_PARAMS_VALUE]]
}
}
}
post {
failure {
// We want to email the development team.
}
aborted {
echo "aborted.. during deploy to iDev"
}
}
}
错误如下:
java.lang.NullPointerException
at org.jenkins.plugins.lockableresources.LockableResourcesManager.freeResources(LockableResourcesManager.java:323)
at org.jenkins.plugins.lockableresources.LockableResourcesManager.unlockNames(LockableResourcesManager.java:367)
at org.jenkins.plugins.lockableresources.LockStepExecution$Callback.finished(LockStepExecution.java:125)
at org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback$TailCall.onSuccess(BodyExecutionCallback.java:114)
at org.jenkinsci.plugins.workflow.cps.CpsBodyExecution$SuccessAdapter.receive(CpsBodyExecution.java:368)
at com.cloudbees.groovy.cps.Outcome.resumeFrom(Outcome.java:73)
答案 0 :(得分:2)
根据this
您需要执行此操作,因为锁定是声明性步骤或包装器
stage('Deploy to iDev') {
steps {
lock(resource: "$DEV_LOCK", inversePrecedence: true) {
script {
.
.
.
}
}
}
}
根据定义的方式,$DEV_LOCK
也可能会遇到问题。您可能可以执行"${env.DEV_LOCK}"
或"${DEV_LOCK}"
再仔细一点,我认为您只需要script
条if
语句。您甚至可以使用带有表达式的when
子句将build job...
放入单独的阶段,并完全失去script
并按照我的第一个链接答案锁定整个管道
答案 1 :(得分:0)
乍一看,我想说$DEV_LOCK
在被评估时不存在。只是为了论证,您可以尝试将其更改为一个临时的静态字符串,比如说
lock(resource: "foo", inversePrecedence: true)
深入了解它,然后
看到错误堆栈跟踪的这一行
org.jenkins.plugins.lockableresources.LockableResourcesManager.freeResources(LockableResourcesManager.java:323)
根据帖子的发布日期
该行是
323. private synchronized void freeResources(List<String> unlockResourceNames, @Nullable Run<?, ?> build) {
321. for (String unlockResourceName : unlockResourceNames) {
322. for (LockableResource resource : this.resources) {
323. if (resource != null && resource.getName() != null && resource.getName().equals(unlockResourceName)) {
我们可以看到resource
不为null,并且resource.getName()
不为null,因此唯一可能的null值是unlockResourceName
,这是有道理的,因为未在上面的行。
因此,您资源的资源名称(请记住您的资源为$DEV_LOCK
)似乎为空。
所以我想说,上面建议的测试仅使用lock(resource: "foo", inversePrecedence: true)
来查看问题是否来自那里,将是一个很好的起点。如果可行,则可以确定是否确实需要$DEV_LOCK
作为环境变量,也可以将其更改为静态变量。如果需要,请从那里带走,以尝试找出未设置的位置或实际设置的位置。