Hudson: how to get a list of all failed jobs?
If you have a lot of job configured on your Hudson installation, you maybe want to have a list of job which last run fails. Sadly there is no view to that, but you could get the list via Hudsons script console and a three lines of Groovy:
activeJobs = hudson.model.Hudson.instance.items.findAll{job -> job.isBuildable()}
failedRuns = activeJobs.findAll{job -> job.lastBuild.result == hudson.model.Result.FAILURE}
failedRuns.each{run -> println(run.name)}
or a little bit longer (for explanations):
hudsonInstance = hudson.model.Hudson.instance
allItems = hudsonInstance.items
activeJobs = allItems.findAll{job -> job.isBuildable()}
failedRuns = activeJobs.findAll{job -> job.lastBuild.result == hudson.model.Result.FAILURE}
failedRuns.each{run -> println(run.name)}
Line 1: This is the main entry point to the running Hudson instance.
Line 2: Jobs are a special kind of item. Here are all stored items.
Line 3: Now we filter the whole list. We dont need disabled jobs as we dont want to fix them. So the closure checks if the job is buildable and therefore active.
Line 4: The next filter removes all jobs which were ok. The closure checks the last build result and selects the job if it was a failure.
Line 5: Now we pass an additional closure to the list for getting a nicer output (than a println(failedRuns) )
(Sorry for bad source code formatting – WordPress doesnt support Groovy.)
5. Juli 2009 at 17:30
You can also execute your Groovy script via Hudson CLI.
java -jar hudson-cli.jar -s http://localhost:8080/ groovy script.groovy
6. Juli 2009 at 08:26
Yes, but as Kohsuke said [1], it doesnt provide credentials yet. That means, if you have a secured Hudson installation (login with username and password) the CLI doesnt work because you cant provide these information.
You will get a
java.io.IOException: Server returned HTTP response code: 403 for URL: http://HUDSON_SERVER/cli
[1] http://www.nabble.com/Authorization-For-hudson-cli-td23530589.html
6. Juli 2009 at 09:42
Ok, you’re right.
I will wait for this security feature is available for using it.
27. Juli 2009 at 18:13
Does anyone know how to actually then run jobs from the hudson API? This example is really helpful when getting a list of failed jobs, but then i would actually like to rerun them all at once.
28. Juli 2009 at 14:38
I tried a little bit: http://janmaterne.wordpress.com/2009/07/28/hudson-start-a-list-of-jobs-using-groovy-console/
5. Oktober 2009 at 14:25
Jan, this is really helpful, but do you happen to know how this can be rewritten in java..
basically, I’m trying to write a java plugin for hudson to clean the maven repositories on a regular basis.
Any help is appreciated.
TIA
5. Oktober 2009 at 15:25
The entry in the Groovy code is >>hudsonInstance = hudson.model.Hudson.instance<< for getting the running Hudson instance. From a plugin this is Hudson.getInstance(). Groovys findAll() Method filteres a list of items according to the given closure. So you have to do the filtering for yourself. All other coding is just using the accessor methods instead of just writing the fields.
31. Oktober 2009 at 19:36
Hi I was reading this post very nice I was curious if you would like to be a part of my site team lililately.wordpress.com a news site with over 2,000 hits in the first month. If you’d like to do so contact contactlililately@gmail.com. Become an author!
-Lili
Great Job janmaterne