Class: Autodeploy::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/autodeploy.rb

Instance Method Summary collapse

Constructor Details

#initializeJob

Returns a new instance of Job.



15
16
17
18
19
20
21
22
23
# File 'lib/autodeploy.rb', line 15

def initialize
    @config = YAML.load_file(File.join('config', 'autodeploy.yml'))
    @client = JenkinsApi::Client.new(@config)

    unless Dir.exist? @config['download_dir']
      log "Creating #{@config['download_dir']} in #{Dir.pwd}"
      Dir.mkdir @config['download_dir']
    end
end

Instance Method Details

#daemonize(time = 30) ⇒ Object



68
69
70
71
72
73
# File 'lib/autodeploy.rb', line 68

def daemonize(time=30)
  loop do
    run
    sleep time
  end
end

#jobObject



29
30
31
# File 'lib/autodeploy.rb', line 29

def job
  @job ||= @client.job.get_build_details(@config['job'], job_number)
end

#job_numberObject



25
26
27
# File 'lib/autodeploy.rb', line 25

def job_number
  @job_number ||= @client.job.get_current_build_number(@config['job'])
end

#log(message) ⇒ Object



11
12
13
# File 'lib/autodeploy.rb', line 11

def log(message)
  puts "[#{DateTime.now.strftime}] #{message}"
end

#runObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/autodeploy.rb', line 33

def run
  return unless job['result'] == 'SUCCESS'

  filename = "#{job_number}-#{job['artifacts'][0]['fileName']}"
  filepath = File.join(@config['download_dir'], filename)

  if File.exists?(filepath)
    log "#{filepath} already exists"
    return
  end

  uri = URI("#{job['url']}artifact/#{job['artifacts'][0]['relativePath']}")

  log "Starting download: #{uri.path}"

  Net::HTTP.start(uri.host, uri.port, use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE) do |http|
    request = Net::HTTP::Get.new uri.request_uri
    request.basic_auth @config['username'], @config['password']

    resp = http.request(request)

    if resp.code != "200"
      log "Error downloading artifact: #{resp.code} - #{resp.message}"
      return
    end

    log "Saving #{filename}"

    open("downloads/#{filename}", "wb") do |file|
      file.write(resp.body)
      log "Completed download - #{filename}"
    end
  end
end