Skip to content

Instantly share code, notes, and snippets.

@abihf
Last active April 8, 2020 19:03
Show Gist options
  • Save abihf/223f5c0d67bac04d34a2875cffcca07e to your computer and use it in GitHub Desktop.
Save abihf/223f5c0d67bac04d34a2875cffcca07e to your computer and use it in GitHub Desktop.
Clean jenkins jobs
Jenkins.instance.slaves.each {
def comp = it.getComputer()
if (comp.isOffline()) {
comp.doDoDelete()
}
}
#!/bin/bash
find /var/jenkins_home/jobs -name build.xml | grep -v "/release/" | while read file ; do
dir_name="$(dirname "${file}")"
mtime=$(stat -c '%Y' "${file}")
now=$(date +%s)
age=$(( ( now - mtime ) / 86400 ))
if [ $age -gt 14 ]; then
echo Deleting ${age} old days ${dir_name}
rm -r "${dir_name}"
fi
done
def now = new Date() // Get the current time
// Get a list of all running jobs
def buildingJobs = Jenkins.instance.getAllItems(Job.class).findAll{ it.isBuilding() }.each { job->
// Enumerate all runs
job._getRuns().each { item ->
// If NOT currently building... check the next build for this job
if (!item.isBuilding()) return
// Access and calculate time information for this build.
def startedAt = new Date(item.getStartTimeInMillis())
def duration_mins = ((now.getTime() - item.getStartTimeInMillis()) / 60000).intValue()
println("${duration_mins}: ${item.getUrl()}")
if (duration_mins > 60) {
item.doKill()
}
}
}
[]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment