Class: Autodeploy::Job
- Inherits:
-
Object
- Object
- Autodeploy::Job
- Defined in:
- lib/autodeploy.rb
Instance Method Summary collapse
- #daemonize(time = 10) ⇒ Object
-
#initialize(configfile) ⇒ Job
constructor
A new instance of Job.
- #job ⇒ Object
- #job_number ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(configfile) ⇒ Job
Returns a new instance of Job.
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/autodeploy.rb', line 23 def initialize(configfile) @config = YAML.load_file(configfile) @client = JenkinsApi::Client.new(@config) unless Dir.exist? @config['download_dir'] Autodeploy.log "Creating #{@config['download_dir']} in #{Dir.pwd}" Dir.mkdir @config['download_dir'] end puts @config ActiveRecord::Base.establish_connection(@config['database']) end |
Instance Method Details
#daemonize(time = 10) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/autodeploy.rb', line 85 def daemonize(time=10) loop do run sleep time end end |
#job ⇒ Object
40 41 42 |
# File 'lib/autodeploy.rb', line 40 def job @job ||= @client.job.get_build_details(@config['job'], job_number) end |
#job_number ⇒ Object
36 37 38 |
# File 'lib/autodeploy.rb', line 36 def job_number @job_number ||= @client.job.get_current_build_number(@config['job']) end |
#run ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/autodeploy.rb', line 44 def run @deployer = Autodeploy::Deployer.new(@config, job) @deployer.run() return unless job['result'] == 'SUCCESS' build_action = job['actions'].detect{|action| action.include?('lastBuiltRevision')} sha = build_action.nil? ? nil : build_action['lastBuiltRevision']['SHA1'] filename = [job_number, sha, job['artifacts'][0]['fileName']].compact.join('-') filepath = File.join(@config['download_dir'], filename) if File.exists?(filepath) Autodeploy.log "#{filepath} already exists" return end uri = URI("#{job['url']}artifact/#{job['artifacts'][0]['relativePath']}") Autodeploy.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" Autodeploy.log "Error downloading artifact: #{resp.code} - #{resp.}" return end Autodeploy.log "Saving #{filename}" open("downloads/#{filename}", "wb") do |file| file.write(resp.body) Autodeploy.log "Completed download - #{filename}" end end end |