Class: Jenkins::Builder::App

Inherits:
Object
  • Object
show all
Defined in:
lib/jenkins/builder/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service = nil, options = {}) ⇒ App

Returns a new instance of App.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/jenkins/builder/app.rb', line 53

def initialize(service = nil, options={})
  @options = options
  @service = service
  @config = Jenkins::Builder::Config.new(@service)

  if @config.url && @config.username && @config.password
    @client = JenkinsApi::Client.new(server_url: @config.url,
                                     timeout: 1,
                                     http_open_timeout: 1,
                                     http_read_timeout: 1,
                                    username: @config.username,
                                    password: @config.password)
  end
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



51
52
53
# File 'lib/jenkins/builder/app.rb', line 51

def client
  @client
end

#configObject

Returns the value of attribute config.



51
52
53
# File 'lib/jenkins/builder/app.rb', line 51

def config
  @config
end

#optionsObject

Returns the value of attribute options.



51
52
53
# File 'lib/jenkins/builder/app.rb', line 51

def options
  @options
end

Instance Method Details

#all_branchesObject



157
158
159
# File 'lib/jenkins/builder/app.rb', line 157

def all_branches
  @config.branches
end

#all_jobsObject



153
154
155
# File 'lib/jenkins/builder/app.rb', line 153

def all_jobs
  @client.job.list_all
end

#build(job) ⇒ Object



128
129
130
131
132
133
# File 'lib/jenkins/builder/app.rb', line 128

def build(job)
  job_name, branch = job.split(':')
  latest_build_no = @client.job.get_current_build_number(job_name)
  start_build(job_name, branch)
  check_and_show_result(job_name, latest_build_no)
end

#build_each(jobs) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/jenkins/builder/app.rb', line 110

def build_each(jobs)
  if @options[:failfast]
    failed_job = jobs.find { |job| build(job).nil? }
    if failed_job
      exit 1
    else
      exit 0
    end
  else
    results = jobs.map { |job| build(job) }
    if results.any? { |r| r.nil? }
      exit 1
    else
      exit 0
    end
  end
end

#check_and_show_result(job_name, latest_build_no) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/jenkins/builder/app.rb', line 210

def check_and_show_result(job_name, latest_build_no)
  while (build_no = @client.job.get_current_build_number(job_name)) <= latest_build_no
    sleep 1
  end
  printed_size = 0
  if @options[:silent]
    spinner = TTY::Spinner.new(':spinner Building ...', format: :bouncing_ball)
    spinner.auto_spin
  end

  all_console_output = ''

  loop do
    console_output = @client.job.get_console_output(job_name, build_no, 0, 'text')
    all_console_output = console_output['output']
    print console_output['output'][printed_size..-1] unless @options[:silent]
    printed_size = console_output['output'].size
    break unless console_output['more']
    sleep 0.5
  end
  if @options[:silent]
    spinner.stop
  end
  status = @client.job.get_build_details(job_name, build_no)
  msg = "Build Result: [#{status['result']}]"
  pastel = Pastel.new
  if msg =~ /SUCCESS/
    puts pastel.green.bold(msg)
  else
    puts pastel.red.bold(msg)
  end

  if hooks = @config.hooks_of(job_name)
    hooks.each do |hook|
      puts pastel.green('Execute hook: "%s"' % hook)
      begin
        IO.popen(hook, 'r+') do |process|
          process.print(all_console_output)
          process.each { |line| print line }
        end
      rescue Interrupt
        puts
        puts pastel.red('User Canceld hook: "%s"' % hook)
      end
    end
  end

  msg =~ /SUCCESS/
end

#create_alias(name, command) ⇒ Object



89
90
91
92
93
# File 'lib/jenkins/builder/app.rb', line 89

def create_alias(name, command)
  @config.aliases ||= {}
  @config.aliases[name] = command
  @config.save!
end

#delete_alias(name) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/jenkins/builder/app.rb', line 95

def delete_alias(name)
  if @config.aliases.nil? || @config.aliases.empty?
    return
  end

  @config.aliases.delete(name)
  @config.save!
end

#fetch_all_jobsObject



