getting started with capistrano
DESCRIPTION
Learn how to use Capistrano to automate the deployment of your Ruby on Rails applications. Apply best practices and add-ons for customizing Capistrano.TRANSCRIPT
![Page 1: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/1.jpg)
Getting Started with Capistrano
and Ruby on Rails
![Page 2: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/2.jpg)
Automated App Deployment
using ssh
![Page 3: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/3.jpg)
Not for server configurationUse Chef or Puppet for that
![Page 4: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/4.jpg)
Installation$ gem install capistrano
Bundler:gem ‘capistrano’
![Page 5: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/5.jpg)
Capify your application
$ capify .
![Page 6: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/6.jpg)
Rails Directory Structure/ |- public/ |- config/ - deploy.rb <--- Capistrano |- application/
![Page 7: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/7.jpg)
Example Script
set :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
![Page 8: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/8.jpg)
Example Script (git)
set :scm, :git set :repository, “username@hostname:myapp.git”set :branch, “master”set :deploy_via, :remote_cache
![Page 9: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/9.jpg)
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 end
end
![Page 10: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/10.jpg)
Example Script (mongrel)
require 'mongrel_cluster/recipes'
namespace :deploy do task :restart do restart_mongrel_cluster endend
![Page 11: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/11.jpg)
First-time server setup
$ cap deploy:setup
![Page 12: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/12.jpg)
:deploy_to/ |- releases/ - 201205041112 - 201206110904 |- current/ --> 201206110904
![Page 13: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/13.jpg)
Deploy the app$ cap deploy
![Page 14: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/14.jpg)
Deploy and run migrationscap deploy:migrations
![Page 15: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/15.jpg)
![Page 16: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/16.jpg)
Rollback to the last version$ cap rollback
![Page 17: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/17.jpg)
Capistrano Best Practices
![Page 18: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/18.jpg)
1. Create Deploy User$ sudo useradd deploy
(helps scope gems, config, logs, etc.)
![Page 19: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/19.jpg)
2. Cleanup Old Deploys
$ cap cleanup
(leaves last 5 deploys, removes the rest)
![Page 20: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/20.jpg)
3. Multi-Stage Deploy$ gem install capistrano-ext
Bundler:gem ‘capistrano-ext’
![Page 21: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/21.jpg)
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
![Page 22: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/22.jpg)
4. Disable sudoconfig/deploy.rb:
:use_sudo false
![Page 23: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/23.jpg)
5. Colorize Capistrano$ gem install capistrano_colors
In config/deploy.rb:require 'capistrano_colors'
![Page 24: Getting Started with Capistrano](https://reader033.vdocuments.us/reader033/viewer/2022051400/554f8540b4c9052a518b4e79/html5/thumbnails/24.jpg)