Sample Capistrano Recipe with Assets Precompiling
Dear Developer,
Sub: Full Sample Capistrano Recipe for Rails 3.1
Capistrano provides for easy production deployment. Automation of deployment procedure with Capistrano recipe significantly reduces time spent and chances of human errors on subsquent app deployments. We were amazed by reduction of re-deployment times by well over 70% of developer's time!
Here is a cap recipe sample for rails 3.1.0 app for reference. We hope that you will really enjoy deployment of your rails app, with a neat Capistrano recipe of your own.
First, you should setup a secure ssh configuration with public/private RSA key (recommended). We are on RackSpace (Slicehost), and thankfully they have a neat blog to explain how to do this. With RSA your local machine get authenciated with server in easy and secure way for future deployment.
Here are the steps:
SSH Configuration
Install ssh on server
Change the default port
$ sudo nano /etc/ssh/sshd_config
// Create local keys
$ ssh-keygen -t rsa
// Add public key on server
$ .ssh/authorized_keys
// Make sure to start the server
$ sudo /etc/init.d/ssh restart
Capistrano Receipe
Once into the root directory of your project run capify:
And then prepare your deploy.rb as follows. This capistrano receipe is with unicorn and svn repository.
The scm_verbose when set to true display all the svn commands. This is very useful when you want to know what latest file that has been updated from svn server and with its version information.
Set :use_sudo, false. This makes sure that deployment works without asking for user password.
Since you're on Rails 3.1.0 so before :deploy:assets:precompile, place :bundle_install; This make sure all gems and dependecies are installed before assets are compiled and dumped into the public directory.
..root/config/deploy.rb
# ========================================================
# REQUIRED VARIABLES
# ========================================================
set :application, "domain.com"
set :domain, "domain.com"
# ========================================================
# SCM OPTIONS
# ========================================================
set :scm, :subversion # or :git
set :scm_user, "username" # optional
set :scm_password, "password" # optional
set :repository, "https://subversion.repository.com/project"
set :scm_verbose, true
# ========================================================
# SSH OPTIONS
# ========================================================
set :user, "ssh_user"
set :use_sudo, false
set :port, ssh-port
# ========================================================
# ROLES
# ========================================================
# Modify these values to execute tasks on a different server.
role :web, application
role :app, application
role :db, application, :primary => true
# ========================================================
# CAPISTRANO OPTIONS
# ========================================================
set :deploy_via, :remote_cache
set :rails_env, :development
default_run_options[:pty] = true
set :runner, user
set :group, "admin"
set :deploy_to, "/project/staging"
set :pid, "/project/staging/shared/pids/unicorn.pid"
# This make sure the bundle runs with the task else it gives error
require 'bundler/capistrano'
# ========================================================
# Project CAPISTRANO TASKS
# ========================================================
task :bundle_install, :roles => :app do
run "cd #{release_path} && bundle install --deployment"
endbefore :"deploy:assets:precompile", :bundle_install;
namespace :deploy do
desc "Start Unicorn Server."task :start, :roles => :app do
run " cd #{current_path} && #{unicorn_cmd} -D "
end desc "stop Unicorn Server."
task :stop, :roles => :app do
kill_processes_matching "unicorn master -D"
enddef kill_processes_matching(name)
run "ps -ef | grep '#{name}' | grep -v grep | awk '{print $2}' | xargs kill || echo 'no process with name #{name} found'"
end
desc "Restart Unicorn processes"
task :restart, :roles => :app do
stop
start
endend
# ========================================================
# RECIPE ENDS HERE
# ========================================================
That's it. Hope you'll find it useful.
Cheers,
@bubbles team