135
136
137
138
# File 'lib/jenkins/builder/app.rb', line 135

def fetch_all_jobs
  refresh_jobs_cache unless validate_jobs_cache
  @config['services'][@service]['jobs-cache']['jobs']
end

#job_detail(job_name) ⇒ Object



161
162
163
# File 'lib/jenkins/builder/app.rb', line 161

def job_detail(job_name)
  @client.job.list_details(job_name)
end

#list_aliasesObject



104
105
106
107
108
# File 'lib/jenkins/builder/app.rb', line 104

def list_aliases
  @config.aliases.each do |k, v|
    puts "%-10s -->     %s" % [k, v]
  end
end


80
81
82
83
84
85
86
87
# File 'lib/jenkins/builder/app.rb', line 80

def print_info(options)
  puts <<-INFO.gsub(/^\s*/, '')
  URL: #{@config.url}
  Username: #{@config.username}
  INFO

  puts "Password: #{@config.password}" if options[:password]
end

#refresh_jobs_cacheObject



140
141
142
143
144
145
146
# File 'lib/jenkins/builder/app.rb', line 140

def refresh_jobs_cache
  @config['services'][@service]['jobs-cache'] = {
    'expire' => (Time.now + 86400*30).strftime('%F %T'),
    'jobs' => all_jobs
  }
  @config.save!
end

#setup(options) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/jenkins/builder/app.rb', line 68

def setup(options)
  validate_credentials!(options)

  config.url = options[:url]
  config.username = options[:username]
  config.branches = options[:branches]
  config.password = options[:password]
  config.save!

  puts 'Credentials setup successfully.'
end

#start_build(job_name, branch) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/jenkins/builder/app.rb', line 169

def start_build(job_name, branch)
  if use_mbranch?(job_name)
    msg = "#{job_name} with branch #{branch}"
    mbranch_param = {name: 'mbranch', value: branch}
    params = mbranch_param.merge(json: {parameter: mbranch_param}.to_json)
    begin
      @client.api_post_request("/job/#{job_name}/build?delay=0sec", params, true)
    rescue JenkinsApi::Exceptions::ForbiddenWithCrumb => e
      start_build_use_ferrum(job_name, branch)
    end
  else
    msg = job_name
    begin
      @client.api_post_request("/job/#{job_name}/build?delay=0sec")
    rescue JenkinsApi::Exceptions::ForbiddenWithCrumb => e
      start_build_use_ferrum(job_name, nil)
    end
  end
  puts Pastel.new.cyan.bold("\n%s%s  %s  %s%s\n" % [' '*30, ''*5, msg, ''*5, ' '*30])
end

#start_build_use_ferrum(job_name, branch) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/jenkins/builder/app.rb', line 190

def start_build_use_ferrum(job_name, branch)
  browser = Ferrum::Browser.new(headless: true)
  browser.goto("#{config.url}/login")
  username_input = browser.at_css('input[name=j_username]')
  password_input = browser.at_css('input[name=j_password]')
  username_input.focus.type(config.username)
  password_input.focus.type(config.password)
  browser.at_css('input[name=Submit]').click
  if branch
    browser.goto("#{config.url}/job/#{job_name}/build?delay=0sec")
    sleep(2)
    browser.evaluate("document.querySelector('#gitParameterSelect').value = '#{branch}'")
    browser.at_css('#yui-gen1-button').click
  else
    browser.goto("#{config.url}/job/#{job_name}/")
    browser.at_css('#tasks a.task-link[onclick^=return\ build]').click
  end
  browser.quit
end

#use_mbranch?(job_name) ⇒ Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/jenkins/builder/app.rb', line 165

def use_mbranch?(job_name)
  job_detail(job_name).to_s =~ /mbranch/
end

#validate_jobs_cacheObject



148
149
150
151
# File 'lib/jenkins/builder/app.rb', line 148

def validate_jobs_cache
  @config['services'][@service]['jobs-cache'] && !@config['services'][@service]['jobs-cache'].empty? && \
    Time.parse(@config['services'][@service]['jobs-cache']['expire']) > Time.now
end