Class: KStrano::JenkinsHelper
- Inherits:
-
Object
- Object
- KStrano::JenkinsHelper
- Defined in:
- lib/helpers/jenkins_helper.rb
Class Method Summary collapse
- .available?(base_uri) ⇒ Boolean
- .build_and_wait(job_uri, timeout = 300, interval = 2) ⇒ Object
- .build_info(job_uri, build = "lastBuild") ⇒ Object
- .build_job(job_uri) ⇒ Object
- .create_new_job(base_uri, job_name, config) ⇒ Object
- .fetch_build_hash_from_build_info(build_info, branch_name) ⇒ Object
- .job_url_for_branch(jenkins_base_uri, branch_name) ⇒ Object
- .job_url_for_name(jenkins_base_uri, job_name) ⇒ Object
- .last_build_number(job_uri) ⇒ Object
- .list_jobs(base_uri) ⇒ Object
- .make_safe_job_name(app_name, branch_name) ⇒ Object
- .retrieve_config_xml(job_uri) ⇒ Object
Class Method Details
.available?(base_uri) ⇒ Boolean
9 10 11 12 |
# File 'lib/helpers/jenkins_helper.rb', line 9 def self.available?(base_uri) res = get_plain("#{base_uri}") res.code.to_i == 200 end |
.build_and_wait(job_uri, timeout = 300, interval = 2) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/helpers/jenkins_helper.rb', line 14 def self.build_and_wait(job_uri, timeout=300, interval=2) success = false last_build_info = nil prev_build = KStrano::JenkinsHelper.last_build_number(job_uri) KStrano::JenkinsHelper.build_job(job_uri) KStrano.poll("A timeout occured", timeout, interval) do ## wait for the building to be finished last_build_info = KStrano::JenkinsHelper.build_info(job_uri) result = last_build_info['result'] ## SUCCESS or FAILURE building = last_build_info['building'] number = last_build_info['number'] if number == (prev_build + 1) && "false" == building.to_s && !result.nil? if "SUCCESS" == result success = true else success = false end true else false end end return success, last_build_info end |
.build_info(job_uri, build = "lastBuild") ⇒ Object
119 120 121 122 |
# File 'lib/helpers/jenkins_helper.rb', line 119 def self.build_info(job_uri, build="lastBuild") res = get_plain("#{job_uri}/#{build}/api/json") parsed_res = JSON.parse(res.body) end |
.build_job(job_uri) ⇒ Object
108 109 110 111 |
# File 'lib/helpers/jenkins_helper.rb', line 108 def self.build_job(job_uri) res = get_plain("#{job_uri}/build") res.code.to_i == 302 end |
.create_new_job(base_uri, job_name, config) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/helpers/jenkins_helper.rb', line 90 def self.create_new_job(base_uri, job_name, config) uri = URI.parse("#{base_uri}/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 |
.fetch_build_hash_from_build_info(build_info, branch_name) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/helpers/jenkins_helper.rb', line 39 def self.fetch_build_hash_from_build_info(build_info, branch_name) actions = build_info['actions'] build_hash = nil actions.each do |list| last_revision = list['lastBuiltRevision'] if !last_revision.nil? && branch_name == last_revision['branch'][0]['name'].sub("origin/", "") build_hash = last_revision['branch'][0]['SHA1'] break end end build_hash end |
.job_url_for_branch(jenkins_base_uri, branch_name) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/helpers/jenkins_helper.rb', line 59 def self.job_url_for_branch(jenkins_base_uri, branch_name) current_job_url = nil KStrano::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 |
.job_url_for_name(jenkins_base_uri, job_name) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/helpers/jenkins_helper.rb', line 72 def self.job_url_for_name(jenkins_base_uri, job_name) current_job_url = nil KStrano::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 |
.last_build_number(job_uri) ⇒ Object
113 114 115 116 117 |
# File 'lib/helpers/jenkins_helper.rb', line 113 def self.last_build_number(job_uri) res = get_plain("#{job_uri}/api/json?tree=lastBuild[number]") parsed_res = JSON.parse(res.body) parsed_res['lastBuild']['number'] end |
.list_jobs(base_uri) ⇒ Object
85 86 87 88 |
# File 'lib/helpers/jenkins_helper.rb', line 85 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 |
.make_safe_job_name(app_name, branch_name) ⇒ Object
54 55 56 57 |
# File 'lib/helpers/jenkins_helper.rb', line 54 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 |
.retrieve_config_xml(job_uri) ⇒ Object
104 105 106 |
# File 'lib/helpers/jenkins_helper.rb', line 104 def self.retrieve_config_xml(job_uri) res = get_plain("#{job_uri}/config.xml").body end |