Skip to content

Instantly share code, notes, and snippets.

@mitramejia
Created December 24, 2016 16:18
Show Gist options
  • Save mitramejia/27ca7bb9f1f2e1deab72ebed5c905136 to your computer and use it in GitHub Desktop.
Save mitramejia/27ca7bb9f1f2e1deab72ebed5c905136 to your computer and use it in GitHub Desktop.
Deploy trellis projects in a Continuous Integration manner
#!/usr/bin/env ruby
require 'colorize'
require 'git'
# Public: A Class to deploy trellis projects in a Continuous Integration manner.
class Deployment
# Modify these to match your project
TESTS_DIR = '/your/behat/tests/directory'.freeze
TRELLIS_DEPLOY_SCRIPT_DIR = '/path/to/trellis/deploy.sh/script'.freeze
REMOTE_WP_DIR = '/srv/www/myapp.com/current/web/wp'.freeze
WORKING_DIR = '/path/to/trellis/project/myapp.com'.freeze
ENVIRONMENTS = {
# 'branch' => 'environment'
'develop' => 'staging',
'master' => 'production'
}.freeze
# Public: read git repo
GIT = Git.open WORKING_DIR
# Public: Get current branch
CURRENT_BRANCH = GIT.branches.select { |branch| branch.current == true }.first.to_s
# Public: Based on CURRENT_BRANCH set the CURRENT_ENVIRONMENT
if ENVIRONMENTS.key? CURRENT_BRANCH
CURRENT_ENVIRONMENT = ENVIRONMENTS[CURRENT_BRANCH].freeze
else
puts 'You must use one of these branches to deploy: '.yellow
ENVIRONMENTS.keys.each { |key| puts ' ' + key.bold.yellow }
puts "Branch #{CURRENT_BRANCH.bold} was given.".yellow
exit 1
end
# Public: Based on CURRENT_ENVIRONMENT determine the WP_DB_MIGRATE_PROFILE to use
WP_DB_MIGRATE_PROFILE = case CURRENT_ENVIRONMENT
when 'staging' then 1
when 'production' then 2
else 1
end
# Public: Display a notice.
#
# message - The String to be printed.
# :message_color - The colorize color symbol.
#
# Examples
#
# display_notice('Hello this is a success message', :green)
# # => 'Hello this is a success message' :green colored
def display_notice(message, message_color)
puts "\n============================================================================================"
.colorize(color: message_color)
puts message.colorize(color: message_color)
puts "============================================================================================\n"
.colorize(color: message_color)
end
# Public: Check if CURRENT_BRANCH is "develop" or "master". If not, exit script
def check_branch
unless ENVIRONMENTS.key? CURRENT_BRANCH
display_notice "You must be on master or develop branch to deploy, #{CURRENT_BRANCH.bold} given.", :yellow
exit 1
end
end
# Public: Run behat tests for CURRENT_BRANCH, if test fail exit script
def run_tests
display_notice "Running tests for #{CURRENT_BRANCH.bold}", :light_blue
Dir.chdir TESTS_DIR
if system 'bin/behat -n --colors --no-snippets'
display_notice 'Great! All tests passed'.bold, :green
else
display_notice 'One ore more tests failed'.bold, :red
exit 1
end
end
# Public: Push CURRENT_BRANCH, if push fail exit script
def push_current_branch
display_notice "Pushing to #{CURRENT_BRANCH.bold}", :light_blue
unless system "git push origin #{CURRENT_BRANCH}"
display_notice "Push to #{CURRENT_BRANCH} failed".bold, :red
exit 1
end
end
# Public: Deploy project to CURRENT_ENVIRONMENT using trellis ./deploy.sh script, if deploy fails exit script
def deploy_to_environment
Dir.chdir TRELLIS_DEPLOY_SCRIPT_DIR
display_notice "Deploying to #{CURRENT_ENVIRONMENT.bold} environment", :light_blue
unless system "./deploy.sh #{CURRENT_ENVIRONMENT} myapp.com"
display_notice "Deploy to #{CURRENT_ENVIRONMENT.bold} environment failed".bold, :red
exit 1
end
end
# Public: Migrate local WordPress database to CURRENT_ENVIRONMENT. If code excecution on vagrant machine
# fails, exit script.
#
# => Note: This method does not test if wp wpsdb migrate command fails. Requires wp sync db with cli addon to be installed
# => Link: https://github.com/wp-sync-db/wp-sync-db-cli
def migrate_db
wp_migrate_command = "wp wpsdb migrate #{WP_DB_MIGRATE_PROFILE} --path=#{REMOTE_WP_DIR}"
display_notice "Migrating local database to #{CURRENT_ENVIRONMENT.bold} environment", :light_blue
unless system "vagrant ssh -- -t '#{wp_migrate_command}'"
display_notice 'Database Migration failed'.bold, :red
exit 1
end
end
# Public: run all deploy methods
def deploy
check_branch
run_tests
push_current_branch
deploy_to_environment
migrate_db
display_notice 'Deploy complete'.bold, :green
exit 0
end
end
# Run deploy command
Deployment.new.deploy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment