Skip to content

Instantly share code, notes, and snippets.

@hooptie45
Created October 14, 2010 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hooptie45/625243 to your computer and use it in GitHub Desktop.
Save hooptie45/625243 to your computer and use it in GitHub Desktop.
namespace :deploy do
# this can probably be done better once bundler is updated or you can lock before deploying
# but I really like the way bundler 0.8.1 just updated the gems on the fly from the cache
# I also really don't like deploying all my gems in the system directory, since you can run into
# really messy version conflicts. Keep the gems with the app that needs them, and your system gems clean
desc "expand the gems"
task :gems, :roles => :web, :except => { :no_release => true } do
run "cd #{current_path}; #{shared_path}/bin/bundle unlock"
run "cd #{current_path}; #{shared_path}/bin/bundle install vendor/"
run "cd #{current_path}; #{shared_path}/bin/bundle lock"
end
desc 'Bundle and minify the JS and CSS files'
task :precache_assets, :roles => :app do
root_path = File.expand_path(File.dirname(__FILE__) + '/../..')
assets_path = "#{root_path}/public/assets"
run_locally "#{root_path}/vendor/bin/jammit"
top.upload assets_path, "#{current_path}/public", :via => :scp, :recursive => true
end
end
load 'config/deploy/settings.rb'
load 'config/deploy/symlinks.rb'
load 'config/deploy/passenger.rb'
load 'config/deploy/callbacks.rb'
load 'config/deploy/maintenance.rb'
load 'config/deploy/git.rb'
namespace :deploy do
desc "Deploy app"
task :default do
update
restart
cleanup
end
# note: the shared children is where we setup for the new bundler
# to share the deploy of the gems between deploys. this is kind of bad
# since we're re-installing all of them each deploy. Hope to fix this
# when bundler gets updated to just install new via the cache like 0.8.1
desc "Setup a GitHub-style deployment."
task :setup, :except => { :no_release => true } do
run "[ -d #{current_path} ] || git clone #{repository} #{current_path}"
set (:shared_path) { File.join(deploy_to, shared_dir) }
set :shared_children, %w(public/system log config gems specifications doc bin)
dirs = [shared_path]
dirs += shared_children.map { |d| File.join(shared_path, d) }
run "#{try_sudo} mkdir -p #{dirs.join(' ')} && #{try_sudo} chmod g+w #{dirs.join(' ')}"
end
desc "Update the deployed code."
task :update_code, :except => { :no_release => true } do
run "cd #{current_path}; git fetch origin; git reset --hard origin/#{branch}"
end
desc "Deploy and run migrations"
task :migrations, :except => { :no_release => true } do
update
restart
cleanup
end
desc "write the current version to REVISION"
task :write_revision, :except => { :no_release => true } do
run "cd #{current_path}; git rev-parse HEAD > #{current_path}/REVISION"
end
namespace :rollback do
desc "Rollback"
task :default do
code
end
desc "Rollback a single commit."
task :code, :except => { :no_release => true } do
set :branch, "HEAD^"
default
end
end
# cleanup is a non-op with github deploy strat
desc "Cleanup releases"
task :cleanup do
end
desc "Make sure there is something to deploy"
task :check_revision, :roles => [:web] do
unless `git rev-parse HEAD` == `git rev-parse origin/#{branch}`
puts ""
puts " **************************************************"
puts " * WARNING: HEAD is not the same as origin/#{branch} *"
puts " **************************************************"
puts ""
exit
end
end
end
#############################################################
# Other Tasks
#############################################################
# don't really use this, so it may not work 100% but included
# for completeness
namespace :deploy do
namespace :web do
desc "Serve up a custom maintenance page."
task :disable, :roles => :web do
require 'erb'
on_rollback { run "rm #{shared_path}/system/maintenance.html" }
reason = ENV['REASON']
deadline = ENV['UNTIL']
template = File.read("app/views/layouts/maintenance.html.erb")
page = ERB.new(template).result(binding)
put page, "#{shared_path}/system/maintenance.html", :mode => 0644
end
end
end
namespace :deploy do
# override default tasks to make capistrano happy
desc "Start Passenger"
task :start do
run "touch #{current_path}/tmp/restart.txt"
end
desc "Restart Passenger"
task :restart do
stop
start
end
desc "Stop Passenger"
task :stop do
end
end
#############################################################
# Application
#############################################################
set :application, 'appname'
set :deploy_to, "/home/username/website/"
#use trunk to deploy to production
set :branch, "master"
set :rails_env, "production"
#production
set :domain, 'domainname'
role :app, domain
role :web, domain
role :db, domain, :primary => true
#############################################################
# Git
#############################################################
set :scm, :git
set :repository, "repository-location"
#############################################################
# Servers
#############################################################
set :user, 'dreamhost-username'
#############################################################
# Includes
#############################################################
#############################################################
# Settings
#############################################################
default_run_options[:pty] = true
set :use_sudo, false
#before "deploy", "deploy:check_revision"
set :ssh_options, {:forward_agent => true}
#############################################################
# Post Deploy Hooks
#############################################################
after "deploy:update_code", "deploy:write_revision"
before "deploy:gems", "deploy:symlink"
after "deploy:update_code", "deploy:gems"
#after "deploy:update_code", "deploy:precache_assets" #not working for rails3 yet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment