Jenkins Scripts: Print Jobs

1 minute read

In this post we’ll see how write a very simple script to print a list of all Jenkins jobs.
We’ll also see how to print jobs names based on given regex.

The following script will first print the number of jobs on our Jenkins and line below it will print the names of all the jobs we have.

jobs = jenkins.model.Jenkins.instance.getJobNames()                                                                                                                          
println "Number of jobs: " + jobs.size()
println "\nList of jobs:\n" + jobs.join("\n")

This following script will print the same information, it’s simply using a little bit different syntax.

jobs = jenkins.model.Jenkins.instance.items
println "Number of jobs: " + jobs.size()
println "\nList of jobs:\n"
jobs.each { job -> println }

The following script will print only jobs whose name include “prod” and “v1” strings accordingly. If v1 is before prod, the name of the job will not be included in the result.

jobs = jenkins.model.Jenkins.instance.items.findAll { job -> =~ /prod.*v1*$/ }

println "Number of jobs: " + jobs.size() + "\n"

jobs.each { job -> println }

If you would like to see a different version of the above scripts, let me know in the comments :)