user3270760
user3270760

Reputation: 1504

How to get number of failed builds by cause with Jenkins

I have the Build Failure Analyzer plugin installed in my Jenkins instance, and I have a number of different failures entered into the plugin. Does anyone know if it is possible to get the total number of failures across all jobs that have the same cause?

For example, I occasionally get "ChannelClosedException" failures if the build node goes offline during a build or test unexpectedly and I would like to determine how often this is happening across all my jobs. Is there some way to aggregate this value? I imagine it could be done through groovy if you can iterate over each build for each job and collect the Build Failure cause if one is detected.

Has anyone else done something like this before?

Upvotes: 1

Views: 2124

Answers (2)

user3270760
user3270760

Reputation: 1504

Using some google-fu and some of the info from VinDev, I came up with this solution:

// get all jobs in Jenkins
Jenkins.instance.getAllItems(Job).each {
    // get all builds for each job
    def jobBuilds=it.getBuilds()
    //for each build, get the name and status + and failure messages
    jobBuilds.each { build ->
      // get the build status
      def currentStatus = build.buildStatusSummary.message
      // we only care about the broken builds because we want failure messages
      if (currentStatus.contains("broken")) {
        println "Build: ${build} | Status: ${currentStatus}"
        def BFA = build.actions.find{ it instanceof com.sonyericsson.jenkins.plugins.bfa.model.FailureCauseBuildAction };
          if (BFA != null) {
            for (failureCause in BFA.getFoundFailureCauses()) {
              println("name: " + failureCause.getName() + ", description: " + failureCause.getDescription())
              }
          }
      }
    }
  }

Upvotes: 0

D. Vinson
D. Vinson

Reputation: 1158

Not the exact answer... but should be able to modify to get what you are looking for:

Jenkins.instance.getAllItems(Job).each{

      def jobBuilds=it.getBuilds()

        //for each job get the things (you can limit at your convenience)
        jobBuilds.each { build ->
          def runningSince = groovy.time.TimeCategory.minus( new Date(), build.getTime() )
          def currentStatus = build.buildStatusSummary.message
          def cause = build.getCauses()[0] //we keep the first cause
          def user = cause instanceof Cause.UserIdCause? cause.getUserId():""
          println "Build: ${build} | Since: ${runningSince} | Status: ${currentStatus} | Cause: ${cause} | User: ${user}"
          def parameters = build.getAction(ParametersAction)?.parameters
          parameters.each {
            println "Type: ${it.class} Name: ${it.name}, Value: ${it.dump()}" 

            }
        }
    }

Upvotes: 1

Related Questions