Skip to content

Instantly share code, notes, and snippets.

@wintondeshong
Created October 31, 2013 19:11
Show Gist options
  • Save wintondeshong/7255133 to your computer and use it in GitHub Desktop.
Save wintondeshong/7255133 to your computer and use it in GitHub Desktop.
Given an array of Jenkins CI job names, the script will retrieve the details for each build and output the results into a JSON file. Uses the Jenkins API Client Ruby Gem.
source 'http://rubygems.org'
ruby "2.0.0"
gem "jenkins_api_client"
gem "pry"
gem "json"
require "jenkins_api_client"
require "yaml"
require "pry"
require "json"
cli = JenkinsApi::Client.new(YAML.load_file(File.expand_path("~/.jenkins_api_client/login.yml", __FILE__)))
line_break = "-------------------------------------------------------------------------------"
######################################################
##
## Functions
##
######################################################
header = lambda { |title|
puts line_break
puts title
puts line_break
}
list_jobs = lambda {
header.call "All Jobs"
puts cli.job.list_all
}
get_latest_build_number = lambda { |job|
cli.job.get_current_build_number(job)
}
get_builds = lambda { |job|
header.call "Builds for #{job}"
puts cli.job.get_builds(job)
}
get_build_details = lambda { |job, build_number|
cli.job.get_build_details(job, build_number)
}
######################################################
##
## Work
##
######################################################
list_jobs.call
["MyProject-Development", "MyProject-Staging", "MyProject-Production"].each do |job|
latest_build_number = get_latest_build_number.call(job)
puts "Latest Build Number for '#{job}': #{latest_build_number}"
builds = Range.new(1, latest_build_number).map do |build_number|
begin
get_build_details.call job, build_number
rescue
{ :build_number => build_number, :failed => true }
end
end
File.open("#{job.downcase}.json", "w") do |f|
f.write(JSON.pretty_generate(builds))
end
end
# The Jenkins server and login information can be stored in a YAML file like this
# so we don't have to pass in the parameters every we login to the server
# through this API client.
# This file contains the following four parameters
# The IP address of the Jenkins CI Server
:server_ip: 0.0.0.0
# The port number on which the Jenkins listens on. The default is 8080
:server_port: 8080
# If there is a web server routing your Jenkins requests you might need this
:jenkins_path: ""
# The username and password to authenticate to the server
:username: my_username
:password: my_password
# The priviate key file for Jenkins CLI authentication
# remember to upload the public key to http://#{server_ip}:#{server_port}/user/#{my_username}/configure
:identity_file: ~/.ssh/id_rsa
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment