Skip to content

Instantly share code, notes, and snippets.

@tomislav
Created July 13, 2011 13:12
Show Gist options
  • Save tomislav/1080269 to your computer and use it in GitHub Desktop.
Save tomislav/1080269 to your computer and use it in GitHub Desktop.
Capistrano recipe for Wordpress
default_run_options[:pty] = true
load 'deploy' if respond_to?(:namespace) # cap2 differentiator
require 'capistrano'
require 'rubygems'
require 'railsless-deploy'
set :application, "vault42.org"
set :deploy_to, "/usr/local/www/#{application}"
set :backup_dir, "/home/tomislav/backup"
set :use_sudo, false
set :site_root, "#{deploy_to}/#{current_dir}"
set :keep_releases, 2
server 'vault42.org', :web, :app
role :db, 'vault42.org', :primary => true
# MySQL logins for backup
set :db_user, 'xxxxxx'
set :db_password, 'xxxxxxx'
set :db_database, 'vault42'
# Namespace of nginx's FastCGI cache
set :cache_namespace, 'vault'
# Git settings
set :scm, :git
set :local_repository, "ssh://overseer.vault42.org/home/tomislav/git/#{application}.git"
set :repository, "file:///home/tomislav/git/#{application}.git"
set :branch, 'master'
set :git_shallow_clone, 1
before 'deploy', 'db:backup'
before 'deploy:update_code', 'wordpress:symlinks:setup'
after 'deploy:symlink', 'wordpress:symlinks:update'
after "wordpress:symlinks:update", "deploy:cleanup"
after "deploy:cleanup", "cache:clear"
namespace :deploy do
desc "A macro-task that updates the code and fixes the symlink."
task :default do
transaction do
#make sure the release and htdocs directories exists
run "mkdir -p #{deploy_to}/releases/", :once => true
run "rm -rf #{deploy_to}/public/", :once => true
update_code #update the code from the latest version in the repository
symlink #see symlink task below
end
end
task :update_code, :except => { :no_release => true } do
on_rollback { run "rm -rf #{release_path}; true" }
strategy.deploy!
end
after "symlink" do
puts "Symlinking #{current_path} to #{site_root}."
run "ln -nfs #{release_path} #{site_root}"
end
end
namespace :wordpress do
namespace :symlinks do
desc "Setup application symlinks in the public directory"
task :setup, :roles => [:web] do
run "mkdir -p #{shared_path}/assets/"
end
desc "Link public directories to shared location."
task :update, :roles => [:web] do
#after we have copied the release to the server and symlinked it, we also symlink the assets directory
run "rm -rf #{current_path}/public/assets/"
#symlink assets in the current release to the one in shared
run "ln -nfs #{shared_path}/assets #{current_path}/public/assets"
# replace the db-config.php file with a link to the one in shared
run "if [ ! -f #{shared_path}/wp-config-production.php ] ;then cp #{current_path}/config/wp-config-production.php #{shared_path}/; fi"
run "ln -nfs #{shared_path}/wp-config-production.php #{release_path}/public/wp-config.php"
end
end
end
namespace :db do
desc "Backup MySQL database"
task :backup, :roles => :db, :only => { :primary => true } do
filename = "#{backup_dir}/#{application}.dump.#{Time.now.to_f}.sql.bz2"
on_rollback { run "rm #{filename}" }
run "mysqldump -u #{db_user} -p #{db_database} | bzip2 -c > #{filename}" do |ch, stream, out|
ch.send_data "#{db_password}\n" if out =~ /^Enter password:/
end
#Keep 14 days of backups
run "find #{backup_dir} -type f -mtime +14 -exec rm {} \\;"
end
end
namespace :cache do
desc "Clear nginx cache"
task :clear, :roles => :app do
sudo "/usr/local/bin/clearnginxcache #{cache_namespace}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment