Skip to content

Instantly share code, notes, and snippets.

@Brunas
Forked from mavimo/Readme.md
Last active June 27, 2017 15:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Brunas/7025460 to your computer and use it in GitHub Desktop.
Save Brunas/7025460 to your computer and use it in GitHub Desktop.
Extended Jenkins build widget for Dashing

Description

This is extended version of Dashing widget to display a Jenkins build status and build progress.

The widget is based on the original Jenkins Build widget. It supports multiple Jenkins servers. Also, when build is not BUILDING the progress gauge isn't shown, instead build title is bigger. Also, fixed problem when build progress was shown with negative number.

Installation

Put the files jenkins_build.coffee, jenkins_build.html and jenkins_build.scss goes in the /widget/jenkins_build directory and the jenkins_build.rb in the /jobs directory

Otherwise you can install this plugin typing:

dashing install GIST_ID

Usage (jenkins_build.rb)

Change the job_mapping to specify your Jenkins projects, servers and authentication information, eg.:

job_mapping = {
  'job1' => { :job => 'Job Name 1', :jenkins => {'url' => URI.parse("http://localhost:8080"), 'auth_name' => nil 'auth_password' => nil }},
  'job2' => { :job => 'Job Name 2', :pre_job => 'Job Name 3', :jenkins => {'url' => URI.parse("http://neighbour:8080"), 'auth_name' => 'user' 'auth_password' => 'psw' }}
}

Put the following in your dashing board file to show the status:

<li data-row="1" data-col="1" data-sizex="1" data-sizey="1">
  <div data-id="job1" data-view="JenkinsBuild" data-title="Build title" data-description="A little description of build" data-min="0" data-max="100"></div>
</li>

Multiple jobs can add in dashboard repeating the snippet and changing data-id in accord with job name specified in job_mapping. Widget support title and description via data attributes, this attributest can be omitted if you do not like to display this information.

