清理Jenkins中所有任务的历史记录 发表于 2019-11-08 | 更新于 2019-11-23 | 阅读次数: 123456789101112131415161718192021222324252627282930313233def jobRetain = 3 // job 保留的历史记录数量// Jenkins.instance.getClass(): hudson.model.Hudson// 获取所有freestyle的jobjobs = Jenkins.instance.getAllItems(AbstactProject.class)println(jobs)// 获取所有pipeline的jobjobs = Jenkins.instance.getAllItems(org.jenkinsci.plugins.workflow.job.WorkflowJob.class)println(jobs)// 获取所有的jobjobs = 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...") }} 执行日志如下 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677Started by user unknown or anonymousRunning 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] echoclear_history's last build number: 24[Pipeline] echoclass hudson.util.RunList[Pipeline] echodelete clear_history's 21[Pipeline] echodelete clear_history's 20[Pipeline] echodelete clear_history's 19[Pipeline] echodelete clear_history's 18[Pipeline] echodelete clear_history's 17[Pipeline] echodelete clear_history's 16[Pipeline] echodelete clear_history's 15[Pipeline] echodelete 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] echojenkins_shell_test's last build number: 7[Pipeline] echoclass hudson.util.RunList[Pipeline] echodelete jenkins_shell_test's 4[Pipeline] echodelete jenkins_shell_test's 3[Pipeline] echodelete jenkins_shell_test's 2[Pipeline] echodelete jenkins_shell_test's 1[Pipeline] echo[multijob_test #3, multijob_test #2, multijob_test #1][Pipeline] echomultijob_test's last build number: 3[Pipeline] echomultijob_test's last build is less than 3, skip...[Pipeline] echo[][Pipeline] echono_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] echopipeline_shell_test's last build number: 91[Pipeline] echoclass hudson.util.RunList[Pipeline] echodelete pipeline_shell_test's 88[Pipeline] echodelete pipeline_shell_test's 87[Pipeline] echodelete pipeline_shell_test's 86[Pipeline] echodelete pipeline_shell_test's 85[Pipeline] echodelete pipeline_shell_test's 84[Pipeline] echodelete pipeline_shell_test's 83[Pipeline] echodelete pipeline_shell_test's 82[Pipeline] End of PipelineFinished: SUCCESS 打赏 微信支付