A Groovy Continuous Integration (Jenkins) Dashboard

on Tuesday, 17 December 2013
I took inspiration from this post, but I wanted to use Groovy. I know there are plugins that do the same, but this was intended to be a learning exercise.


Whilst the example in the link is quite simple - I also need to address authentication of the user. Once available this would be used for displaying build status via a wall mounted monitor.

Requires "commons-codec-1.8.jar" and "commons-httpclient-3.1.jar" on the classpath

import org.apache.commons.httpclient.*
import org.apache.commons.httpclient.auth.*
import org.apache.commons.httpclient.methods.*
import groovy.xml.MarkupBuilder
def server = "localhost"
def port = 8093
def jenkinsHost = "https://${server}:${port}"
def username = "yourUsername"
def apiToken = "yourPassword"
def client = new HttpClient()
client.state.setCredentials(
new AuthScope( server, port, "realm"),
new UsernamePasswordCredentials( username, apiToken )
)
// Jenkins does not do any authentication negotiation,
// ie. it does not return a 401 (Unauthorized)
// but immediately a 403 (Forbidden)
client.params.authenticationPreemptive = true
def post = new PostMethod( "${jenkinsHost}/rssLatest" )
post.doAuthentication = true
File file = new File("rssLatest.xml")
try {
int result = client.executeMethod(post)
file.write(post.getResponseBodyAsString())
} finally {
post.releaseConnection()
}
def builds = new XmlParser().parse("rssLatest.xml")
def writer = new StringWriter()
def html = new MarkupBuilder(writer)
def cssClass
def hrefURL
html.html {
head {
title 'Continuous Integration HUD'
style(type: 'text/css', """
.stable { background-color: green }
.unstable { background-color: yellow }
.broken { background-color: red }
table { margin: 0 auto; }
a { font-size: bigger; text-decoration: none; color: black; }"""
)
script (src: "http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js")
script (type: "text/javascript", """
\\\$(document).ready(redraw);
\\\$(window).resize(redraw);
function redraw() {
\\\$('tr').height((\\\$(window).height()-60)/\\\$('tr').size());
\\\$('td').width((\\\$(window).width()-60)/\\\$('tr').first().find('td').size());
}
"""
)
}
body{
table {
tr {
def counter = 0
builds.each{ item->
cssClass = (item.title.text() =~ 'unstable') ? 'unstable' : 'stable'
cssClass = (item.title.text() =~ 'broken') ? 'broken' : cssClass
item.link.each{ a ->
hrefURL = a.attribute("href")
}
td(class: cssClass){
a(href: hrefURL, item.title.text())
}
counter++
if(counter == 6){
tr{}
counter = 0
}
}
}
}
}
}
println writer

0 comments:

Post a Comment