Skip to content

Instantly share code, notes, and snippets.

@tizki
Created January 23, 2016 18:39
Show Gist options
  • Save tizki/413fc510e586bbb6f7cd to your computer and use it in GitHub Desktop.
Save tizki/413fc510e586bbb6f7cd to your computer and use it in GitHub Desktop.
jenkins groovy post build script that post links to running builds from a view with their elapsed time in minutes
/*
This script should be used in groovy post build
it posts a link to the running jobs with their duration
the job are taken from the view
*/
import hudson.model.*
import jenkins.model.Jenkins
MY_VIEW = "My-view"
def myView = Hudson.instance.getView(MY_VIEW)
currentJobName = "myJob"
Map map = [:]
map = myView.getItems().collectEntries{job ->
def currBuild = job.getBuilds().getLastBuild()
def buildTime = currBuild?.getExecutor()?.getElapsedTime()
[(job.getAbsoluteUrl()): buildTime]
}.sort { a,b -> b.value <=> a.value }
map.each { k,v -> println "${k.padRight(45)} ${v}" }
def newDesc=""
summary = manager.createSummary("document.gif")
summary.appendText("**** Running jobs sorted by elapsed time ****<ul>", false)
map.each {
def timeInMinutes = it.value/60000
def text=""
if(timeInMinutes > 180){
text = sprintf("<li><b><a href=%s>%s</a></b> runs for <font color=\"red\" >%.2f minutes.</font></li>", it.key, it.key, timeInMinutes)
}
else{
text = sprintf("<li><b><a href=%s>%s</a></b> runs for %.2f minutes.</li>", it.key, it.key, timeInMinutes)
}
newDesc+=text.toString()
summary.appendText(text, false)
}
summary.appendText("</ul>", false)
def the_jen = Jenkins.getInstance();
def cd_job = the_jen.getItem(currentJobName);
cd_job.setDescription(newDesc);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment