Skip to content

Instantly share code, notes, and snippets.

@squarism
Created Aug 21, 2012
Embed
What would you like to do?
Capistrano with zsh + nginx + passenger + rbenv
# Here's what you can do with this capistrano setup:
# > cap staging deploy:setup
# > cap staging deploy
# (create your database by hand)
# > cap staging deploy:migrate
# this is specific to my app because my fixtures are my seeds :(
# > cap staging fixtures
# same commands above with production:
# > cap production deploy:setup
# @squarism
# see also https://gist.github.com/896445
require 'bundler/capistrano'
# multistage plugin not needed:
# https://github.com/capistrano/capistrano/wiki/2.x-Multiple-Stages-Without-Multistage-Extension
# require 'capistrano/ext/multistage'
set :application, "quickie_mart"
set :using_rvm, false # using rbenv
set :monitorer, false # for dark-capistrano-recipies bluepill after-deploy hook
# set up rbenv shims and paths
# http://henriksjokvist.net/archive/2012/2/deploying-with-rbenv-and-capistrano/
set :bundle_flags, "--deployment --quiet --binstubs --shebang ruby-local-exec"
set :default_environment, {
'PATH' => "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$HOME/bin:$HOME/local/bin:$PATH"
}
# paths
set :use_sudo, false
# git
set :repository, "user@localbox:/full/path/to/git/repo"
set :scm, :git
set :branch, "develop"
set :deploy_via, :remote_cache
# multistage setup and definition
set :stages, %w(production staging)
set :default_stage, "staging"
set :web_server, :nginx
set :server, :passenger
set :is_using_unicorn, false # this doesn't work to prevent the dark-capistrano-recipes prompt
set :application_port, 8080
set :application_uses_ssl, false
# run these stages like `cap staging deploy` etc
desc "Run tasks in staging environment."
task :staging do
set :user, "changeme" # ssh username here
role :web, "stagingbox"
role :app, "stagingbox"
role :db, "stagingbox", :primary => true
# CHANGE MY HARDCODED PATHS
set :deploy_to, "/opt/#{application}"
set :nginx_path_prefix, "/usr/local/etc/nginx"
set :nginx_remote_config, "#{nginx_path_prefix}/sites/#{application}.conf"
# restart homebrew managed nginx
# after capistrano_recipe gem prompts
after "deploy:setup" do
task :restart, :roles => :web do
run 'killall "nginx"'
run 'launchctl start homebrew.mxcl.nginx'
end
end
end
desc "Run tasks in production environment."
task :production do
set :user, "changeme"
role :web, "prodbox"
role :app, "prodbox"
role :db, "prodbox", :primary => true
set :deploy_env, 'production'
# CHANGE MY HARDCODED PATHS
set :deploy_to, "/opt/apps/#{application}"
set :nginx_path_prefix, "/usr/local/nginx/conf"
set :nginx_remote_config, "#{nginx_path_prefix}/sites/#{application}.conf"
after "deploy:update_code" do
run "ln -s #{deploy_to}/shared/database.yml #{deploy_to}/current/config/database.yml"
end
after "deploy:setup" do
task :restart, :roles => :web do
run '/etc/init.d/nginx restart'
end
end
# this is specific to my app because my fixtures are my seeds :(
task :fixtures do
run "cd #{deploy_to}/current && RAILS_ENV=production rake db:fixtures:load"
end
end
# copy over the nginx virtual host config
# this should only have to run after deploy:setup
after "deploy:setup" do
# todo: copy .conf to nginx sites/* include
# homework for the readers: set up nginx to include *.conf from somewhere and
# create a conf file for your app, then magically scp the conf file over and
# bam, your app is deployed in nginx+passenger
end
# dark-capistrano-recipes adds a lot of nice `cap -T` targets
# I had a difficult time with the current symlink being in my home directory...
# I'm sure it's a good plugin if it works for you, for me on OSX, it did not.
# require 'capistrano_recipes'
after "deploy", "deploy:cleanup" # keep only the last 5 releases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment