Skip to content

Instantly share code, notes, and snippets.

@cfcosta
Created January 1, 2014 09:42
Show Gist options
  • Save cfcosta/8206470 to your computer and use it in GitHub Desktop.
Save cfcosta/8206470 to your computer and use it in GitHub Desktop.
Yeoman to S3 deploy script.
:s3:
:provider: 'AWS'
:aws_access_key_id: 'AWS_ACCESS_KEY'
:aws_secret_access_key: 'AWS_ACCESS_SECRET'
:region: 'sa-east-1'
:bucket:
:key: 'bucket-name'
:public: true
:metadata:
:cache_expiration: 604800
#!/usr/bin/env ruby
require 'fileutils'
require 'yaml'
require 'zlib'
require 'bundler/setup'
Bundler.require
CONFIG = YAML.load_file(File.expand_path("config/fog.yml", File.dirname(__FILE__)))
def connection
$connection ||= Fog::Storage.new(CONFIG[:s3])
end
def files
list = Dir["dist/**/*"].select { |f| File.file? f }
list << 'index.html'
list
end
def gzip(file)
Zlib::GzipWriter.open("#{file}.gz") do |gz|
gz.mtime = File.mtime(file)
gz.orig_name = file
gz.write IO.binread(file)
end
end
def upload(file)
created_file = bucket.files.new(
key: file,
body: File.read(file + '.gz'),
public: true,
content_type: MIME::Types.type_for(file).first.content_type,
content_encoding: 'gzip',
cache_control: "public, #{CONFIG[:metadata][:cache_expiration]}",
)
created_file.save
end
def cleanup(file)
FileUtils.rm_f(file + '.gz')
end
def bucket
connection.directories.get(CONFIG[:bucket][:key])
end
files.each do |f|
begin
print "Uploading file #{f}..."
gzip(f)
upload(f)
puts " [DONE]"
ensure
cleanup(f)
end
end
source 'https://rubygems.org'
gem 'unf'
gem 'fog'
gem 'mime-types', require: 'mime/types'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment