Getting Started with Capistrano

1,898 views

Published on

Learn how to use Capistrano to automate the deployment of your Ruby on Rails applications. Apply best practices and add-ons for customizing Capistrano.

Published in: Technology

Getting Started with Capistrano

  1. 1. Getting Startedwith Capistrano and Ruby on Rails
  2. 2. Automated App Deployment using ssh
  3. 3. Not for serverconfigurationUse Chef or Puppet for that
  4. 4. Installation$ gem install capistrano Bundler: gem „capistrano‟
  5. 5. Capify your application $ capify .
  6. 6. Rails Directory Structure / |- public/ |- config/ - deploy.rb <--- Capistrano |- application/
  7. 7. Example Scriptset :application, "set your application name here”role :app, "your app-server here”role :web, "your web-server here”role :db, "your db-server here", :primary => true
  8. 8. Example Script (git)set :scm, :gitset :repository, “username@hostname:myapp.git”set :branch, “master”set :deploy_via, :remote_cache
  9. 9. Example Script (Passenger)namespace :deploy do desc "Restarting mod_rails with restart.txt” task :restart, :roles => :app, :except => { :no_release => true } 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 endend
  10. 10. Example Script (mongrel)require mongrel_cluster/recipesnamespace :deploy do task :restart do restart_mongrel_cluster endend
  11. 11. First-time server setup $ cap deploy:setup
  12. 12. :deploy_to/ |- releases/ - 201205041112 - 201206110904 |- current/ --> 201206110904
  13. 13. Deploy the app $ cap deploy
  14. 14. Deploy and run migrations cap deploy:migrations
  15. 15. Rollback to the last version $ cap rollback
  16. 16. Capistrano Best Practices
  17. 17. 1. Create Deploy User $ sudo useradd deploy (helps scope gems, config, logs, etc.)
  18. 18. 2. Cleanup Old Deploys $ cap cleanup (leaves last 5 deploys, removes the rest)
  19. 19. 3. Multi-Stage Deploy $ gem install capistrano-ext Bundler: gem „capistrano-ext‟
  20. 20. config/deploy.rb:set :stages, %w(production staging)set :default_stage, "staging”require capistrano/ext/multistage‟Add users for each stage:$ sudo useradd staging$ sudo useradd production
  21. 21. 4. Disable sudo config/deploy.rb: :use_sudo false
  22. 22. 5. Colorize Capistrano $ gem install capistrano_colors In config/deploy.rb: require capistrano_colors
  23. 23. Thank you @launchanyjames@launchany.com http://launchany.com

×