def process_job_history(Map config = [:]) {
// create default dict with some values
def defaultDict = [
verbose: false,
olderThanDays: 30,
]
// update the default map content with the user provided config content
// new key/value of provided map is automatically added to the defaultDict
defaultDict << config
def counter = 0;
// get the job by its name, this requires a non sandboxed job
def job_name = defaultDict.job_name;
def job = jenkins.model.Jenkins.instance.getItemByFullName(job_name, Job.class);
def all_builds = job.getBuilds();
if (defaultDict.verbose) {
println();
println("Processing ${job_name}");
// println("all_builds: ${all_builds}");
}
if (all_builds.size() < 1) {
if (defaultDict.verbose) {
println("No builds found");
}
return 0;
}
def today = new Date();
def earlier_in_time = today - defaultDict.olderThanDays;
// collect the history of builds, and print additional infos for each build
all_builds.each { build ->
// a value less than 0 if this Date is before the Date argument
if (build.getTime().compareTo(earlier_in_time) < 0 && build.keepLog) {
println("${build.absoluteUrl}: ${build.id}, result: ${build.result}, date: ${build.getTime().format('YYYY-MMM-dd HH:mm:ss')}");
// may "delete" (do not mark it as) keep forever
// build.keepLog = false;
counter++;
}
}
return counter;
}
pipeline {
agent any
stages {
stage("Process job history") {
steps {
// ''' uses system variables like $HOSTNAME, $PWD, ...
// """ uses groovy variables like someCustomVar
script {
def counter = 0;
def job_age = 90; // 90 days since today
Jenkins.instance.getAllItems(Job.class).each {
counter += process_job_history([job_name: "${it.getFullName()}", olderThanDays: job_age]);
}
println("Counted ${counter} jobs older than ${job_age} days");
}
}
}
}
}
3条答案
按热度按时间uqxowvwt1#
以下针对Jenkins的MySQL查询将列出所有标记为“Keep Forever”的构建版本的URL:
在浏览器中输入它,看看它会返回什么。
现在,您可以将其嵌入到基于XSLT的HTML中,以获得一个包含这些构建的链接的列表。要删除构建,您可以提供一个调用Jenkins CLI的按钮:
不幸的是,我不知道如何禁用'保持建设永远'与CLI不删除它。
js81xvg62#
我也在寻找同样的东西,我们的Jenkins也很大,并尝试链接:
我把它弄坏了。
但事实证明,我一次只需要一个作业的最后一个“永久保存”版本,这似乎工作得更快。因此,我使用以下代码代替:
它返回带有标记为“keep forever”的所有构建号的xml。您可以修改xpath以满足您的需要。
jv2fixgn3#
您可以使用以下管道来覆盖所有作业及其所有构建,并取消标记构建,如果它是例如。超过90天