Jenkins 脚本 批量删除构建记录

Jenkins 脚本 批量删除构建记录

删除单个 job 构建记录

def jobName = "admin-test"
def maxNumber = 100    // 小于该构建编号的构建会被删除

Jenkins.instance.getItemByFullName(jobName).builds.findAll {
  it.number <= maxNumber
}.each {
  it.delete()
}

删除所有 job 构建记录

可以保留最新的几个构建记录

def maxNumberToKeep = 2 // 保留的最新构建记录数量

Jenkins.instance.getAllItems().each { job ->
  def builds = job.builds
  if (builds.size() > maxNumberToKeep) {
    builds = builds.toArray()
    Arrays.sort(builds, { a, b -> a.number - b.number })
    builds[0..(builds.size() - maxNumberToKeep - 1)].each {
      it.delete()
    }
  }
}

你可能感兴趣的:(运维,jenkins,运维)