groovy script to get the jenkins jobs failures and there cause

4,511 views
Skip to first unread message

raja saggam

unread,
Oct 25, 2016, 1:12:00 PM10/25/16
to Jenkins Developers
I looking out to write the groovy script to list the failure jenkins jobs and there cause for the failure

raja saggam

unread,
Oct 25, 2016, 1:19:11 PM10/25/16
to Jenkins Developers
I have used the below code where i am getting list of failed jobs and also the jobs which are currently inprogress

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


how to skip those jobs which are running  
and the getting the cause of the jobs which are failed

Rachel Moreno

unread,
Oct 25, 2016, 6:26:35 PM10/25/16
to Jenkins Developers
Hi Raja,

Have a look at these methods for job.lastBuild:
I hope be useful.

Best regards,
Rachel

Jimilian

unread,
Oct 26, 2016, 3:29:18 AM10/26/16
to Jenkins Developers

raja saggam

unread,
Oct 26, 2016, 5:25:22 AM10/26/16
to Jenkins Developers
Hi Jimilian,

i am trying to get the reason why jenkins jobs failed for example code changes, build steps etc.

Regards,
raja
Message has been deleted

raja saggam

unread,
Oct 27, 2016, 10:19:14 AM10/27/16
to Jenkins Developers
Hi Rachel,

I have used the above function where i'm getting whole console out for last build.

can you let me know how to filtered errors from console output using groovy instead of printing the whole console out.

Regards,
Naresh.

raja saggam

unread,
Oct 27, 2016, 10:23:48 AM10/27/16
to Jenkins Developers
here is the code for it

import hudson.tasks.*
import com.cloudbees.hudson.plugins.folder.Folder


//Iterate over the each Job
activeJobs = hudson.model.Hudson.instance.items.findAll{job -> !(job instanceof Folder) && job.isBuildable()}
println("successjobs = " +activeJobs.size())
failedRuns = activeJobs.findAll{job -> job.lastBuild != null && !(job.lastBuild.isBuilding()) && job.lastBuild.result == hudson.model.Result.FAILURE}

// Do something with them - e.g. listing failed jobs
println("failedjobs = " +failedRuns.size())
//BUILD_STRING = "Build step 'Execute shell' marked build as failure"
//failedRuns.each{error -> println "errors: ${error.lastBuild.log.contains(BUILD_STRING)}"}
for(item in [failedRuns]){ 
 println("=====================================================================")
  item.each{ run -> println "Failed Job Name: ${run.name}"}
  item.each{logs -> println (logs.lastBuild.getLog())}

Rachel Moreno

unread,
Oct 27, 2016, 5:14:25 PM10/27/16
to Jenkins Developers
Hi Naresh,

That's not a Jenkins question, but Groovy question.

You're lucky, Christmas time is near ;)

Try it with this code from BUILD_STRING declaration (I think it's very readable):

...


BUILD_STRING
= "Build step 'Execute shell' marked build as failure"


failedRuns
.each{ item ->
    println
("=====================================================================")
    println
"Failed Job Name: ${item.name}"
    item
.lastBuild.getLog().eachLine { line ->
       
if (line =~ /$BUILD_STRING/) {
            println
"error: $line"
       
}
   
}
}

println
("=====================================================================")


I hope be useful.

Best regards,
Rachel

raja saggam

unread,
Nov 2, 2016, 1:52:30 AM11/2/16
to Jenkins Developers
Hi Rachel,

Sorry for the delay response...thanks very much :)

Regards,
Naresh.
Reply all
Reply to author
Forward
0 new messages