Created
January 1, 2014 09:42
-
-
Save cfcosta/8206470 to your computer and use it in GitHub Desktop.
Yeoman to S3 deploy script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
: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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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