bash Jenkins 删除所有作业的最新 20 个版本之前的版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35610053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 14:18:15  来源:igfitidea点击:

Jenkins delete builds older than latest 20 builds for all jobs

bashjenkinsgroovyjobs

提问by Fadi

I am in the process of cleaning up Jenkins (it was setup incorrectly) and I need to delete builds that are older than the latest 20 builds for every job.

我正在清理 Jenkins(它的设置不正确),我需要为每个作业删除比最新 20 个版本旧的版本。

Is there any way to automate this using a script or something?

有什么方法可以使用脚本或其他东西自动执行此操作吗?

I found many solutions to delete certain builds for specific jobs, but I can't seem to find anything for all jobs at once.

我找到了许多解决方案来删除特定作业的某些构建,但我似乎无法同时为所有作业找到任何内容。

Any help is much appreciated.

任何帮助深表感谢。

回答by Dave Bacher

You can use the Jenkins Script Consoleto iterate through all jobs, get a list of the N most recent and perform some action on the others.

您可以使用Jenkins 脚本控制台遍历所有作业,获取 N 个最近的列表并对其他作业执行一些操作。

import jenkins.model.Jenkins
import hudson.model.Job

MAX_BUILDS = 20

for (job in Jenkins.instance.items) {
  println job.name

  def recent = job.builds.limit(MAX_BUILDS)

  for (build in job.builds) {
    if (!recent.contains(build)) {
      println "Preparing to delete: " + build
      // build.delete()
    }
  }
}

The Jenkins Script Console is a great tool for administrative maintenance like this and there's often an existing script that does something similar to what you want.

Jenkins 脚本控制台是一个很好的管理维护工具,通常有一个现有的脚本可以执行类似于您想要的操作。

回答by Christian Ciach

For Multibranch Pipelines, I modified the script by Dave Bacher a bit. Use this to delete builds older than the latest 20 build of "master" branches:

对于多分支管道,我稍微修改了 Dave Bacher 的脚本。使用它来删除早于“主”分支的最新 20 个构建的构建:

MAX_BUILDS = 20

for (job in Jenkins.instance.items) {
  if(job instanceof jenkins.branch.MultiBranchProject) {
    job = job.getJob("master")
    def recent = job.builds.limit(MAX_BUILDS)
    for (build in job.builds) {
      if (!recent.contains(build)) {
        println "Preparing to delete: " + build
        // build.delete()
      }
    }
  }
}

回答by Johnny Doe

I got an issue No such property: builds for class: com.cloudbees.hudson.plugins.folder.Folderon Folders Plugin 6.6 while running @Dave Bacher's script

No such property: builds for class: com.cloudbees.hudson.plugins.folder.Folder在运行 @Dave Bacher 的脚本时遇到了有关 Folders Plugin 6.6的问题

Alter it to use functional api

改变它以使用功能性api

import jenkins.model.Jenkins
import hudson.model.Job

MAX_BUILDS = 5
Jenkins.instance.getAllItems(Job.class).each { job ->
  println job.name
  def recent = job.builds.limit(MAX_BUILDS)
  for (build in job.builds) {
    if (!recent.contains(build)) {
      println "Preparing to delete: " + build
      build.delete()
    }
  }
}

回答by KeepCalmAndCarryOn

There are lots of ways to do this

有很多方法可以做到这一点

Personally I would use the 'discard old builds' in the job config

我个人会在作业配置中使用“丢弃旧版本”

If you have lots of jobs you could use the CLIto step through all the jobs to add it

如果你有很多工作,你可以使用CLI来遍历所有工作来添加它

Alternatively there is the configuration slicing pluginwhich will also do this for you on a large scale

或者,还有配置切片插件,它也可以大规模地为您执行此操作

回答by chanukhya bachina

This can be done in many ways. You can try the following

这可以通过多种方式完成。您可以尝试以下操作

  1. get all your job names in a textfile by going to the jobs location in jenkins and run the following
  1. 通过转到 jenkins 中的作业位置并运行以下命令,在文本文件中获取所有作业名称

ls >jobs.txt

ls > 工作.txt

Now you can write a shell script with a for loop

现在您可以使用 for 循环编写 shell 脚本

#!/bin/bash
##read the jobs.txt
for i in 'cat <pathtojobs.txt>'
     do
curl -X POST http://jenkins-host.tld:8080/jenkins/job/$i/[1-9]*/doDeleteAll
     done

the above deletes all the jobs

以上删除所有作业

you can also refer herefor more answers

你也可以参考这里获得更多答案