Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Daan Poron
committed
Nov 10, 2011
0 parents
commit 3dbc535
Showing
6 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.gem | ||
.rvmrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
This script will contain Kunstmaan specific capistrano commands | ||
|
||
How to install: | ||
+ gem build kumastrano.gemspec | ||
+ gem install kumastrano-0.0.1.gem |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Gem::Specification.new do |s| | ||
s.name = 'kumastrano' | ||
s.version = '0.0.1' | ||
s.summary = <<-DESC.strip.gsub(/\n\s+/, " ") | ||
Deploying Kunstmaan applications with Capistrano, Jenkins and GIT. | ||
DESC | ||
s.description = <<-DESC.strip.gsub(/\n\s+/, " ") | ||
Deploying Kunstmaan applications with Capistrano, Jenkins and GIT. | ||
DESC | ||
|
||
s.files = Dir.glob("lib/**/*") | ||
s.require_path = 'lib' | ||
s.has_rdoc = false | ||
|
||
s.author = "Kunstmaan" | ||
s.email = 'hello@kunstmaan.be' | ||
s.homepage = 'http://www.kunstmaan.be' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module Kumastrano | ||
class GitHelper | ||
|
||
require 'cgi' | ||
|
||
def self.git_hash | ||
hash = %x(git rev-parse HEAD) | ||
hash.strip | ||
end | ||
|
||
def self.branch_name | ||
name = %x(git name-rev --name-only HEAD) | ||
name.strip | ||
end | ||
|
||
def self.origin_refspec | ||
refspec = %x(git config remote.origin.fetch) | ||
refspec.strip | ||
end | ||
|
||
def self.origin_url | ||
url = %x(git config remote.origin.url) | ||
url.strip | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
module Kumastrano | ||
class JenkinsHelper | ||
|
||
require 'cgi' | ||
require "net/http" | ||
require 'uri' | ||
require 'json' | ||
|
||
def self.make_safe_job_name(app_name, branch_name) | ||
job_name = "#{app_name} (#{branch_name})" | ||
job_name.gsub("/", "-") # \/#* is unsafe for jenkins job name, because not uri safe | ||
end | ||
|
||
def self.job_url_for_branch(jenkins_base_uri, branch_name) | ||
current_job_url = nil | ||
Kumastrano::JenkinsHelper.list_jobs(jenkins_base_uri).each do |job| | ||
name = job["name"] | ||
url = job["url"] | ||
if /.*\(#{branch_name}\)/.match(name) | ||
current_job_url = url | ||
break | ||
end | ||
end | ||
current_job_url | ||
end | ||
|
||
def self.job_url_for_name(jenkins_base_uri, job_name) | ||
current_job_url = nil | ||
Kumastrano::JenkinsHelper.list_jobs(jenkins_base_uri).each do |job| | ||
name = job["name"] | ||
url = job["url"] | ||
if job_name == name | ||
current_job_url = url | ||
break | ||
end | ||
end | ||
current_job_url | ||
end | ||
|
||
def self.list_jobs(base_uri) | ||
res = get_plain("#{base_uri}/api/json?tree=jobs[name,url]") | ||
parsed_res = JSON.parse(res.body)["jobs"] | ||
end | ||
|
||
def self.create_new_job(base_uri, job_name, config) | ||
uri = URI.parse("http://jenkins.uranus.kunstmaan.be/jenkins/createItem/api/json") | ||
request = Net::HTTP::Post.new(uri.path + "?name=#{CGI.escape(job_name)}") | ||
request.body = config | ||
request["Content-Type"] = "application/xml" | ||
res = Net::HTTP.start(uri.host, uri.port) {|http| http.request(request)} | ||
if res.code.to_i == 200 | ||
puts "job created" | ||
else | ||
puts "job not created" | ||
puts res.body | ||
end | ||
end | ||
|
||
def self.retrieve_config_xml(job_uri) | ||
res = get_plain("#{job_uri}/config.xml").body | ||
end | ||
|
||
def self.build_job(job_uri) | ||
res = get_plain("#{job_uri}/build") | ||
res.code.to_i == 302 | ||
end | ||
|
||
private | ||
|
||
def self.get_plain(uri) | ||
uri = URI.parse uri | ||
res = Net::HTTP.start(uri.host, uri.port) { |http| http.get(uri.path, {}) } | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
namespace :jenkins do | ||
|
||
set :jenkins_base_uri, "http://jenkins.uranus.kunstmaan.be/jenkins" | ||
set :jenkins_base_job_name, "Default" | ||
|
||
require "#{File.dirname(__FILE__)}/helpers/git_helper.rb" | ||
require "#{File.dirname(__FILE__)}/helpers/jenkins_helper.rb" | ||
require 'rexml/document' | ||
|
||
desc "Try to build the current branch on Jenkins" | ||
task:build do | ||
|
||
## 1. locate the job in jenkins | ||
current_branch = Kumastrano::GitHelper.branch_name | ||
job_name = Kumastrano::JenkinsHelper.make_safe_job_name(application, current_branch) | ||
puts "Locating job #{job_name}" | ||
current_job_url = Kumastrano::JenkinsHelper.job_url_for_name(jenkins_base_uri, job_name) | ||
|
||
## 2. if no job was found, first create a job for this branch | ||
if current_job_url.nil? | ||
puts "No job found, start creating one" | ||
default_job_url = Kumastrano::JenkinsHelper.job_url_for_name(jenkins_base_uri, jenkins_base_job_name) | ||
if !default_job_url.nil? | ||
current_refspec = Kumastrano::GitHelper.origin_refspec | ||
current_url = Kumastrano::GitHelper.origin_url | ||
|
||
default_job_config = Kumastrano::JenkinsHelper.retrieve_config_xml(default_job_url) | ||
document = REXML::Document.new(default_job_config) | ||
root = document.root | ||
|
||
## change the description | ||
root.elements['description'].text = "This job will be used for testing the branch " + current_branch | ||
|
||
## change the git config | ||
git_element = root.elements["scm[@class='hudson.plugins.git.GitSCM']"] | ||
git_element.elements["userRemoteConfigs/hudson.plugins.git.UserRemoteConfig/refspec"].text = current_refspec | ||
git_element.elements["userRemoteConfigs/hudson.plugins.git.UserRemoteConfig/url"].text = current_url | ||
git_element.elements["branches/hudson.plugins.git.BranchSpec/name"].text = current_branch | ||
|
||
## create the new job based on the modified git config | ||
Kumastrano::JenkinsHelper.create_new_job(jenkins_base_uri, job_name, document.to_s) | ||
current_job_url = Kumastrano::JenkinsHelper.job_url_for_name(jenkins_base_uri, job_name) | ||
end | ||
end | ||
|
||
## 3. run the build command | ||
if !current_job_url.nil? | ||
puts "Start building job #{job_name}" | ||
Kumastrano::JenkinsHelper.build_job(current_job_url) | ||
end | ||
end | ||
|
||
desc "List jobs" | ||
task:list_jobs do | ||
puts Kumastrano::JenkinsHelper.list_jobs(jenkins_base_uri) | ||
end | ||
|
||
## puts Kumastrano::GitHelper.git_hash | ||
|
||
end |