Skip to content

Instantly share code, notes, and snippets.

@harish86
Created May 15, 2013 02:25
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 harish86/5581249 to your computer and use it in GitHub Desktop.
Save harish86/5581249 to your computer and use it in GitHub Desktop.
Capistrano deployment script. After installing capistrano gem, run the command 'capify .' from the root directory of a rails application
# Location: /Capfile
load 'deploy'
# Uncomment if you are using Rails' asset pipeline
load 'deploy/assets'
load 'config/deploy' # remove this line to skip loading any of the default tasks
# Location: /config/deploy.rb
require 'rvm/capistrano'
require 'bundler/capistrano'
require 'capistrano/ext/multistage'
set :stages, %w(staging production) # List of available stages. This requires one file for each stage under config/deploy directory
set :default_stage, "staging"
# Uncomment following lines if whenever is being used to update crontab file
# require 'whenever/capistrano'
#
# set :whenever_command, 'bundle exec whenever'
# set :whenever_environment, defer { rails_env }
# set :whenever_identifier, defer { "#{application}" }
# set(:whenever_update_flags) { "--set environment=#{whenever_environment} --update-crontab #{whenever_identifier}" }
set :repository, "git@example.com:path/to/repo.git"
set :scm, :git
set :scm_user, "git"
set :keep_releases, 3
# Uncomment following if delayed job is used
# set :delayed_job_pid_dir, File.join(shared_path, 'delayed_job_pids') # Ensure delayed_job_pids directory exists under shared directory on server
desc "Symlinks database.yml, mailer.yml file from shared directory into the latest release"
task :symlink_shared, :roles => [:app, :db] do
run "ln -s #{shared_path}/config/database.yml #{latest_release}/config/database.yml"
end
namespace :deploy do
# Following tasks is used to restart a passenger. Modify accordingly if different server is being used
desc "Restart Application"
task :restart, :roles => :app do
run "touch #{current_path}/tmp/restart.txt"
end
[:start, :stop].each do |t|
desc "#{t} task is a no-op with mod_rails"
task t, :roles => :app do ; end
end
# Uncomment following lines if delayed job is used
# desc "Start delayed job"
# task :start_delayed_job, :roles => :app do
# run "cd #{current_path}; RAILS_ENV=#{rails_env} script/delayed_job --pid-dir=#{delayed_job_pid_dir} -n 5 start"
# end
#
# desc "Stop delayed job"
# task :stop_delayed_job, :roles => :app do
# run "cd #{current_path}; RAILS_ENV=#{rails_env} script/delayed_job --pid-dir=#{delayed_job_pid_dir} stop"
# end
#
# desc "Restart delayed job"
# task :restart_delayed_job, :roles => :app do
# run "cd #{current_path}; RAILS_ENV=#{rails_env} script/delayed_job --pid-dir=#{delayed_job_pid_dir} restart"
# end
end
after 'deploy:finalize_update', :symlink_shared
# Location: /config/deploy/production.rb
set :application, "example_production"
set :domain, "example.com"
set :deploy_to, "/var/www/webapps/#{application}"
set :user, "deploy"
set :use_sudo, false
set :rails_env, "production"
role :app, "example.com"
role :web, "example.com"
role :db, "example.com", :primary => true
# Location: /config/deploy/staging.rb
set :application, "example_staging"
set :domain, "example.com"
set :deploy_to, "/var/www/webapps/#{application}"
set :user, "deploy"
set :use_sudo, false
set :rails_env, "staging"
role :app, "example.com"
role :web, "example.com"
role :db, "example.com", :primary => true
@harish86
Copy link
Author

Steps for initial setup:

  1. Run "capify ." from the root of a rails application

  2. Copy the files above and modify the contents according to your requirement

  3. Run "cap [stage] deploy:check". Where [stage] is one of the stages configured in config/deploy.rb (staging, production, etc). This checks for the necessary permissions on server to create directories/files. Fix if there are any permission errors.

  4. Run "cap [stage] deploy:setup". This creates all the necessary directories.

  5. Create database.yml and other symlinked shared files in the shared directory of the application on server (ex: /var/www/webapps/example_staging/shared).

  6. Run "cap [stage] deploy". This will clone the repository to the specified deployment location on server.

  7. Now create and migrate database by executing following commands from the current directory of the application on server (ex: /var/www/webapps/example_staging/current):

    bundle exec rake db:create
    bundle exec rake db:migrate

For future deployments:

Run "cap [stage] deploy:migrations". This command will deploy the application, migrate the database and restarts the server.


If delayed_job is used, use the following commands to start, stop and restart the delayed_job process.

cap [stage] deploy:start_delayed_job
cap [stage] deploy:stop_delayed_job
cap [stage] deploy:restart_delayed_job

Note: Replace [stage] with one of the stages configured in config/deploy.rb (staging, production, etc) in the above commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment