清理Jenkins中所有任务的历史记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def jobRetain = 3  // job 保留的历史记录数量
// Jenkins.instance.getClass(): hudson.model.Hudson
// 获取所有freestyle的job
jobs = Jenkins.instance.getAllItems(AbstactProject.class)
println(jobs)
// 获取所有pipeline的job
jobs = Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.job.WorkflowJob.class)
println(jobs)
// 获取所有的job
jobs = Jenkins.instance.getAllItems(hudson.model.Job.class)
println(jobs)

jobs.each { job ->
def builds = job.getBuilds()
println(builds)
if(builds) {
def latestBuildNumber = job.getLastBuild().getNumber()
def retainMaxNumber = latestBuildNumber - jobRetain
println("${job.name}'s last build number: ${latestBuildNumber}")
if (retainMaxNumber <= 0) {
println("${job.name}'s last build is less than ${jobRetain}, skip...")
return
}
println(builds.getClass())
job.getBuilds().findAll{it.number <= retainMaxNumber}.each {
// it.getClass(): org.jenkinsci.plugins.workflow.job.WorkflowRun
println("delete ${job.name}'s ${it.number}")
it.delete()
}
} else {
println("${job.name} don't have builder, skip...")
}
}

执行日志如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
Started by user unknown or anonymous
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] echo
[hudson.model.FreeStyleProject@5aab9184[jenkins_shell_test], com.tikal.jenkins.plugins.multijob.MultiJobProject@ce9939f[multijob_test], hudson.model.FreeStyleProject@12b9358a[no_build_job]]
[Pipeline] echo
[org.jenkinsci.plugins.workflow.job.WorkflowJob@10dabbd8[clear_history], org.jenkinsci.plugins.workflow.job.WorkflowJob@4e79153f[pipeline_shell_test]]
[Pipeline] echo
[org.jenkinsci.plugins.workflow.job.WorkflowJob@10dabbd8[clear_history], hudson.model.FreeStyleProject@5aab9184[jenkins_shell_test], com.tikal.jenkins.plugins.multijob.MultiJobProject@ce9939f[multijob_test], hudson.model.FreeStyleProject@12b9358a[no_build_job], org.jenkinsci.plugins.workflow.job.WorkflowJob@4e79153f[pipeline_shell_test]]
[Pipeline] echo
[clear_history #24, clear_history #23, clear_history #22, clear_history #21, clear_history #20, clear_history #19, clear_history #18, clear_history #17, clear_history #16, clear_history #15, clear_history #14]
[Pipeline] echo
clear_history's last build number: 24
[Pipeline] echo
class hudson.util.RunList
[Pipeline] echo
delete clear_history's 21
[Pipeline] echo
delete clear_history's 20
[Pipeline] echo
delete clear_history's 19
[Pipeline] echo
delete clear_history's 18
[Pipeline] echo
delete clear_history's 17
[Pipeline] echo
delete clear_history's 16
[Pipeline] echo
delete clear_history's 15
[Pipeline] echo
delete clear_history's 14
[Pipeline] echo
[jenkins_shell_test #7, jenkins_shell_test #6, jenkins_shell_test #5, jenkins_shell_test #4, jenkins_shell_test #3, jenkins_shell_test #2, jenkins_shell_test #1]
[Pipeline] echo
jenkins_shell_test's last build number: 7
[Pipeline] echo
class hudson.util.RunList
[Pipeline] echo
delete jenkins_shell_test's 4
[Pipeline] echo
delete jenkins_shell_test's 3
[Pipeline] echo
delete jenkins_shell_test's 2
[Pipeline] echo
delete jenkins_shell_test's 1
[Pipeline] echo
[multijob_test #3, multijob_test #2, multijob_test #1]
[Pipeline] echo
multijob_test's last build number: 3
[Pipeline] echo
multijob_test's last build is less than 3, skip...
[Pipeline] echo
[]
[Pipeline] echo
no_build_job don't have builder, skip...
[Pipeline] echo
[pipeline_shell_test #91, pipeline_shell_test #90, pipeline_shell_test #89, pipeline_shell_test #88, pipeline_shell_test #87, pipeline_shell_test #86, pipeline_shell_test #85, pipeline_shell_test #84, pipeline_shell_test #83, pipeline_shell_test #82]
[Pipeline] echo
pipeline_shell_test's last build number: 91
[Pipeline] echo
class hudson.util.RunList
[Pipeline] echo
delete pipeline_shell_test's 88
[Pipeline] echo
delete pipeline_shell_test's 87
[Pipeline] echo
delete pipeline_shell_test's 86
[Pipeline] echo
delete pipeline_shell_test's 85
[Pipeline] echo
delete pipeline_shell_test's 84
[Pipeline] echo
delete pipeline_shell_test's 83
[Pipeline] echo
delete pipeline_shell_test's 82
[Pipeline] End of Pipeline
Finished: SUCCESS