Skip to content

Instantly share code, notes, and snippets.

@tganzarolli
Forked from MaherSaif/.gitignore
Created November 6, 2011 04:46
Show Gist options
  • Save tganzarolli/1342487 to your computer and use it in GitHub Desktop.
Save tganzarolli/1342487 to your computer and use it in GitHub Desktop.
rake deploy and rake cache_assets for Heroku (storing JS minimized and gzipped on Amazon S3)
# Add this
public/javascripts/all.js
# On production, don't using caching, instead load all.js from S3
- if Rails.env.production?
= javascript_include_tag 'http://my-bucket-name.s3.amazonaws.com/public/javascripts/all.js'
- else
= javascript_include_tag :defaults, 'jquery.min', 'other-libs', :cache => true
desc "cache assets"
task :cache_assets => :environment do
BUCKET = 'my-bucket-name'
PATHS = ['public/javascripts/all.js']
s3_yml = "#{Rails.root}/config/s3.yml"
s3_config = YAML.load_file(s3_yml)
puts "-----> caching assets..."
PATHS.each do |path|
FileUtils.rm(path) if File.exist?(path)
end
ActionController::Base.perform_caching = true
session = ActionDispatch::Integration::Session.new(Rails.application)
session.get('/')
session.follow_redirect!
AWS::S3::Base.establish_connection!(
:access_key_id => s3_config['production']['access_key_id'],
:secret_access_key => s3_config['production']['secret_access_key']
)
PATHS.each do |f|
next if File.directory?(f)
key = f.gsub(/\.\/public/, '')
puts " -> %s" % key
puts " -> uglify..."
ugly_file = Uglifier.new.compile(File.read(f))
puts " -> gzip..."
gzip_file = StringIO.open('', 'w')
gz = Zlib::GzipWriter.new(gzip_file)
gz.write(ugly_file)
gz.close
puts " -> upload to S3..."
AWS::S3::S3Object.store(
key, gzip_file.string, BUCKET,
:access => :public_read, 'Cache-Control' => 'max-age=315360000', 'Content-Encoding' => 'gzip'
)
end
puts "-----> done"
end
desc "cache assets, push to github, deploy to heroku"
task :deploy => :environment do
puts "-----> running rake cache_assets"
system("rake cache_assets") ? true : fail
puts "-----> pushing to github"
system("git push origin master") ? true : fail
puts "-----> deploying to heroku"
system("git push heroku master") ? true : fail
puts "-----> done"
end
# Add this to your gemfile
group :development do
gem 'therubyracer'
gem 'uglifier'
gem 'aws-s3', :require => 'aws/s3'
end
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# Don't forget to exclude groups for HEROKU because 'therubyracer' does not work on Heroku.
#
# heroku config:add BUNDLE_WITHOUT="development:test"
# More infos about this: http://devcenter.heroku.com/articles/bundler
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment