Tuesday, 23 July 2013

A simple capistrano setup (multistage)

 step 1

Add this to your Gemfile and run 'bundle install'

group :deployment do
    gem 'capistrano'
    gem 'capistrano-ext'
    gem 'rvm-capistrano'
end

step 2

Create a file called 'config/deploy.rb' 'touch config/deploy.rb' or 'capify .', and add this to your deploy.rb file

set :stages, %w(production) # Example: set :stages, %w(production test staging)
set :default_stage, "production"

require 'capistrano/ext/multistage'
require "capistrano-resque"
require "rvm/capistrano"
require "bundler/capistrano"

step 3

Create a folder inside config/deploy 'mkdir config/deploy', and 'touch config/deploy/production.rb'. Then add this code snippet replace
variables as you want Example: :application, :deploy_to, :user, :repository etc

set :application, "someapp"
set :deploy_to, "/home/rajib/RailsApps/someapp"
set :user, "root"
set :use_sudo, false
set :scm, :git
set :repository, "git@github.com:example/someapp.git"
set :branch, "master"
set :rails_env, "production"
set :keep_releases, 5
ssh_options[:keys] = [File.join(ENV["HOME"], ".ssh", "id_rsa")]

set :rvm_ruby_string, ENV['GEM_HOME'].gsub(/.*\//,"")
set :bundle_without,  [:development]

server "box.example..com", :app, :web, :db, :primary => true

before 'deploy:restart', 'deploy:migrate'
before 'deploy',         'rvm:install_rvm'
before 'deploy',         'rvm:install_ruby'

load 'deploy/assets'

# tasks
namespace :deploy do
 task :start, :roles => :app do
   run "touch #{current_path}/tmp/restart.txt"
 end

 task :stop, :roles => :app do
   # Do nothing.
 end

 desc "Restart Application"
 task :restart, :roles => :app do
   run "touch #{current_path}/tmp/restart.txt"
 end
end

namespace :db do
 task :db_config, :except => { :no_release => true }, :role => :app do
   run "cp #{shared_path}/config/database.yml #{release_path}/config/database.yml"
 end
end

namespace :git do
 namespace :submodule do
   desc "Initializes submodules"
   task :init, :roles => :app do
     invoke_command "cd #{current_path} && git submodule init", :via => run_method
   end

   desc "Updates submodules"
   task :update, :roles => :app do
     invoke_command "cd #{current_path} && git submodule update", :via => run_method
   end

   desc "Initializes and updates submodules"
   task :pull, :roles => :app do
     git.submodule.init
     git.submodule.update
   end
 end
end

after 'deploy',             'deploy:cleanup'
after 'deploy',             'git:submodule:pull'
after 'deploy:update_code', 'deploy:symlink_shared'
after "deploy:finalize_update", "db:db_config"



step 4

Thats it you are almost done, now test your script using 'cap deploy:check' from root of your app, if everything is fine run 'cap deploy:setup'.
Once this command finished successfully, ssh to your production server, go to your deployment path 'cd /home/rajib/RailsApps/someapp', you will be able to see
some folders there 'releases' and 'shared'. now 'cd shared && mkdir config && touch config/database.yml' then 'nano config/database.yml' write your db configuration here
for production ENV save the file and exit from remote server.

step 5

Go to the root of your app and run 'cap deploy', your app will be deployed :)

No comments: