Last Updated: February 25, 2016
·
4.288K
· tiagocis

Capistrano task for compiling tasks locally

Sometimes we don't want to waste CPU and memory compiling assets on production.

With Capistrano, we can easily compile assets on production including this line on your Capfile:

load 'deploy/assets'

But we don't want this here.

With this little recipe, based on Fernando Blat post (http://fernando.blat.es/post/12563486374/optimize-deploy-time-compiling-your-assets-locally) we can run precompilation locally and then upload them using pure Capistrano DSL and still get an easy to read/modify Capistrano task :)

set :rails_env, 'production'

namespace :assets do
  desc 'Run the precompile task locally and rsync with shared'
  task :precompile do
    run_locally('rm -rf public/assets/*')
    run_locally("RAILS_ENV=#{rails_env} rake assets:precompile")
    run_locally('touch assets.tgz && rm assets.tgz')
    run_locally('tar zcvf assets.tgz public/assets/')
    run_locally('mv assets.tgz public/assets/')
  end

  desc 'Upload precompiled assets'
  task :upload_assets do
    upload "public/assets/assets.tgz", "#{release_path}/assets.tgz"
    run "cd #{release_path}; tar zxvf assets.tgz; rm assets.tgz"
  end
end

before 'deploy:update_code', 'assets:precompile'
after 'deploy:create_symlink', 'assets:upload_assets'

2 Responses
Add your response

does this work for capistrano 3?

over 1 year ago ·

Hi hanzhao. Probably not, but Capistrano 3 still have the "run_locally" commands and the "upload" command too.

If you change the "before" and "after" hooks to the new Capistrano 3 flow you'll get what you need.

Check the new hooks here: http://capistranorb.com/documentation/getting-started/flow/

over 1 year ago ·