Skip to content

Instantly share code, notes, and snippets.

@duksis
Created February 1, 2013 11:02
Show Gist options
  • Save duksis/4690675 to your computer and use it in GitHub Desktop.
Save duksis/4690675 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require "optparse"
require "uri"
class PreCiRunner
DEFAULTS = {
:jobs => [:wimdu, :wimdu_js, :wimdu_jasmine, :wimdu_guard_jasmine, :wimdu_checkout]
}
def initialize(attributes = {})
@attributes = DEFAULTS.merge(attributes)
@original_branch = current_branch
@branch_to_build = generate_branch_name
end
attr_reader :original_branch, :branch_to_build
def jenkins_url
@attributes.fetch(:jenkins_url)
end
def trigger_build(suffix = nil)
url = "#{job_url(suffix)}/buildWithParameters?GIT_SHA=#{branch_to_build}"
if num = @attributes[suffix.to_sym]
url << "&PARALLEL_TEST_PROCESSORS=#{num}"
end
puts "sending to url #{url}"
system "curl --insecure -i '#{url}'"
end
def job_url(suffix)
[jenkins_url.gsub(/\/$/, ''), 'job', suffix].compact.join('/')
end
def hostname
URI(jenkins_url).hostname
end
def whoami
`whoami`.strip
end
def generate_branch_name
"build_#{whoami}_%s" % [Time.now.strftime("%Y%m%d%H%M%S")]
end
def current_branch
`git branch | grep "* " | cut -d " " -f 2`.strip
end
def jobs
@attributes.fetch(:jobs)
end
def run
system "git checkout -b #{branch_to_build}"
system "git push #{hostname} #{branch_to_build}"
jobs.each do |job|
trigger_build job
end
system "git checkout #{original_branch}"
system "git branch -D #{branch_to_build}"
puts "pushed #{branch_to_build}"
end
end
MASTER_NUMBERS = {
wimdu_js: 4,
wimdu: 4,
}
options = {
jenkins_url: ENV["PRE_CI_URL"],
}
opts = OptionParser.new do |o|
o.on('-u', '--jenkins-url JENKINS_URL', String, "Jenkins on which to trigger the build. default: ENV['PRE_CI_URL']") do |jenkins_url|
options[:jenkins_url] = jenkins_url
end
o.on('--jobs JOBS', Array, 'Default is to run all jobs') do |jobs|
options[:jobs] = jobs
end
o.on('--master', 'User same config as for master') do
options.merge!(MASTER_NUMBERS)
end
o.on('-j NUM', '--js-processes', Integer, 'Number of parallel js processes') do |num|
options[:wimdu_js] = num
end
o.on('-n NUM', '--default-processes', Integer, 'Number of parallel non-js processes') do |num|
options[:wimdu] = num
end
end
opts.parse(ARGV)
if !options[:jenkins_url]
puts opts
else
runner = PreCiRunner.new(options)
runner.run
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment