Skip to content

Instantly share code, notes, and snippets.

@daniilyar
Last active January 2, 2024 06:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save daniilyar/b89d29c620c39f80da5e68a4209b5d44 to your computer and use it in GitHub Desktop.
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'
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
@daniilyar-incountry
Copy link

daniilyar-incountry commented Apr 22, 2020

If you use Jenkins folders then this one will work for you:

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 = []

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))))){
        oldJobs.add(job)
      }
    }
  }
}

oldJobs.sort { getLastBuildTime(it) }

jlc = new JenkinsLocationConfiguration()
oldJobs.each {
  println "'${it.getFullName()}' - ${getLastBuildTime(it)}, disabled = ${!it.buildable}"
}

return

@carlosrodlop
Copy link

carlosrodlop commented Dec 12, 2022

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