Tuesday, July 13, 2010

Using Capistrano to deploy Rails App with multiple environments

Firstly, you must install capistrano gem if you haven't


> sudo gem install capistrano


next, run this code " capify . " on your root Rails App to generate deploy.rb file, don't forget follow dot (.) at end of script


/app_root$ capify .


after execute that code, deploy.rb file has locate at root_app/config/deploy.rb
in this case i'll set deploy to multiple environments (production, development and staging)

open up the deploy.rb file and writes follow codes, in this case i used git repository (you can setup that with other repository like svn etc...)


abort "needs capistrano 2" unless respond_to?(:namespace)

set :stages, %w(development staging production)
set :default_stage, "production"
require File.expand_path("#{File.dirname(__FILE__)}/../vendor/gems/capistrano-ext-1.2.1/lib/capistrano/ext/multistage")

# =============================================================================
# REQUIRED VARIABLES
# =============================================================================
set :application, "my_app"  # CHANGE THIS LINE TO USE YOUR WEBSITE'S NAME
set :repository,  "git@you_app_pat.git"  # CHANGE THIS LINE TO USE YOUR SCM REPO
set :directory_path, "/opt/rails_apps/my_app"

# =============================================================================
# ROLES
# =============================================================================
role :web, "sample.com"  # CHANGE THESE LINES TO USE YOUR OCS SERVER NAME
role :app, "sample.com"
role :db,  "sample.com", :primary => true

# =============================================================================
# OPTIONAL VARIABLES
# =============================================================================
set :user, "root"
set :scm, :git
set :scm_username, "arul"
set :scm_passphrase,  Proc.new { Capistrano::CLI.password_prompt('Git Password: ') }

set :runner, "www-data"
set :use_sudo, false
set :branch, "master"
set :deploy_via, :checkout
set :git_shallow_clone, 1
set :scm_verbose, true

default_run_options[:pty] = true
ssh_options[:forward_agent] = true

# =============================================================================
# TASKS
# =============================================================================
namespace :deploy do
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "rm -rf #{current_path}/config/database.yml"
    run "rm -rf #{current_path}/config/app_config.yml"
    run "rm -rf #{current_path}/public/system"
    run "rm -rf #{current_path}/public/images/photos"

    run "ln -s #{directory_path}/shared/config/database.yml #{current_path}/config/database.yml"
    run "ln -s #{directory_path}/shared/config/app_config.yml #{current_path}/config/app_config.yml"
    run "ln -s #{directory_path}/#{stage}/shared/system #{current_path}/public/system"
    run "ln -s #{directory_path}/#{stage}/shared/images/photos #{current_path}/public/images/photos"
    run "touch #{File.join(current_path,'tmp','restart.txt')}"
  end
end


let's make "deploy" directory to root_app/config/ and create new files there (production.rb, development.rb and staging.rb)

/app_root$ mkdir config/deploy
/app_root$ touch config/deploy/production.rb config/deploy/development.rb config/deploy/staging.rb


now, let's begin to coding hee..hee :)
write this codes for every files:
"production.rb" file

set :deploy_to, "/opt/rails_apps/#{application}/production"
set :rails_env, 'production'


"development.rb" file

set :deploy_to, "/opt/rails_apps/#{application}/development"
set :rails_env, 'development'


"staging.rb" file

set :deploy_to, "/opt/rails_apps/#{application}/staging"
set :rails_env, 'staging'


So, that's all
now you can deploy in every environmets, just run these codes:

=> production env

> cap production deploy


=> development env

> cap development deploy


=> staging env

> cap staging deploy


Hope Helpul :)

No comments:

Post a Comment