Deploying Your Apps 
(With Capistrano) 
David Underwood @davefp
What is 'Deployment'? 
David Underwood @davefp
Deployment is the 
process of 
distributing and 
starting your app in 
an environment 
David Underwood @davefp
What Steps do we Need? 
David Underwood @davefp
What do you do in 
development? 
David Underwood @davefp
One-Time Operations 
—Provision server. 
—Set up database and other supporting 
services. 
—Create credentials for external 
services. 
David Underwood @davefp
Per-Deploy Operations 
—Fetch new code. 
—Run migrations. 
—Update configuration. 
—Restart (or stop/start) app. 
David Underwood @davefp
Capistrano 
David Underwood @davefp
The Basics 
Add it to your gemfile: 
gem 'capistrano' 
Get it installed: 
bundle install 
bundle exec cap install 
David Underwood @davefp
Anatomy of a Command 
Specify a stage, and a task to run on it: 
cap production deploy 
cap staging deploy:migrate 
cap custom_stage custom_namespace:custom_task 
David Underwood @davefp
Configuration 
Your Capfile contains any includes you 
might need. 
require 'capistrano/setup' 
require 'capistrano/deploy' 
require 'capistrano/rbenv' 
require 'capistrano/rails' 
require 'capistrano3/unicorn' 
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r } 
David Underwood @davefp
Configuration common to all states is 
found in config/deploy.rb. 
lock '3.2.1' 
set :application, 'arenagym' 
set :repo_url, 'git@bitbucket.org:davefp/arenagym.git' 
set :linked_files, %w{config/database.yml .env} 
set :linked_dirs, %w{tmp/pids} 
set :unicorn_config_path, "config/unicorn.rb" 
set :unicorn_rails, true 
set :rbenv_type, :user 
set :rbenv_ruby, '2.1.1' 
set :rbenv_map_bins, %w{rake gem bundle ruby rails} 
set :rbenv_roles, :all # default value 
David Underwood @davefp
Environment/stage specific config in 
named files, e.g. config/deploy/ 
production.rb 
set :stage, :production 
set :deploy_to, '~/apps/arenagym' 
set :branch, 'master' 
set :rails_env, 'production' 
# Simple Role Syntax 
# ================== 
# Supports bulk-adding hosts to roles, the primary 
# server in each group is considered to be the first 
# unless any hosts have the primary property set. 
role :app, %w{deploy@arenagym.net:2222} 
role :web, %w{deploy@arenagym.net:2222} 
role :db, %w{deploy@arenagym.net:2222} 
David Underwood @davefp
Take a look at the 
deploy task: 
Handy Link 
David Underwood @davefp
Linked Files 
Linked files are any files that are not part 
of your codebase that you need to run 
your app. 
They persist from deploy to deploy. 
—database.yml 
—.env, config.yml, or secrets.yml 
—pid files (e.g. for unicorn restarts) 
David Underwood @davefp
Deploys 
Each time you deploy, your code is put in 
a new folder alongside the old stuff. 
When everything is ready, this folder is 
symlinked to the 'current' folder to 
minimise downtime. 
A certain number of previous releases 
are kept around so that you can roll 
back. 
David Underwood @davefp
Tailoring Capistrano to your needs 
—capistrano/rails provides migration and 
asset tasks. 
—capistrano3/unicorn provides app 
server start, stop, restart tasks. 
—Etc.. 
David Underwood @davefp
Questions? 
David Underwood @davefp
Thanks! 
Capistrano site: capistranorb.com 
This presentation: On my GitHub 
My blog: theflyingdeveloper.com 
David Underwood @davefp

Deploying Your Webapps (with Capistrano)

  • 1.
    Deploying Your Apps (With Capistrano) David Underwood @davefp
  • 2.
    What is 'Deployment'? David Underwood @davefp
  • 3.
    Deployment is the process of distributing and starting your app in an environment David Underwood @davefp
  • 4.
    What Steps dowe Need? David Underwood @davefp
  • 5.
    What do youdo in development? David Underwood @davefp
  • 6.
    One-Time Operations —Provisionserver. —Set up database and other supporting services. —Create credentials for external services. David Underwood @davefp
  • 7.
    Per-Deploy Operations —Fetchnew code. —Run migrations. —Update configuration. —Restart (or stop/start) app. David Underwood @davefp
  • 8.
  • 9.
    The Basics Addit to your gemfile: gem 'capistrano' Get it installed: bundle install bundle exec cap install David Underwood @davefp
  • 10.
    Anatomy of aCommand Specify a stage, and a task to run on it: cap production deploy cap staging deploy:migrate cap custom_stage custom_namespace:custom_task David Underwood @davefp
  • 11.
    Configuration Your Capfilecontains any includes you might need. require 'capistrano/setup' require 'capistrano/deploy' require 'capistrano/rbenv' require 'capistrano/rails' require 'capistrano3/unicorn' Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r } David Underwood @davefp
  • 12.
    Configuration common toall states is found in config/deploy.rb. lock '3.2.1' set :application, 'arenagym' set :repo_url, 'git@bitbucket.org:davefp/arenagym.git' set :linked_files, %w{config/database.yml .env} set :linked_dirs, %w{tmp/pids} set :unicorn_config_path, "config/unicorn.rb" set :unicorn_rails, true set :rbenv_type, :user set :rbenv_ruby, '2.1.1' set :rbenv_map_bins, %w{rake gem bundle ruby rails} set :rbenv_roles, :all # default value David Underwood @davefp
  • 13.
    Environment/stage specific configin named files, e.g. config/deploy/ production.rb set :stage, :production set :deploy_to, '~/apps/arenagym' set :branch, 'master' set :rails_env, 'production' # Simple Role Syntax # ================== # Supports bulk-adding hosts to roles, the primary # server in each group is considered to be the first # unless any hosts have the primary property set. role :app, %w{deploy@arenagym.net:2222} role :web, %w{deploy@arenagym.net:2222} role :db, %w{deploy@arenagym.net:2222} David Underwood @davefp
  • 14.
    Take a lookat the deploy task: Handy Link David Underwood @davefp
  • 15.
    Linked Files Linkedfiles are any files that are not part of your codebase that you need to run your app. They persist from deploy to deploy. —database.yml —.env, config.yml, or secrets.yml —pid files (e.g. for unicorn restarts) David Underwood @davefp
  • 16.
    Deploys Each timeyou deploy, your code is put in a new folder alongside the old stuff. When everything is ready, this folder is symlinked to the 'current' folder to minimise downtime. A certain number of previous releases are kept around so that you can roll back. David Underwood @davefp
  • 17.
    Tailoring Capistrano toyour needs —capistrano/rails provides migration and asset tasks. —capistrano3/unicorn provides app server start, stop, restart tasks. —Etc.. David Underwood @davefp
  • 18.
  • 19.
    Thanks! Capistrano site:capistranorb.com This presentation: On my GitHub My blog: theflyingdeveloper.com David Underwood @davefp