jenkins - 删除 Jenkins 构建历史时出错
问题描述
大家好,我在删除詹金斯作业的构建历史时遇到错误
No build logs found in calcmanager-pull-request and build history count is : 0
[Pipeline] End of Pipeline
java.lang.NullPointerException: Cannot invoke method getLastBuild() on null object
下面是我正在使用的管道脚本。calcmanager-pull-request 作业包含构建历史记录,但显示为零
pipeline {
agent { label 'master' }
stages {
stage('delete all jobs build history'){
steps {
script {
def buildLogCount=10
Jenkins.instance.getAllItems(Job.class).each { jobitem ->
def jobName = jobitem.name
def jobInfo = Jenkins.instance.getItem(jobName)
if(jobInfo.getLastBuild()){
def lastBuildNum=jobInfo.getLastBuild().getNumber()
if(lastBuildNum>=buildLogCount){
def deletedBuildNum=lastBuildNum - buildLogCount
def deletedNumRange = Fingerprint.RangeSet.fromString("0-${deletedBuildNum}",false);
def buildCount=jobInfo.getBuilds(deletedNumRange).size()
if(buildCount==0){
println "No build logs found in ${jobName} and build history count is : ${buildCount}"
}
else{
jobInfo.getBuilds(deletedNumRange).each { item ->
item.delete()
}
}
}
else{
println "No build logs to delete in ${jobName}"
}
}
else{
println "No build logs found in ${jobName}"
}
}
}
}
}
}
}
}
解决方案
由于某些作业从未运行,您收到了该错误。它没有历史。
因此,您可以使用具有历史记录的作业构建一个列表:
Jenkins.instance.getAllItems(Job.class)
.findAll { it.getLastBuild()?.getNumber() } // if job doesn't not history it will return null == false
.each { job ->
def lastBuildNum=job.getLastBuild().getNumber()
/// you code is here
}
推荐阅读
- bash - 如何使用 bash 操作 jq 输出?
- javascript - 按唯一 ID 添加/显示每一行
- python - 我如何使用 Apache 基本上从根本上运行 python 脚本?
- visual-studio - 从自动生成输出目录停止 Visual Studio 2017
- adsense - 我应该为博客使用动态页面还是实际文件?
- rest - 模拟 REST,如何将二进制文件附件从客户端请求保存到文件系统?
- nginx - NGINX 没有为 Parse Server 和 Socket.io 打开端口
- scala - Spark Scala Kmeans - 如何标记结果和可视化?
- routes - 如何规划具有分隔部分的页面的路线
- bash - 从文本文件中删除除第一行和每第 n 行之外的行