Skip to content

Instantly share code, notes, and snippets.

@jimklimov
Last active June 26, 2019 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimklimov/48227cb3b9ccc8968efd5b814868baa5 to your computer and use it in GitHub Desktop.
Save jimklimov/48227cb3b9ccc8968efd5b814868baa5 to your computer and use it in GitHub Desktop.
List currently known Jenkins jobs along with their status and labels, if any
// List currently known Jenkins jobs along with their status and labels, if any.
// Primary use-case for us is to find enabled obsolete jobs, we have a few
// we don't want deleted just yet, and to avoid enabled jobs without an agent
// label assigned so they are not picked up by random unsuitable machines.
import hudson.model.Job
//getAllItems searches a global lookup table of items regardless of folder structure
Jenkins.instance.getAllItems(Job.class).each { Job job ->
String jobLabelString
String jobStatus
try {
jobStatus = (job.disabled ? "DISABLED" : "ENABLED" )
} catch (Exception e) {
// e.g. pipeline generated jobs
jobStatus = "Status:N/A"
}
try {
jobLabelString = job.getAssignedLabelString()
} catch (Exception e) {
jobLabelString = "N/A"
}
//if ( jobLabelString == null || jobLabelString.equals("") || jobLabelString.equals("N/A") ) return
println jobStatus + ":\t'" + job.fullName + "' \tLABEL(S): '" + jobLabelString + "' \tCFGURL: " + job.getAbsoluteUrl() + "configure"
}
//null so no result shows up
null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment