Capistrano deploy.rb examples

Updated . Posted . Visible to the public. Repeats.

All examples here for deploy RoR application with unicorn, staging machine (where we deploys) available via ssh by ssh_key.

  1. Deploy via remote_cache:

     require 'rvm/capistrano'
     require 'bundler/capistrano'
    
     set :application, '_proj_'
     set :rails_env, 'production'
     set :domain, '_user@your_deploy_domain_'
     set :deploy_to, "_path_to_#{application}"
     set :use_sudo, false
     set :unicorn_conf, "#{deploy_to}/current/config/unicorn.rb"
     set :unicorn_pid, "#{deploy_to}/shared/pids/unicorn.pid"
    
     set :rvm_ruby_string, '_2.0.0@proj_'
    
     set :scm, :git
     set :repository, '_git@your_proj_repo_'
     ssh_options[:forward_agent] = true
     ssh_options[:keys] = File.join(ENV['HOME'], '.ssh', '_ssh_key_for_staging_machine.pem_')
     set :branch, 'master'
     set :deploy_via, :remote_cache
     set :bundle_gemfile,  'Gemfile'
     set :rake_file, "#{deploy_to}/current/Rakefile"
    
     role :web, domain
     role :app, domain
     role :db,  domain, :primary => true
    
     before 'deploy:setup', 'rvm:install_rvm', 'rvm:install_ruby'
    
     before 'deploy:assets:update_asset_mtimes', :roles => :app do
       run "rm -f #{current_release}/config/database.yml"
       run "rm -f #{current_release}/config/application.yml"
       run "ln -s #{deploy_to}/shared/config/database.yml #{current_release}/config/database.yml"
       run "ln -s #{deploy_to}/shared/config/application.yml #{current_release}/config/application.yml"
     end
    
     before 'deploy:db:populate', 'deploy:stop'
    
     after 'deploy:db:populate', 'deploy:start'
    
     namespace :deploy do
       namespace :db do
         task :populate do
           puts 'run db:populate'
           run "cd #{current_release}; RAILS_ENV=#{rails_env} /usr/bin/env rake db:populate -f #{rake_file}"
         end
       end
    
       task :restart do
         run "cd #{current_release}; if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -USR2 `cat #{unicorn_pid}`; else cd #{deploy_to}/current && bundle exec unicorn_rails -c #{unicorn_conf} -E #{rails_env} -D; fi"
       end
       task :start do
         run "cd #{current_release}; bundle exec unicorn_rails -c #{unicorn_conf} -E #{rails_env} -D"
       end
       task :stop do
         run "cd #{current_release}; if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -QUIT `cat #{unicorn_pid}`; fi"
       end
     end
    
  2. Deploy from local folder via rsync:

  • add to your Gemfile next lines into development group:
    gem 'oz_capistrano_rsync_with_remote_cache'
    gem 'capistrano-deploy-scm-passthrough'

  • and change deploy.rb like this:
    require 'rvm/capistrano'
    require 'bundler/capistrano'

    set :application, '_proj_'
    set :rails_env, 'production'
    set :domain, '_user@your_deploy_domain_'
    set :deploy_to, "_path_to_#{application}"
    set :use_sudo, false
    set :unicorn_conf, "#{deploy_to}/current/config/unicorn.rb"
    set :unicorn_pid, "#{deploy_to}/shared/pids/unicorn.pid"

    set :rvm_ruby_string, '_2.0.0@proj_'

    set :scm, :passthrough
    set :repository, '.'
    set :user, '_user_'
    ssh_options[:forward_agent] = true
    ssh_options[:keys] = File.join(ENV['HOME'], '.ssh', '_ssh_key_for_staging_machine.pem_')
    set :rsync_options, '-azr --exclude-from=.rsync-filter --delete --delete-excluded'
    set :deploy_via, :rsync_with_remote_cache
    set :bundle_gemfile,  'Gemfile'
    set :rake_file, "#{deploy_to}/current/Rakefile"
    set :local_cache, '.'
    set :revision, ENV['GIT_COMMIT'] || ENV['SVN_REVISION'] || Time.now

    role :web, domain
    role :app, domain
    role :db,  domain, :primary => true

    before 'deploy:setup', 'rvm:install_rvm', 'rvm:install_ruby'

    before 'deploy:assets:update_asset_mtimes', :roles => :app do
      run "rm -f #{current_release}/config/database.yml"
      run "rm -f #{current_release}/config/application.yml"
      run "ln -s #{deploy_to}/shared/config/database.yml #{current_release}/config/database.yml"
      run "ln -s #{deploy_to}/shared/config/application.yml #{current_release}/config/application.yml"
    end

    before 'deploy:db:populate', 'deploy:stop'

    after 'deploy:db:populate', 'deploy:start'

    namespace :deploy do
      namespace :db do
        task :populate do
          puts 'run db:populate'
          run "cd #{current_release}; RAILS_ENV=#{rails_env} /usr/bin/env rake db:populate -f #{rake_file}"
        end
      end

      task :restart do
        run "cd #{current_release}; if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -USR2 `cat #{unicorn_pid}`; else cd #{deploy_to}/current && bundle exec unicorn_rails -c #{unicorn_conf} -E #{rails_env} -D; fi"
      end
      task :start do
        run "cd #{current_release}; bundle exec unicorn_rails -c #{unicorn_conf} -E #{rails_env} -D"
      end
      task :stop do
        run "cd #{current_release}; if [ -f #{unicorn_pid} ] && [ -e /proc/$(cat #{unicorn_pid}) ]; then kill -QUIT `cat #{unicorn_pid}`; fi"
      end
    end
  • .rsync-filter should looks like:

     #rsync will ignore files in this list
    config/database.yml
    config/application.yml
    log/*
    public/*
    tmp/*
konjoot
Last edit
konjoot
Posted by konjoot to wiki (2014-01-06 15:42)