25
Getting Started with Capistrano and Ruby on Rails

Getting Started with Capistrano

Embed Size (px)

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.

Citation preview

Page 1: Getting Started with Capistrano

Getting Started with Capistrano

and Ruby on Rails

Page 2: Getting Started with Capistrano

Automated App Deployment

using ssh

Page 3: Getting Started with Capistrano

Not for server configurationUse Chef or Puppet for that

Page 4: Getting Started with Capistrano

Installation$ gem install capistrano

Bundler:gem ‘capistrano’

Page 5: Getting Started with Capistrano

Capify your application

$ capify .

Page 6: Getting Started with Capistrano

Rails Directory Structure/ |- public/ |- config/ - deploy.rb <--- Capistrano |- application/

Page 7: Getting Started with Capistrano

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

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

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

Example Script (mongrel)

require 'mongrel_cluster/recipes'

namespace :deploy do task :restart do restart_mongrel_cluster endend

Page 11: Getting Started with Capistrano

First-time server setup

$ cap deploy:setup

Page 12: Getting Started with Capistrano

:deploy_to/ |- releases/ - 201205041112 - 201206110904 |- current/ --> 201206110904

Page 13: Getting Started with Capistrano

Deploy the app$ cap deploy

Page 14: Getting Started with Capistrano

Deploy and run migrationscap deploy:migrations

Page 15: Getting Started with Capistrano
Page 16: Getting Started with Capistrano

Rollback to the last version$ cap rollback

Page 17: Getting Started with Capistrano

Capistrano Best Practices

Page 18: Getting Started with Capistrano

1. Create Deploy User$ sudo useradd deploy

(helps scope gems, config, logs, etc.)

Page 19: Getting Started with Capistrano

2. Cleanup Old Deploys

$ cap cleanup

(leaves last 5 deploys, removes the rest)

Page 20: Getting Started with Capistrano

3. Multi-Stage Deploy$ gem install capistrano-ext

Bundler:gem ‘capistrano-ext’

Page 21: Getting Started with Capistrano

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

4. Disable sudoconfig/deploy.rb:

:use_sudo false

Page 23: Getting Started with Capistrano

5. Colorize Capistrano$ gem install capistrano_colors

In config/deploy.rb:require 'capistrano_colors'

Page 24: Getting Started with Capistrano
Page 25: Getting Started with Capistrano

Thank you@launchany

[email protected]://launchany.com