class Dashing.JenkinsBuild extends Dashing.Widget
@accessor 'value', Dashing.AnimatedValue
@accessor 'bgColor', ->
if @get('currentResult') == "SUCCESS"
"#96bf48"
else if @get('currentResult') == "FAILURE"
"#D26771"
else if @get('currentResult') == "PREBUILD"
"#ff9618"
else
"#999"
constructor: ->
super
@observe 'value', (value) ->
$(@node).find(".jenkins-build").val(value).trigger('change')
ready: ->
meter = $(@node).find(".jenkins-build")
title = $(@node).find(".title")
$(@node).fadeOut().css('background-color', @get('bgColor')).fadeIn()
meter.attr("data-bgcolor", meter.css("background-color"))
meter.attr("data-fgcolor", meter.css("color"))
if @get('currentResult') != 'BUILDING'
meter.hide()
title.fadeOut().addClass("static-title").fadeIn()
else
meter.show().knob()
title.fadeOut().removeClass("static-title").fadeIn()
onData: (data) ->
if data.currentResult isnt data.lastResult
$(@node).fadeOut().css('background-color', @get('bgColor')).fadeIn()
<h2 class="title" data-bind="title"></h2>
<input class="jenkins-build" data-angleOffset=-125 data-angleArc=250 data-width=250 data-readOnly=true data-bind-value="value | shortenedNumber" data-bind-data-min="min" data-bind-data-max="max">
<p class="more-info" data-bind="description"></p>
<p class="last-built" data-bind="lastBuilt"></p>
<p class="updated-at" data-bind="updatedAtMessage"></p>
require 'net/http'
require 'json'
require 'time'
# the key of this mapping must be a unique identifier for your job, the according value must be the name that is specified in jenkins
job_mapping = {
'jenkins' => { :job => 'Jenkins Build', :jenkins => {'url' => URI.parse("http://localhost:8080"), 'auth_name' => nil, 'auth_password' => nil}},
'jenkins2' => { :job => 'Jenkins Build 2', :jenkins => {'url' => URI.parse("http://neighbour:8080"), 'auth_name' => "user", 'auth_password' => "psw"}}
}
def get_number_of_failing_tests(job_name, jenkins)
info = get_json_for_job(job_name, jenkins, 'lastCompletedBuild')
info['actions'][4]['failCount']
end
def get_completion_percentage(job_name, jenkins)
build_info = get_json_for_job(job_name, jenkins)
prev_build_info = get_json_for_job(job_name, jenkins, 'lastCompletedBuild')
return 0 if not build_info["building"]
last_duration = (prev_build_info["duration"] / 1000).round(2)
current_duration = (Time.now.to_f - build_info["timestamp"] / 1000).round(2)
return 99 if current_duration >= last_duration
result = ((current_duration * 100) / last_duration).round(0)
return 0 if result < 0
result
end
def get_json_for_job(job_name, jenkins, build = 'lastBuild')
job_name = URI.encode(job_name)
http = Net::HTTP.new(jenkins['url'].host, jenkins['url'].port)
#request = http.request(Net::HTTP::Get.new("/job/#{job_name}/#{build}/api/json"))
request = Net::HTTP::Get.new("/job/#{job_name}/#{build}/api/json")
if jenkins['auth_name']
request.basic_auth(jenkins['auth_name'], jenkins['auth_password'])
end
response = http.request(request)
JSON.parse(response.body)
end
job_mapping.each do |title, jenkins_project|
current_status = nil
SCHEDULER.every '10s', :first_in => 0 do |job|
last_status = current_status
build_info = get_json_for_job(jenkins_project[:job], jenkins_project[:jenkins])
current_status = build_info["result"]
if build_info["building"]
current_status = "BUILDING"
percent = get_completion_percentage(jenkins_project[:job], jenkins_project[:jenkins])
elsif jenkins_project[:pre_job]
pre_build_info = get_json_for_job(jenkins_project[:pre_job], jenkins_project[:jenkins])
current_status = "PREBUILD" if pre_build_info["building"]
percent = get_completion_percentage(jenkins_project[:pre_job], jenkins_project[:jenkins])
end
send_event(title, {
currentResult: current_status,
lastResult: last_status,
timestamp: build_info["timestamp"],
value: percent
})
end
end
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color: #4b5f24;
$title-color: rgba(255, 255, 255, 1);
$moreinfo-color: rgba(255, 255, 255, 0.7);
$meter-background: rgba(20, 20, 20, 1);
// ----------------------------------------------------------------------------
// Widget-jenkins-build styles
// ----------------------------------------------------------------------------
.widget-jenkins-build {
background-color: $background-color;
input.jenkins-build {
background-color: $meter-background;
color: #fff;
text-shadow:
-1px -1px 0 #000,
1px -1px 0 #000,
-1px 1px 0 #000,
1px 1px 0 #000;
}
.title {
color: $title-color;
font-size: 300%;
text-align:center;
text-shadow:
-1px -1px 0 #000,
1px -1px 0 #000,
-1px 1px 0 #000,
1px 1px 0 #000;
}
.static-title {
font-size:370%;
}
.more-info {
color: $moreinfo-color;
font-size:1em;
}
.updated-at {
color: rgba(0, 0, 0, 1);
}
}
@ramkumar-hgst
Copy link

ramkumar-hgst commented Nov 24, 2016

Hi,

I have successfully installed but only one jenkins instance is connected. I am getting exception for other jenkins instance.
could you please help me.

scheduler caught exception:
execution expired
/root/.rbenv/versions/2.3.1/lib/ruby/2.3.0/net/http.rb:880:in initialize' /root/.rbenv/versions/2.3.1/lib/ruby/2.3.0/net/http.rb:880:in open'
/root/.rbenv/versions/2.3.1/lib/ruby/2.3.0/net/http.rb:880:in block in connect' /root/.rbenv/versions/2.3.1/lib/ruby/2.3.0/timeout.rb:101:in timeout'
/root/.rbenv/versions/2.3.1/lib/ruby/2.3.0/net/http.rb:878:in connect' /root/.rbenv/versions/2.3.1/lib/ruby/2.3.0/net/http.rb:863:in do_start'
/root/.rbenv/versions/2.3.1/lib/ruby/2.3.0/net/http.rb:852:in start' /root/.rbenv/versions/2.3.1/lib/ruby/2.3.0/net/http.rb:1398:in request'
/var/www/dashing/jenkins/jobs/jenkins_build.rb:37:in get_json_for_job' /var/www/dashing/jenkins/jobs/jenkins_build.rb:45:in block (2 levels) in <top (required)>'
/root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:230:in trigger_block' /root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rufus-scheduler-2.0.24/lib/rufus/sc/jobs.rb:204:in block in trigger'
/root/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rufus-scheduler-2.0.24/lib/rufus/sc/scheduler.rb:430:in `block in trigger_job'

Regards,
Ram

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment