Skip to content

Instantly share code, notes, and snippets.

@pryley
Created Feb 6, 2016
Embed
What would you like to do?
Capistrano global WP deploy config
set :application, 'myapp'
set :repo_url, "git@bitbucket.org:geminilabs/#{fetch(:application)}.git"
SSHKit.config.command_map[:composer] = '/srv/users/serverpilot/bin/composer'
set :wpcli, '/srv/users/serverpilot/bin/wp'
# Branch options
# Prompts for the branch name (defaults to current branch)
#ask :branch, -> { `git rev-parse --abbrev-ref HEAD`.chomp }
# Hardcodes branch to always be master
# This could be overridden in a stage config file
set :branch, :master
set :deploy_to, -> { "/srv/users/serverpilot/apps/#{fetch(:application)}" }
# Use :debug for more verbose output when troubleshooting
set :log_level, :info
set :keep_releases, 7
# Apache .htaccess:
# it needs to be added to linked_files so it persists across deploys:
# set :linked_files, fetch(:linked_files, []).push('.env', 'web/.htaccess')
set :linked_files, fetch(:linked_files, []).push('.env', 'wp-cli.yml')
set :linked_dirs, fetch(:linked_dirs, []).push('app/uploads')
set :local_tmp_dir, "/tmp"
set :wp_remote_url, "http://#{fetch(:application)}.com"
set :wp_local_url, "http://#{fetch(:application)}.dev"
# IMPORTANT: Add trailing slash!
set :wp_local_uploads_dir, "/Users/Shared/Dropbox/Sites/Wordpress/#{fetch(:application)}/app/uploads/"
set :wp_remote_uploads_dir, -> {"#{shared_path}/app/uploads/"}
# These options are passed directly to rsync
set :wp_rsync_options, %w[-avz --progress -e 'ssh -p 1337' --exclude=cache/ --exclude=temp/]
# Temporal db dumps path
set :wp_remote_db_file, -> {"#{fetch(:tmp_dir)}/wpcli_database.sql.gz"}
set :wp_local_db_file, -> {"#{fetch(:local_tmp_dir)}/wpcli_database.sql.gz"}
namespace :deploy do
# before :updated, 'composer:update'
# before :reverted, 'composer:update'
desc 'Copy dir after deploy'
task :finished do
on roles(:app) do
execute :ln, '-sf', shared_path.join('.htaccess'), release_path.join('public/.htaccess')
execute :cp, '-r', release_path.join('theme/public'), release_path.join("app/themes/castor-child")
execute :cp, '-r', release_path.join('app'), release_path.join('public')
end
end
end
namespace :composer do
desc "Update composer version"
task :selfupdate do
on roles(:app) do
within release_path do
info "Updating composer version"
execute :composer, :selfupdate
end
end
end
desc "Update composer dependencies"
task :update do
on roles(:app) do
within release_path do
info "Running composer update"
execute :composer, :update
end
end
end
after :update, 'deploy:finished'
end
namespace :sync do
desc 'Push local database and rsync uploads to remote machine'
task :push do
roles(:all).each do |role|
run_locally do
execute :rsync, fetch(:wp_rsync_options), fetch(:wp_local_uploads_dir), "#{role.user}@#{role.hostname}:#{fetch(:wp_remote_uploads_dir)}"
end
end
run_locally do
execute :wp, :db, :export, "- |", :gzip, ">", fetch(:wp_local_db_file)
end
on roles(:app) do
upload! fetch(:wp_local_db_file), fetch(:wp_remote_db_file)
within release_path do
execute :gunzip, "<", fetch(:wp_remote_db_file), "|", fetch(:wpcli), :db, :import, "-"
execute :rm, fetch(:wp_remote_db_file)
execute fetch(:wpcli), "search-replace", fetch(:wp_local_url), fetch(:wp_remote_url), fetch(:wp_args) || "--skip-columns=guid"
end
end
run_locally do
execute :rm, fetch(:wp_local_db_file)
end
end
desc 'Pull remote database and rsync uploads to local machine'
task :pull do
roles(:all).each do |role|
run_locally do
execute :rsync, fetch(:wp_rsync_options), "#{role.user}@#{role.hostname}:#{fetch(:wp_remote_uploads_dir)}", fetch(:wp_local_uploads_dir)
end
end
on roles(:app) do
within release_path do
execute fetch(:wpcli), :db, :export, "- |", :gzip, ">", fetch(:wp_remote_db_file)
download! fetch(:wp_remote_db_file), fetch(:wp_local_db_file)
execute :rm, fetch(:wp_remote_db_file)
end
run_locally do
execute :gunzip, "<", fetch(:wp_local_db_file), "|", :wp, :db, :import, "-"
execute :rm, fetch(:wp_local_db_file)
execute :wp, "search-replace", fetch(:wp_remote_url), fetch(:wp_local_url), fetch(:wp_args) || "--skip-columns=guid"
end
end
end
end
Airbrussh.configure do |config|
config.log_file = nil
config.truncate = false
config.command_output = :stderr
end
# EXAMPLE COMMANDS:
# bundle exec cap production deploy:check
# bundle exec cap production deploy
# bundle exec cap production composer:update
# bundle exec cap production composer:selfupdate
# bundle exec cap production sync:pull
# bundle exec cap production sync:push
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment