Skip to content

Instantly share code, notes, and snippets.

@jeanfbrito
Forked from stevenyap/Capistrano 3.md
Created April 21, 2019 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeanfbrito/be6dccb39cc2bcd43166e99cb371b0ce to your computer and use it in GitHub Desktop.
Save jeanfbrito/be6dccb39cc2bcd43166e99cb371b0ce to your computer and use it in GitHub Desktop.
Capistrano 3 Setup

This guide explains the way to setup a production server using Capistrano.

Setup Capistrano on LOCAL

  • Capistrano is a development gem which assist the developer to run commands on the production server (something like a Heroku toolbelt)
  • Hence, it is installed and configured on developer's computer
# Gemfile

# Use Capistrano for deployment
gem 'capistrano', '~> 3.0.1'
gem 'capistrano-rails', '~> 1.1.0'
gem 'capistrano-rvm', '~> 0.1.0'
gem 'capistrano-bundler'
  • Run bundle install
  • Run cap install STAGES=staging,production
  • Edit the file Capfile
# Capfile
# Make sure the below is uncommented
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails/assets'
require 'capistrano/rails/migrations'
  • Edit the file config/deploy/staging.rb with your own user, domain and application:
set :stage, :staging
role :app, %w{localhost}
role :web, %w{localhost}
role :db,  %w{localhost}

set :application, 'depot'
set :repo_url, 'ssh://deploy@localhost:2222/~/git/depot.git'
set :branch, 'master'
set :deploy_to, '/var/www/stapi/staging'

set :ssh_options, {
  forward_agent: false,
  user: 'root',
  port: 2222
}

Push from local to server

  • This assumes your local is already git initizated (Why shouldn't you!??)
  • After you have capify and edited all the Capistrano config, you should push it to the server:
git remote add staging ssh://user@host/~/git/depot.git
git push staging master

Deploy Capistrano

cap staging deploy:check # make sure everything is okay

# then finally...
cap staging deploy

# or for production
cap production deploy

Seed database

# Add this in config/deploy.rb
# and run 'cap production deploy:seed' to seed your database
desc 'Runs rake db:seed'
task :seed => [:set_rails_env] do
  on primary fetch(:migration_role) do
    within release_path do
      with rails_env: fetch(:rails_env) do
        execute :rake, "db:seed"
      end
    end
  end
end

Restart Apache

# This is found in /config/deploy.rb
# This works on apache/passenger
# To run it, use cap production deploy:restart

namespace :deploy do
  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      # Your restart mechanism here, for example:
      execute :touch, release_path.join('tmp/restart.txt')
    end
  end
end

Inform you when deployment is completed

  • A fun task that works only on Mac to say a message when deployment is completed
# /config/deploy.rb

namespace :deploy do
  desc 'Says a message when deployment is completed'
  task :say do
    system("\\say Capistrano Deployment Completed! Good Job!")
  end
end

after :finished, 'deploy:say'

Issues

  • Nokogiri gem installation error: run yum install libxml2 libxml2-devel libxslt libxslt-devel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment