SHARE
TWEET

Untitled

a guest May 21st, 2019 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'rubygems'
  2. require 'active_support/all'
  3. require 'date'
  4. require 'aws-sdk'
  5. require 'pry'
  6.  
  7. # If you have a static website that you generate locally, track with git, and
  8. # push to S3, you can use this script to incrementally push new files to your
  9. # bucket. It worked for me when johnholdun.com was hosted with Amazon but use it
  10. # at your own risk!
  11.  
  12. # The directory where your local files live
  13. DIRECTORY = '/changeme/'.freeze
  14.  
  15. AWS_REGION = 'us-east-1'.freeze
  16. ACCESS_KEY_ID = 'CHANGEME'.freeze
  17. SECRET_ACCESS_KEY = 'CHANGEME'.freeze
  18. BUCKET = 'example.com'.freeze
  19. CLOUDFRONT_DISTRIBUTION_ID = 'CHANGEME'.freeze
  20.  
  21. puts 'Checking for unstaged output files...'
  22.  
  23. filenames = `git diff --name-only -- #{DIRECTORY}`.lines.map(&:strip)
  24.  
  25. if filenames.empty?
  26.   puts 'Checking for output files in last commit...'
  27.  
  28.   filenames =
  29.     `git diff HEAD~ --name-only -- #{DIRECTORY}`.lines.map(&:strip)
  30. end
  31.  
  32. filenames.select! { |f| File.exist?(f) }
  33.  
  34. if filenames.empty?
  35.   puts 'Nothing to upload!'
  36.   exit
  37. end
  38.  
  39. puts "Uploading #{filenames.size} file#{'s' unless filenames.size == 1}..."
  40.  
  41. Aws.config.update \
  42.   region: AWS_REGION,
  43.   credentials: Aws::Credentials.new(ACCESS_KEY_ID, SECRET_ACCESS_KEY)
  44.  
  45. s3 = Aws::S3::Client.new
  46.  
  47. filenames.each do |filename|
  48.   puts filename
  49.  
  50.   # TODO: determine content_type from filename?
  51.   content_type = 'text/html'
  52.  
  53.   File.open(filename) do |file|
  54.     s3.put_object \
  55.       bucket: BUCKET,
  56.       key: filename.sub(%r{^#{DIRECTORY}}, ''),
  57.       body: file,
  58.       acl: 'public-read',
  59.       content_type: content_type
  60.   end
  61. end
  62.  
  63. puts 'Done uploading! Time to invalidate the cloud.'
  64.  
  65. cloud_front = Aws::CloudFront::Client.new
  66.  
  67. cloud_front.create_invalidation \
  68.   distribution_id: CLOUDFRONT_DISTRIBUTION_ID,
  69.   invalidation_batch: {
  70.     paths: {
  71.       quantity: 1,
  72.       items: ['/*'],
  73.     },
  74.     caller_reference: Time.now.to_i.to_s
  75.   }
  76.  
  77. puts 'ayy'
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top