Created
December 11, 2012 17:24
-
-
Save jrochkind/4260448 to your computer and use it in GitHub Desktop.
A cap 'stage' for dynamic deploy of feature branch demos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# An experiment for a capistrano 'stage' that can actually be used to | |
# deploy multiple feature branches at once. | |
# Intended to be installed config/deploy/feature_demo , when using cap | |
# multi-stage support: add 'feature_demo' to 'stages' in `config/deploy.rb` too. | |
# Run from a git feature branch `cap feature_demo deploy` to deploy to | |
# single machine we have all our feature demos on. | |
# Name of feature branch will be taken from current git checkout. | |
# Will be deployed to /opt/feature_demos/name_of_feature, from | |
# git branch name_of_feature, AND an httpd conf will be created telling | |
# passenger to deploy that app, woo. | |
# | |
# cap feature_demo deploy | |
# | |
# You WILL need to manually restart apache if this is the first time | |
# you've deployed that feature demo. | |
# | |
# If you want to deploy a different branch than your current checkout: | |
# | |
# cap feature_demo deploy -S branch=some_branch | |
# | |
# If you want to reference the deployed feature by something other than | |
# branch name: | |
# | |
# cap feature_demo deploy -S feature=something_else | |
# | |
# feature name is used for directory path of checkout, Apache | |
# subUri, etc. shouldn't have spaces, should be a nice token. | |
# | |
# | |
# If you are done with a feature demo and want to wipe it from disk etc, | |
# cap feature_demo deploy:cleanup_feature_demo | |
# | |
# (Have to use `feature` or `branch` args matching how you created it) | |
# not using any of our special roles for cronjobs, for a feature | |
# demo we just want a basic app deploy. | |
server "demo.server.tld", :app, :web | |
unless exists? :branch | |
set :branch do | |
branch = `git symbolic-ref -q HEAD`.sub(%r{^refs/heads/}, '').chomp | |
raise CommandError.new("feature_demo cap stage only works if you have a checked out feature branch!") if branch == "master" | |
branch | |
end | |
end | |
unless exists? :feature | |
set(:feature) { fetch :branch } | |
end | |
# have to tell it the SubURI we'll be deploying at for asset | |
# precompilation. | |
set :asset_env, "#{asset_env} RAILS_RELATIVE_URL_ROOT=/#{feature}" | |
set :deploy_to, "/opt/feature_demos/#{feature}" | |
# I can never get capistrano sudo to work anyway, all places | |
# we're going to write to just need to be writeable by the user | |
set :use_sudo, false | |
# We have a 'demo' Rails environment defined, which has config settings | |
# very production-like, but uses non-production database and other external | |
# APIs. | |
set :rails_env, "demo" | |
# For rvm-ified machine on blacklight.library | |
require 'rvm/capistrano' | |
set :rvm_ruby_string, '1.9.3' | |
# No reason to keep old releases in our feature_demo checkouts | |
set :keep_releases, 1 | |
set(:webroot, "/var/www/html") unless exists? :webroot | |
set(:apache_conf_dir, "/etc/httpd/conf.d") unless exists? :apache_conf_dir | |
unless exists? :apache_conf_file | |
set(:apache_conf_file_path) { File.join(apache_conf_dir, "#{feature}.conf") } | |
end | |
namespace :deploy do | |
task :passenger_apache_conf, :roles => [:web] do | |
apache_conf = <<-EOS | |
RackBaseURI /#{feature} | |
<Directory #{deploy_to}> | |
RailsEnv #{rails_env} | |
Options -MultiViews | |
</Directory> | |
EOS | |
put apache_conf, apache_conf_file_path | |
end | |
task :passenger_symlink, :roles => [:web] do | |
run "ln -sf #{File.join(deploy_to, "current", "public")} #{File.join(webroot, feature)}" | |
end | |
end | |
before "deploy:restart", "deploy:passenger_symlink", "deploy:passenger_apache_conf" | |
# Want to clean up a feature branch? | |
# cap deploy:cleanup_feature_demo | |
namespace :deploy do | |
task :cleanup_feature_demo, :roles => [:web] do | |
run "rm -rf #{deploy_to}" | |
run "rm -rf #{File.join(webroot, feature)}" | |
run "rm -rf #{File.join(apache_conf_dir, "#{feature}.conf")}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment