Deploy compiled assets with Capistrano

By gregmolnar

I guess I am not the only one who deploys to an Amazon VPS with capistrano and facing the fact the compilation of the assets are taking ages(compared to my Macbook Pro). So I decided to compile the assets locally but it is not a good practice to commit the compiled assets into the repo and capistrano deploys from that. I figured out a workaround for this:

namespace :assets do
  desc "compile assets locally and upload before finalize_update"
  task :deploy do
      %x[bundle exec rake assets:clean && bundle exec rake assets:precompile]
    ENV['COMMAND'] = " mkdir '#{release_path}/public/assets'"
    invoke
    upload '/path/to/app/public/assets', "#{release_path}/public/assets", {:recursive => true}
  end
end
after "deploy:finalize_update", "assets:deploy"

This capistrano task will clean up previously compiled assets than compile them and upload the assets folder before capistrano finishes the deploy procedure. Don't forget to exclude the assets folder from your repo!