DEPLOYING PROJECT USING CAPISTRANO (CAPISTRANO IN RAILS)

We can Deploy project on Digital Ocean, AWS, Transip, Linode server ... using Capistrano

What is Capistrano:-

Capistrano is a remote server automation tool.

It supports the scripting and execution of arbitrary tasks, and includes a set of sane-default deployment workflows.

For that we have to follow simple steps :-

How to Install Capistrano

We can use Capistrano in RAILS 4, RAILS 5 as well as RAILS 2 and 3 .

If we use RAILS 4 and 5 , then we can follow these steps :-

1.  Gemfile:-

       group :development do
             gem "capistrano"
             gem 'net-ssh'
             gem 'capistrano-bundler'
             gem 'capistrano-rails'
             gem 'capistrano-rvm'
             gem 'capistrano-sidekiq'
       end


 

2.   Run the bundle

      cd /path/to/your/project

       bundle

3.   Prepare your Project for Capistrano 

       capify .

      This will create:

  •        Capfile in the root directory of your Rails app
  •        deploy.rb file in the config directory
  •        deploy directory in the config directory

4.   Replace the contents of your Capfile with the following:

      require "capistrano/setup"
      require "capistrano/deploy"
      require 'capistrano/bundler'
      require 'capistrano/rails'
      require 'capistrano/rvm'
      require 'capistrano/rails/migrations'
      require 'capistrano/sidekiq'
      require 'whenever/capistrano'
      require "capistrano/scm/git"
      set :rvm_type, :user
      set :rvm_ruby_version, '2.1.10'

      install_plugin Capistrano::SCM::Git
      Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }




Note :-
     This Capfile loads some pre-defined tasks in to your Capistrano configuration   files to make your deployments hassle-free, such as automatically:
  •     Selecting the correct Ruby
  •     Pre-compiling Assets
  •     Cloning your Git repository to the correct location
  •     Installing new dependencies when your Gemfile has changed

5. Replace the contents of config/deploy.rb with the following, updating fields marked in green with your app and Droplet parameters:

   # config valid only for current version of Capistrano
    lock "3.7.2"

     set :application, "rohitproject"
     # set :scm, :git
     set :repo_url, "git@bitbucket.org:rohit/rohit.git"
     server '136.142.***.***', user: 'root', roles: %w{web app}, my_property: :my_value,     password: '********'
     set :deploy_to, '/var/deploy/rohitproject'


     # Default branch is :master
     # ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp

     # Default value for :pty is false
     set :pty, true

     # Default value for linked_dirs is []
     append :linked_dirs, "log", "tmp/pids", "tmp/cache", "tmp/sockets", "public/system"

     # Default value for keep_releases is 5
     set :keep_releases, 5
     namespace :deploy do
           desc "start resque"
           task "resque:start" => :app do
                 run "cd #{current_path} && RAILS_ENV=#{environment} BACKGROUND=yes PIDFILE=#{shared_path}/pids/resque.pid QUEUE=* nohup bundle exec rake environment resque:work QUEUE='*' >> #{shared_path}/log/resque.out"
            end

            desc "stop resque"
            task "resque:stop" => :app do
                 run "kill -9 `cat #{shared_path}/pids/resque.pid`"
            end

           desc "ReStart resque"
           task "resque:restart" => :app do
                 Rake::Task['deploy:resque:stop'].invoke
                 Rake::Task['deploy:resque:start'].invoke
           end

           desc "start resque scheduler"
           task "resque:start_scheduler" => :app do
                 run "cd #{current_path} && RAILS_ENV=#{environment} DYNAMIC_SCHEDULE=true BACKGROUND=yes PIDFILE=#{shared_path}/pids/resque_scheduler.pid QUEUE=* nohup bundle exec     rake environment resque:scheduler >> #{shared_path}/log/resque_scheduler.out"
           end

            desc "stop resque scheduler"
            task "resque:stop_scheduler" => :app do
                  run "kill -9 `cat #{shared_path}/pids/resque_scheduler.pid`"
            end

            desc "ReStart resque scheduler"
            task "resque:restart" => :app do
                 Rake::Task['deploy:resque:stop_scheduler'].invoke
                 Rake::Task['deploy:resque:start_scheduler'].invoke
            end


           desc 'Restart application'
            task :restart do
                  on roles(:app), in: :sequence, wait: 5 do
                  execute :touch, release_path.join('tmp/restart.txt')
            end
            end

           after :publishing, :restart

           after :restart, :clear_cache do
           on roles(:web), in: :groups, limit: 3, wait: 10 do
                  execute :touch, 'sudo service nginx restart'
           end

          desc "Update crontab with whenever"
          task :update_cron do
                 on roles(:app) do
                        within current_path do
                             execute :bundle, :exec, "whenever --update-crontab #{fetch(:application)}"
                        end
                 end
           end
          after :finishing, 'deploy:update_cron'
    end

    end
   
 

 

