#337 Capistrano Recipes pro
Get the most out of Capistrano by writing specific recipes with ERB templates. Here I show how to deploy to a blank VPS by running just a few Capistrano commands.
- Download:
- source codeProject Files in Zip (105 KB)
- mp4Full Size H.264 Video (35.9 MB)
- m4vSmaller H.264 Video (17.5 MB)
- webmFull Size VP8 Video (19.1 MB)
- ogvFull Size Theora Video (41.5 MB)
Resources
Note: You may find it necessary to manually fix the Nginx init script as shown here.
terminal
ssh root@72.14.183.209 adduser deployer --ingroup admin exit cap deploy:install cap deploy:setup cap deploy:cold cap nginx:start # this may be necessary if it didn't start up properly before
config/deploy.rb
require "bundler/capistrano" load "config/recipes/base" load "config/recipes/nginx" load "config/recipes/unicorn" load "config/recipes/postgresql" load "config/recipes/nodejs" load "config/recipes/rbenv" load "config/recipes/check" server "72.14.183.209", :web, :app, :db, primary: true set :user, "deployer" set :application, "blog" set :deploy_to, "/home/#{user}/apps/#{application}" set :deploy_via, :remote_cache set :use_sudo, false set :scm, "git" set :repository, "git@github.com:ryanb/#{application}.git" set :branch, "master" default_run_options[:pty] = true ssh_options[:forward_agent] = true after "deploy", "deploy:cleanup" # keep only the last 5 releases
config/recipes/base.rb
def template(from, to) erb = File.read(File.expand_path("../templates/#{from}", __FILE__)) put ERB.new(erb).result(binding), to end def set_default(name, *args, &block) set(name, *args, &block) unless exists?(name) end namespace :deploy do desc "Install everything onto the server" task :install do run "#{sudo} apt-get -y update" run "#{sudo} apt-get -y install python-software-properties" end end
config/recipes/nginx.rb
namespace :nginx do desc "Install latest stable release of nginx" task :install, roles: :web do run "#{sudo} add-apt-repository ppa:nginx/stable" run "#{sudo} apt-get -y update" run "#{sudo} apt-get -y install nginx" end after "deploy:install", "nginx:install" desc "Setup nginx configuration for this application" task :setup, roles: :web do template "nginx_unicorn.erb", "/tmp/nginx_conf" run "#{sudo} mv /tmp/nginx_conf /etc/nginx/sites-enabled/#{application}" run "#{sudo} rm -f /etc/nginx/sites-enabled/default" restart end after "deploy:setup", "nginx:setup" %w[start stop restart].each do |command| desc "#{command} nginx" task command, roles: :web do run "#{sudo} service nginx #{command}" end end end
config/recipes/postgresql.rb
set_default(:postgresql_password) { Capistrano::CLI.password_prompt "PostgreSQL Password: " }