Skip to content

Instantly share code, notes, and snippets.

@v9n
Last active May 31, 2016 02:09
Show Gist options
  • Save v9n/9501819 to your computer and use it in GitHub Desktop.
Save v9n/9501819 to your computer and use it in GitHub Desktop.
Config file for Mina with WP-CLI
require 'mina/git'
# Basic settings:
# domain - The hostname to SSH to.
# deploy_to - Path to deploy into.
# repository - Git repo to clone from. (needed by mina/git)
# branch - Branch name to deploy. (needed by mina/git)
set :user, 'kurei'
set :domain, 'axcoto.com'
# Update this to your website direcoty
set :deploy_to, '/var/webapps/domain/log.axcoto.com'
set :repository, 'https://github.com/axcoto/blog.git'
set :branch, 'master'
set :keep_releases, 1
# Manually create these paths in shared/ (eg: shared/config/database.yml) in your server.
# They will be linked in the 'deploy:link_shared_paths' step.
set :shared_paths, ['wp-config.php', 'wp-content/uploads', 'sitemap.xml', 'sitemap.gz']
# Optional settings:
# set :user, 'foobar' # Username in the server to SSH to.
set :port, '23512' # SSH port number.
# This task is the environment that is loaded for most commands, such as
# `mina deploy` or `mina rake`.
task :environment do
# If you're using rbenv, use this to load the rbenv environment.
# Be sure to commit your .rbenv-version to your repository.
# invoke :'rbenv:load'
# For those using RVM, use this to load an RVM version@gemset.
# invoke :'rvm:use[ruby-1.9.3-p125@default]'
end
# Put any custom mkdir's in here for when `mina setup` is ran.
# For Rails apps, we'll make some of the shared paths that are shared between
# all releases.
task :setup => :environment do
queue! %[mkdir -p "#{deploy_to}/shared/wp-content"]
queue! %[ln -s /srv/http/media/blog/uploads "#{deploy_to}/shared/wp-content/uploads"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/shared/wp-content/uploads"]
queue! %[touch "#{deploy_to}/shared/sitemap.xml"]
queue! %[chmod g+rwx,u+rwx "#{deploy_to}/shared/sitemap.xml"]
queue! %[touch "#{deploy_to}/shared/sitemap.gz"]
queue! %[chmod g+rwx,u+rwx "#{deploy_to}/shared/sitemap.gz"]
queue! %[touch "#{deploy_to}/shared/wp-config.php"]
queue %[echo "-----> Be sure to edit 'shared/wp-config.php'."]
end
desc "Deploys the current version to the server."
task :deploy => :environment do
deploy do
# Put things that will set up an empty directory into a fully set-up
# instance of your project.
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
#invoke :'bundle:install'
#invoke :'rails:db_migrate'
#invoke :'rails:assets_precompile'
to :launch do
queue "touch #{deploy_to}/tmp/restart.txt"
end
invoke :'deploy:cleanup'
end
end
desc 'Clean nginx cache'
task :clean_cache => :environment do
queue! %[ls -la "/var/cache/nginx/blog"]
queue! %[rm -rf "/var/cache/nginx/blog"]
end
desc 'Rollback to previous version'
task :rollback => :environment do
queue %[echo "----> Start to rollback"]
queue %[if [ $(ls #{deploy_to}/releases | wc -l) -gt 1 ]; then echo "---->Relink to previos release" && unlink #{deploy_to}/current && ln -s #{deploy_to}/releases/"$(ls #{deploy_to}/releases | tail -2 | head -1)" #{deploy_to}/current && echo "Remove old releases" && rm -rf #{deploy_to}/releases/"$(ls #{deploy_to}/releases | tail -1)" && echo "$(ls #{deploy_to}/releases | tail -1)" > #{deploy_to}/last_version && echo "Done. Rollback to v$(cat #{deploy_to}/last_version)" ; else echo "No more release to rollback" ; fi]
end
# WP-CLI related task
desc "Show current WordPress version"
task :wp_version => :environment do
queue %[echo "-----> Current version"]
in_directory "#{deploy_to}/current" do
queue "cd #{deploy_to}/current && ./wp-cli.phar core version"
end
end
desc "Toggle a plugin"
task :toggle_plugin , :plugin do |cmd, args|
plugin = args[:plugin]
queue %[echo "----> Toggle plugin #{plugin}"]
in_directory "#{deploy_to}/current" do
queue %[./wp-cli.phar plugin toggle #{plugin}]
end
end
desc "Activate theme"
task :toggle_theme , :theme do |cmd, args|
theme = args[:theme]
queue %[echo "----> Activate theme #{theme}"]
in_directory "#{deploy_to}/current" do
queue %[./wp-cli.phar theme activate #{theme}]
end
end
desc "Export WordPress database to shared/backup"
task :db_export do
# create backup directory if need
backup_to = "#{deploy_to}/shared/backup"
queue %[[ -d #{backup_to} ] || mkdir -p #{backup_to}]
in_directory "#{deploy_to}/current" do
time = Time.new
file = "#{time.year}#{time.month}#{time.day}_#{time.hour}#{time.min}#{time.sec}.sql.gz"
queue %[echo "----> Back up database to #{file}"]
#queue %[./wp-cli.phar db export - | gzip > #{backup_to}/`date +"%d%m%y-%H%M%S"`.sql.gz]
queue %[./wp-cli.phar db export - | gzip > #{backup_to}/#{file}]
end
end
desc "Change domain to a new one"
task :update_domain, :domain do |cmd, args|
domain = args[:domain]
puts domain
in_directory "#{deploy_to}/current" do
queue %[./wp-cli.phar option update home #{domain}]
queue %[./wp-cli.phar option update siteurl #{domain}]
end
end
desc "Generate random post"
task :gen_dummy_post, :howmany do |cmd, args|
howmany = args[:howmany]
puts howmany
in_directory "#{deploy_to}/current" do
queue %[curl http://loripsum.net/api/5 | ./wp-cli.phar post generate --count=#{howmany} --post_content]
end
end
desc "Sample"
task :task_name, :arg, :another_arg do |cmd, args|
puts args[:arg]
puts args[:another_arg]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment