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.)

Werbung
Explore posts in the same categories: en, Hudson

9 Kommentare - “Hudson: how to get a list of all failed jobs?”


  1. You can also execute your Groovy script via Hudson CLI.

    java -jar hudson-cli.jar -s http://localhost:8080/ groovy script.groovy


  2. Ok, you’re right.
    I will wait for this security feature is available for using it.

  3. Rob Whitlock Says:

    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.

  4. newbie Says:

    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. Jan Says:

    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.

  6. lililately Says:

    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

  7. Mike Says:

    Hello Jan,

    I am trying to find the names of jobs from the command line for Hudson. I have never programmed in Groovy, and your scripts are giving my syntax errors. Would it be too much trouble to post the entire working script to list the jobs?

    Thank you in advance,

    Mike


Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit deinem WordPress.com-Konto. Abmelden /  Ändern )

Facebook-Foto

Du kommentierst mit deinem Facebook-Konto. Abmelden /  Ändern )

Verbinde mit %s


%d Bloggern gefällt das: