Last active
January 2, 2024 06:24
-
-
Save daniilyar/b89d29c620c39f80da5e68a4209b5d44 to your computer and use it in GitHub Desktop.
Jenkins Groovy script to find unused jobs. Unused means 'not built for more than X days'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jenkins.model.Jenkins | |
import jenkins.model.JenkinsLocationConfiguration | |
String jobNameRegexp = ".*" | |
long maxThresholdDays = 60; // days | |
boolean ignoreDisabledJobs = true | |
def getLastBuildTime(Job job) { | |
def lastBuild = job.getLastBuild() | |
if(lastBuild == null) { | |
return new Date(0) | |
} else { | |
return lastBuild.getTime() | |
} | |
} | |
Date currentDate = new Date() | |
def oldJobs = Jenkins.instance.items | |
.findAll { job -> job.name =~ jobNameRegexp } | |
.findAll { job -> | |
def item = Jenkins.instance.getItem(job.name) | |
def lastBuildTime = getLastBuildTime(item) | |
boolean tooOld = lastBuildTime.before(new Date((currentDate.getTime() - (maxThresholdDays * 24L * 60 * 60 * 1000)))) | |
if (ignoreDisabledJobs) { | |
return tooOld && job.buildable | |
} else { | |
return tooOld | |
} | |
} | |
.sort { getLastBuildTime(it) } | |
jlc = new JenkinsLocationConfiguration() | |
oldJobs.each { | |
println "${it.name} - ${getLastBuildTime(it)}, disabled = ${!it.buildable}" | |
} | |
return |
Updated version:
- Distinguish between Never run jobs vs job not being run since `maxThresholdDays``
- Removing
jlc = new JenkinsLocationConfiguration()
as it does not add any value
import jenkins.model.Jenkins
import jenkins.model.JenkinsLocationConfiguration
String jobNameRegexp = ".*"
long maxThresholdDays = 60; // days
def getLastBuildTime(Job job) {
def lastBuild = job.getLastBuild()
if(lastBuild == null) {
return new Date(0)
} else {
return lastBuild.getTime()
}
}
Date currentDate = new Date()
def folders = Jenkins.instance.items.findAll { job -> job.name =~ jobNameRegexp }
def oldJobs = []
def neverRunJobs = []
folders.each {
for (job in it.getAllJobs()) {
if(job != null) { // && job.buildable
def item = Jenkins.instance.getItemByFullName(job.getFullName())
def lastBuildTime = getLastBuildTime(item)
if(lastBuildTime.before(new Date((currentDate.getTime() - (maxThresholdDays * 24L * 60 * 60 * 1000))))){
if (lastBuildTime.equals(new Date("01/01/1970"))){
neverRunJobs.add(job)
}else{
oldJobs.add(job)
}
}
}
}
}
oldJobs.sort { getLastBuildTime(it) }
println "\n\nJobs not built since $maxThresholdDays number of days. Total: $oldJobs.size\n\n"
oldJobs.each {
println "'${it.getFullName()}' - ${getLastBuildTime(it)}, disabled = ${!it.buildable}"
}
println "\n\nJobs never build. Total: $neverRunJobs.size\n\n"
neverRunJobs.each {
println "'${it.getFullName()}' - ${getLastBuildTime(it)}, disabled = ${!it.buildable}"
}
return
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you use Jenkins folders then this one will work for you: