Skip to content

Instantly share code, notes, and snippets.

@jpsilvashy
Created October 3, 2011 05:41
Show Gist options
  • Save jpsilvashy/1258504 to your computer and use it in GitHub Desktop.
Save jpsilvashy/1258504 to your computer and use it in GitHub Desktop.
Same deploy.rb for a Rails 3 project
require "bundler/capistrano"
load 'deploy/assets'
# Rake helper task.
# http://pastie.org/255489
# http://geminstallthat.wordpress.com/2008/01/27/rake-tasks-through-capistrano/
# http://ananelson.com/said/on/2007/12/30/remote-rake-tasks-with-capistrano/
def run_remote_rake(rake_cmd)
rake_args = ENV['RAKE_ARGS'].to_s.split(',')
cmd = "cd #{fetch(:latest_release)} && #{fetch(:rake, "rake")} RAILS_ENV=#{fetch(:rails_env, "production")} #{rake_cmd}"
cmd += "['#{rake_args.join("','")}']" unless rake_args.empty?
run cmd
set :rakefile, nil if exists?(:rakefile)
end
#############################################################
# Application
#############################################################
set :application, "app_name"
set :deploy_to, "~/apps/#{application}"
# #############################################################
# # Git & Github
# #############################################################
set :scm, :git
set :branch, "master"
set :repository, "git@github.com:my_user/app_name.git"
# set :deploy_via, :remote_cache
# #############################################################
# # Servers
# #############################################################
set :user, "deploy"
set :domain, "example.com"
server domain, :app, :web
role :db, domain, :primary => true
# #############################################################
# # Settings
# #############################################################
default_run_options[:pty] = true
ssh_options[:forward_agent] = true
set :use_sudo, false
set :scm_verbose, true
# Unicorn
set :unicorn_binary, "/usr/local/bin/unicorn"
set :unicorn_config, "#{current_path}/config/unicorn.rb"
set :unicorn_pid, "#{current_path}/tmp/pids/unicorn.pid"
# #############################################################
# # Deploy
# #############################################################
namespace :deploy do
task :start, :roles => :app, :except => { :no_release => true } do
run "cd #{current_path} && #{try_sudo} #{unicorn_binary} -c #{unicorn_config} -E #{rails_env} -D"
end
task :stop, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} kill `cat #{unicorn_pid}`"
end
task :graceful_stop, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} kill -s QUIT `cat #{unicorn_pid}`"
end
task :reload, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} kill -s USR2 `cat #{unicorn_pid}`"
end
task :restart, :roles => :app, :except => { :no_release => true } do
stop
start
end
desc "Restart Resque Workers"
task :restart_workers, :roles => :db do
run_remote_rake "resque:restart_workers"
end
end
namespace :web do
task :disable, :roles => :web do
require 'erb'
template = File.read('app/views/layouts/maintenance.html.erb')
page = ERB.new(template).result(binding)
put page, "/home/deploy/apps/#{application}/shared/system/maintenance.html", :mode => 0777
end
task :enable, :roles => :web do
run "rm #{shared_path}/system/maintenance.html"
end
desc "precompile the assets"
task :precompile_assets, :roles => :web, :except => { :no_release => true } do
run "cd #{release_path} && rm -rf public/assets/*"
run "cd #{release_path} && RAILS_ENV=production bundle exec rake assets:precompile"
end
end
before "deploy:restart", 'web:precompile_assets'
# maintenance page
# before "deploy:restart", 'web:disable'
# after "deploy:restart", 'web:enable'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment