Module: BundleCache

Defined in:
lib/bundle_cache.rb,
lib/bundle_cache/cache.rb,
lib/bundle_cache/version.rb

Constant Summary collapse

VERSION =
"0.0.4"

Class Method Summary collapse

Class Method Details

.cacheObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
67
# File 'lib/bundle_cache/cache.rb', line 5

def self.cache
  # Setup AWS credentials
  AWS.config({
    :access_key_id => ENV["AWS_S3_KEY"],
    :secret_access_key => ENV["AWS_S3_SECRET"],
    :region => ENV["AWS_S3_REGION"] || "us-east-1"
  })

  bucket_name     = ENV["AWS_S3_BUCKET"]
  architecture    = `uname -m`.strip

  file_name       = "#{ENV['BUNDLE_ARCHIVE']}-#{architecture}.tgz"
  file_path       = File.expand_path("~/#{file_name}")
  lock_file       = File.join(File.expand_path(ENV["TRAVIS_BUILD_DIR"].to_s), "Gemfile.lock")
  digest_filename = "#{file_name}.sha2"
  old_digest      = File.expand_path("~/remote_#{digest_filename}")

  puts "Checking for changes"
  bundle_digest = Digest::SHA2.file(lock_file).hexdigest
  old_digest    = File.exists?(old_digest) ? File.read(old_digest) : ""

  if bundle_digest == old_digest
    puts "=> There were no changes, doing nothing"
  else
    if old_digest == ""
      puts "=> There was no existing digest, uploading a new version of the archive"
    else
      puts "=> There were changes, uploading a new version of the archive"
      puts "  => Old checksum: #{old_digest}"
      puts "  => New checksum: #{bundle_digest}"
    end

    puts "=> Preparing bundle archive"
    `cd ~ && tar -cjf #{file_name} .bundle && split -b 5m -a 3 #{file_name} #{file_name}.`

    parts_pattern = File.expand_path(File.join("~", "#{file_name}.*"))
    parts = Dir.glob(parts_pattern).sort

    s3 = AWS::S3.new
    bucket = s3.buckets[bucket_name]

    puts "=> Uploading the bundle"
    gem_archive = bucket.objects[file_name]

    puts "  => Uploading #{parts.length} parts"
    gem_archive.multipart_upload(:acl => :public_read) do |upload|
      parts.each_with_index do |part, index|
        puts "    => Uploading #{part}"
        File.open part do |part_file|
          upload.add_part(part_file)
          puts "      => Uploaded"
        end
      end
    end
    puts "  => Completed multipart upload"

    puts "=> Uploading the digest file"
    hash_object = bucket.objects[digest_filename]
    hash_object.write(bundle_digest, :acl => :public_read, :content_type => "text/plain")
  end

  puts "All done now."
end