Skip to content

Instantly share code, notes, and snippets.

@joshthewhite
Created March 19, 2012 15:49
Show Gist options
  • Save joshthewhite/2116910 to your computer and use it in GitHub Desktop.
Save joshthewhite/2116910 to your computer and use it in GitHub Desktop.
Capistrano Deploy Script
default_run_options[:pty] = true # Must be set for the password prompt from git to work
set :application, "my-awesome-app"
set :repository, "[email protected]:username/projectname.git"
set :scm, "git"
ssh_options[:forward_agent] = true
set :branch, "master"
set :deploy_via, :remote_cache
set :git_enable_submodules, 1
task :production do
role :web, "192.168.0.1"
role :web, "192.168.0.2"
role :web, "192.168.0.3"
set :stage, 'production'
set :deploy_to, "/#{application}"
set :deploy_via, :remote_cache
after("deploy:symlink", "deploy:restart")
end
task :staging do
role :web, "192.168.1.1"
set :stage, 'staging'
set :deploy_to, "/#{application}-staging"
set :deploy_via, :copy
end
before :deploy do
unless exists?(:deploy_to)
raise "Please invoke me like `cap stage deploy` where stage is production/staging"
end
end
namespace :deploy do
desc "Restart apache."
task :restart do
sudo "/usr/sbin/apachectl graceful"
end
desc "Send a deployment notification"
task :notify do
to = ['[email protected]', '[email protected]']
changes = `git log --no-merges --pretty=format:"* %s %b (%cn)" #{previous_revision}.. | replace '<unknown>' ''`
if not changes.empty?
puts "Sending email to: #{to}"
send_email to, {:body => "Deployed changes:\n#{changes}", :subject => "Deploying to #{stage}"}
end
end
desc "Creates the necessary data directories."
task :createdirs do
run "mkdir -p #{deploy_to}/data/cache #{deploy_to}/data/storage"
run "mkdir -p #{deploy_to}/public_cache/css #{deploy_to}/public_cache/js"
end
desc "Creates symlinks and ensures the necessary directories are writable"
task :dirfix do
run "rm -rf #{current_release}/data #{current_release}/public/cache"
run "ln -s #{deploy_to}/data #{current_release}/data"
run "ln -s #{deploy_to}/public_cache #{current_release}/public/cache"
sudo "chown apache:apache #{deploy_to}/data -R"
sudo "chown apache:apache #{deploy_to}/public_cache -R"
sudo "chown apache:apache #{current_release} -R"
end
desc "Migrates the database changes."
task :doctrine do
changes = capture("#{current_release}/bin/doctrine -e #{stage} -- orm:schema-tool:update --dump-sql")
if !(changes.include? "Nothing to update")
puts "Pending database updates:\n#{changes}"
migrate = Capistrano::CLI.ui.ask "Would you like to update the database (Y/n)? "
if migrate.empty? || migrate != 'n'
run "#{current_release}/bin/doctrine -e #{stage} -- orm:schema-tool:update --force"
end
end
end
end
namespace :logs do
desc "Tails the remote error logs."
task :watch, :on_error => :continue do
run "tail -f /var/log/php_errors.log" do |channel, stream, data|
trap("INT") { puts 'Interupted'; exit 0; }
puts # for an extra line break before the host name
puts "#{channel[:host]}: #{data}"
break if stream == :err
end
end
end
before("deploy:symlink", "deploy:notify")
before("deploy:symlink", "deploy:createdirs")
before("deploy:symlink", "deploy:dirfix")
before("deploy:symlink", "deploy:doctrine")
# Email function for deployment notifications.
require 'net/smtp'
def send_email(to, opts={})
opts[:server] ||= 'localhost'
opts[:from] ||= '[email protected]'
opts[:from_alias] ||= 'Capistrano'
opts[:subject] ||= "Deployment Summary"
opts[:body] ||= "Code has been deployed..."
msg = <<END_OF_MESSAGE
From: #{opts[:from_alias]} <#{opts[:from]}>
To: #{to.join(', ')}
MIME-Version: 1.0
Content-type: text/plain
Subject: #{opts[:subject]}
#{opts[:body]}
END_OF_MESSAGE
Net::SMTP.start(opts[:server]) do |smtp|
smtp.send_message msg, opts[:from], to
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment