Skip to content

Instantly share code, notes, and snippets.

##
# Returns the command that will check out +revision+ from the
# repository into directory +destination+. +revision+ can be any
# SHA1 or equivalent (e.g. branch, tag, etc...)
def checkout(revision, destination)
destination = File.join(destination, 'repo')
revision = 'HEAD' if revision =~ /head/i
[ "rm -rf #{destination}",
# you'd obviously have more settings somewhere
set :repository, "git@github.com:defunkt/github.git"
set :branch, "origin/master"
namespace :deploy do
desc "Deploy the MFer"
task :default do
update
restart
cleanup
# port 3000 is haproxy
upstream github {
server 127.0.0.1:3000;
}
# unicorn master opens a unix domain socket
upstream github {
server unix:/data/github/current/tmp/sockets/unicorn.sock;
}
after_fork do |server, worker|
##
# Set the listening HTTP port for each of the workers dynamically
port = 5000 + worker.nr
begin
server.listen("127.0.0.1:#{port}")
rescue Errno::EADDRINUSE
# we couldn't grab the port we want so just keep
# 9 workers and 1 master
worker_processes 9
# Load rails+github.git into the master before forking workers
# for super-fast worker spawn times
preload_app true
# Restart any workers that haven't responded in 30 seconds
timeout 30
RAILS_ROOT ||= ENV["RAILS_ROOT"]
namespace :bundle do
task :all => [ :js, :gist_js, :css ]
task :js do
require 'lib/js_minimizer'
paths = []
paths += Dir[RAILS_ROOT + '/public/javascripts/jquery.*.js'].sort
paths += Dir[RAILS_ROOT + '/public/javascripts/github.*.js'].sort

Poor Man's Deploy

  • Start a Sinatra server on port 4000
  • GET / to that server triggers a git pull and mod_rails restart
  • Hit port 4000 locally after pushing

Why?

desc "Shrink and bundle js and css"
task :bundle, :roles => :app, :except => { :no_release => true } do
run "cd #{current_path}; RAILS_ROOT=#{current_path} rake bundle:all"
end
desc "Shrink the js"
task :minimize_js, :roles => :app, :except => { :no_release => true } do
require 'lib/js_minimizer'
paths = []
paths += Dir['public/javascripts/jquery.*.js'].sort
paths += Dir['public/javascripts/github.*.js'].sort
paths = paths.flatten
put JSMinimizer.minimize_files(*paths), "#{release_path}/public/javascripts/bundle.js"
end