6. Replace the contents of config/deploy/production.rb

  •      Uses production as the default environment for your Rails app
       role :app, "136.142.***.***"
       role :web, "136.142.***.***"
       role :db,  "136.142.***.***"

       server '136.142.***.***', user: 'root', roles: %w{web app}, my_property: :my_value


       

7.   Then you can run :- 

      cap production deploy

Note :- 
  • It will run automatically Sidekiq, Whenever, DelayedJobs, Assets precompile, bundle , migrations and everything .
  • If we use same setting then we don't need to run any command on the server for running any such kind of Sidekiq, rake assets:precopile etc.

We can follow the links below for the same :-

http://guides.beanstalkapp.com/deployments/deploy-with-capistrano.html
http://robmclarty.com/blog/how-to-deploy-a-rails-4-app-with-git-and-capistrano
http://www.lugolabs.com/articles/14-schedule-rails-tasks-with-whenever-and-capistrano

IF we use  RAILS 2 AND 3 THEN WE WILL FOLLOW THESE BELOW STEPS :-

Gemfile :- 

group :development do
  gem "capistrano"
  gem 'net-ssh'
end
gem 'rails_12factor', group: :production

Run that commands :- 

gem install capistrano-ext
copify .

Capfile:-

load 'deploy'
load 'config/deploy' # remove this line to skip loading any of the default tasks

Deploy.rb :-

set :application, "pdm"
set :repository,  "git@bitbucket.org:rohit/rohityadav.git"
set :user, "root"
set :domain, "136.***.***.***"
set :use_sudo, false
set :scm, :git
set :branch, "develop"

role :web, "136.***.***.***"                          # Your HTTP server, Apache/etc
role :app, "136.***.***.***"                           # This may be the same as your `Web` server
role :db,  "136.***.***.***", :primary => true 
role :db,  "136.***.***.***"

server "136.***.***.***", :app, :web, :db, :primary => true
set :deploy_to, '/var/deploy/project'

require 'capistrano/ext/multistage'

set :stages, ["staging", "production"]
set :default_stage, "production"

Then run:-

cap production deploy

Comments

Popular posts from this blog

psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

CARRIERWAVE - UPLOAD ANY MEDIA (AUDIO , VIDEO AND IMAGE ) FILE IN RAILS

SELECT COUNTRY WITH STATE AND IT'S CITIES RAILS

RAILS: UPLOADING PHOTOS VIA AMAZON S3 AND PAPERCLIP (UPLOADING FILES TO S3 IN RUBY WITH PAPERCLIP)

SEARCHING USING AJAX IN RAILS

UPLOAD ANY MEDIA FILE IN RAILS USING PAPERCLIP

UNINITIALIZED CONSTANT PAPERCLIP::STORAGE::S3::AWS

CURL COMMAND ON RUBY ON RAILS

CHECK PASSENGER RUBY PATH