Capistrano staging/production/demo recipe for precompiling assets

Problem

You have upgraded to rails 3.1.x, 3.2.x and you want capistrano to automatically precompile your assets and do the deployment in your various deployment environments (staging. production, demo etc).

Solution

First add a new directory in your server’s shared folder named assets

mkdir /path/to_your_shared_folder/assets

Then add the following line to your Capfile:

load 'deploy/assets'

And finally have your deploy environment file (deploy/staging.rb | deploy/prodution.rb) as follows:

set :rvm_ruby_string, '1.9.3-p125'
set :rvm_type, :user
set :rvm_bin_path, "$HOME/.rvm/bin"

# Add RVM's lib directory to the load path.
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))

# Load RVM's capistrano plugin
require "rvm/capistrano"


server 'xxx.xxx.xxx.xxx', :app, :web, :db, :primary => true

set :rails_env, :staging

after "deploy:update_code", :precompile_assets
  desc "precompile the assets"
  task :precompile_assets, :roles => :app do
    run "cd #{release_path} && RAILS_ENV=#{rails_env} bundle exec rake assets:precompile"
  end