desc "Generate and deploy assets" task :deploy_assets, :roles => :app do # get the previous timestamp old_timestamp = File.read("config/deploy_timestamp").to_i rescue 0 # generate timestamp into config/deploy_timestamp timestamp = Time.now.to_i File.open("config/deploy_timestamp", 'w') do |f| f.write(timestamp) end # generate minified JS and CSS system('rake asset:packager:build_all') # sync local public/ directory to S3 bucket # the S3 bucket directory should be the timestamp generated above require 'right_aws' s3 = RightAws::S3.new(access_key_id, secret_access_key) bucket = s3.bucket('a-bucket') put_count = 0 copy_count = 0 Dir.glob('public/**/*').each do |f| next if File.directory?(f) key = "#{timestamp}/#{f.gsub(/public\//, '')}" if File.new(f).mtime.to_i > old_timestamp puts "putting #{f} into S3 as #{key}" bucket.put(key, File.read(f), {}, 'public-read') put_count += 1 else old_key = bucket.key("#{old_timestamp}/#{f.gsub(/public\//, '')}") # comment this out until I can replace RightAWS::S3 with something that supports # S3 ACL URI groups. #if old_key.exists? # puts "copying #{old_key} to #{key}" # old_key.copy(key) # copy_count += 1 #else puts "putting #{f} into S3 as #{key}" bucket.put(key, File.read(f), {}, 'public-read') put_count += 1 #end end end puts "done. #{put_count} files uploaded, #{copy_count} keys copied" # add and commit the config/deploy_timestamp file system('git add config/deploy_timestamp') system('git commit -m "deploy_assets complete, updating timestamp"') system('git push') end