This document discusses deployment and using Capistrano to deploy apps. It describes deployment as distributing and starting an app in an environment. Capistrano automates common deployment tasks like fetching code, running migrations, updating configs, and restarting apps. It is configured via a Capfile and deploy.rb file to specify stages, servers, and tasks for each deployment. Capistrano puts new code in folders and symlinks it to "current" to minimize downtime when deploying updates.
5. What do you do in
development?
David Underwood @davefp
6. One-Time Operations
—Provision server.
—Set up database and other supporting
services.
—Create credentials for external
services.
David Underwood @davefp
7. Per-Deploy Operations
—Fetch new code.
—Run migrations.
—Update configuration.
—Restart (or stop/start) app.
David Underwood @davefp
9. The Basics
Add it to your gemfile:
gem 'capistrano'
Get it installed:
bundle install
bundle exec cap install
David Underwood @davefp
10. 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
11. 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
12. 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
13. 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
14. Take a look at the
deploy task:
Handy Link
David Underwood @davefp
15. 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
16. 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
17. 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