user2859458
user2859458

Reputation: 2495

Jenkins: View last X builds

I want to view the more recent X builds for several Jenkins jobs.

So if I wanted to show the last 5 builds for jobs 1-5 it would looks something like this:

status    build     time
-------------------------
pass      job1#3    13:54
fail      job1#2    13:05
fail      job1#1    13:01
pass      job5#1    12:17
pass      job3#1    11:03

How can I accomplish this?

Notice the builds of the jobs are woven together so if one job has run many builds recently it will show up more than other jobs that haven't run as much.

Upvotes: 1

Views: 1675

Answers (2)

Nux
Nux

Reputation: 10002

Here is a bit cleanup version of Jon S Groovy script. Also displaying worst build information.

import hudson.model.*;
import java.text.SimpleDateFormat;

//
// Settings
//
def numHoursBack = 24;
def dateFormat = new SimpleDateFormat("HH:mm");

def cutOfTime = System.currentTimeMillis() - numHoursBack * 3600 * 1000;

/**
 * Basic build information.
 */
def printBuildInfo(finalizedBuild) {
    String level = "INFO";
    String result = finalizedBuild.getResult().toString();
    switch (result) {
        case "UNSTABLE":
            level = "WARNING";
        break;
        case "FAILURE":
            level = "ERROR";
        break;
    }
    // basic info and URL
    println(String.format(
        "[%s] Build %s result is: %s.",
        level,
        finalizedBuild.getFullDisplayName(),
        result
    ));
    // pipe description from downstream
    def description = finalizedBuild.getDescription();
    if (description != null && !description.isEmpty()) {
        println(description.replaceAll("<br>", ""));
    }

    return finalizedBuild;
}

/**
 * Get recent build items.
 */
def getRencentBuilds(cutOfTime) {
    SortedMap res = new TreeMap();

    for (job in Jenkins.instance.getAllItems(BuildableItem.class)) {
        for (build in job.getBuilds()) {
            if (build.getTimeInMillis() < cutOfTime) {
                break;
            }
            res.put(build.getTimeInMillis(), build);
        }
    }

    return res;
}
/**
 * Print build items.
 *
 * minResult - minimum to print
 */
def printBuilds(builds, minResult, dateFormat) {
    def format = "%-10s %-8s %s";
    Result worstResult = Result.SUCCESS;
    def worstBuild = null;
    // header
    println(String.format(format, "status", "Time", "build"));
    // list
    for (entry in builds.descendingMap().entrySet()) {
        def build = entry.getValue();
        Result result = build.getResult();
        if (result.isWorseThan(worstResult)) {
            worstResult = result;
            worstBuild = build;
        }
        if (result.isWorseOrEqualTo(minResult)) {
            println(String.format(
                format, build.getResult(), dateFormat.format(build.getTime()), build.getFullDisplayName()
            ));
        }
    }

    return worstBuild;
}

def builds = getRencentBuilds(cutOfTime);
println ("\n\n----------------------\n Failed builds:\n");
def worstBuild = printBuilds(builds, Result.FAILURE, dateFormat);
println ("\n\n----------------------\n Worst build:\n");
if (worstBuild != null) {
    printBuildInfo(worstBuild);
}
println ("\n\n----------------------\n All builds:\n");
printBuilds(builds, Result.SUCCESS, dateFormat);

Upvotes: 1

Jon S
Jon S

Reputation: 16346

Executing the following script in the Script Console (Manage Jenkins -> Script Console):

import java.text.SimpleDateFormat
def numHoursBack = 24
def dateFormat = new SimpleDateFormat("HH:mm")
def buildNameWidth = 30


def cutOfTime = System.currentTimeMillis() - numHoursBack * 3600 * 1000

SortedMap res = new TreeMap();

for (job in Jenkins.instance.getAllItems(BuildableItem.class)) {
  for (build in job.getBuilds()) {
    if (build.getTimeInMillis() < cutOfTime) {
      break;
    }
    res.put(build.getTimeInMillis(), build)
  }
}

def format = "%-10s%-${buildNameWidth}s%-10s"

println(String.format(format, "status", "build", "Time"))
for (entry in res.descendingMap().entrySet()) {
  def build = entry.getValue()
  println(String.format(format, build.getResult(), build.getFullDisplayName(), dateFormat.format(build.getTime())))
}

For me this gives:

status    build                         Time      
SUCCESS   xxx #107393                   17:53     
SUCCESS   xxx #107392                   17:48     
SUCCESS   xxx #107391                   17:43     
null      yyy #3030                     17:38     
SUCCESS   xxx #107390                   17:38     
FAILURE   zzz #3248                     17:37     
...

You might need to change the numHoursBack constant, it controls the number of hours back to look for builds. As well as the buildNameWidth which determines the column width of the build column (if you have really long job and build names you might need to extend this).

Upvotes: 2

Related Questions