詹金斯:查看最后的X版本

时间:2018-03-07 16:18:38

标签: jenkins jenkins-plugins

我想查看几个Jenkins工作的最新X版本。

因此,如果我想显示作业1-5的最后5个版本,它将看起来像这样:

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

我该如何做到这一点?

请注意,作业的构建是编织在一起的,所以如果一个作业最近运行了很多构建,它将比其他没有运行的作业显示更多。

2 个答案:

答案 0 :(得分:2)

在脚本控制台中执行以下脚本(管理Jenkins - >脚本控制台):

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

对我来说,这给了:

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

您可能需要更改numHoursBack常量,它会控制返回查找构建的小时数。以及确定构建列的列宽的buildNameWidth(如果您有非常长的作业和构建名称,则可能需要扩展它)。

答案 1 :(得分:1)

这是Jon S Groovy脚本的一个清理版本。还显示最差的构建信息。

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