Created
July 12, 2013 22:38
-
-
Save owainlewis/5988384 to your computer and use it in GitHub Desktop.
A simple automated deployment task for Heroku
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#================================================ | |
# | |
# Application Deployment | |
# | |
# These tasks will deploy your application to Heroku | |
# | |
#================================================ | |
# The name of our application | |
APP = "require" | |
# List of environments and their heroku git remotes | |
ENVIRONMENTS = { | |
staging: APP + '-staging', | |
production: APP + '-production' | |
} | |
# colorization | |
def colorize(color_code, string) | |
"\e[#{color_code}m#{string}\e[0m" | |
end | |
def red(text) | |
colorize(31, text) | |
end | |
def green | |
colorize(32, text) | |
end | |
namespace :deploy do | |
desc "Run migrations" | |
task :migrate, :env do |t, args| | |
puts 'Running database migrations ...' | |
puts `heroku rake db:migrate --app #{ENVIRONMENTS[args[:env].to_sym]}` | |
end | |
desc "Put application into maintenance mode" | |
task :off, :env do |t, args| | |
puts 'Putting the app into maintenance mode ...' | |
puts `heroku maintenance:on --app #{ENVIRONMENTS[args[:env].to_sym]}` | |
end | |
desc "Turn maintenance mode off" | |
task :on, :env do |t, args| | |
puts 'Taking the app out of maintenance mode ...' | |
puts `heroku maintenance:off --app #{ENVIRONMENTS[args[:env].to_sym]}` | |
end | |
# ============================= | |
# Deployment tasks | |
# ============================= | |
ENVIRONMENTS.keys.each do |env| | |
desc "Deploy application to #{env}" | |
task env do | |
current_branch = `git branch | grep ^* | awk '{ print $2 }'`.strip | |
puts "Current git branch is #{current_branch}" | |
Rake::Task['deploy:before_deploy'].invoke(env, current_branch) | |
Rake::Task['deploy:update_code'].invoke(env, current_branch) | |
Rake::Task['deploy:after_deploy'].invoke(env, current_branch) | |
end | |
end | |
task :before_deploy, :env, :branch do |t, args| | |
puts "Deploying #{args[:branch]} to #{args[:env]}" | |
# Ensure the user wants to deploy a non-master branch to production | |
if args[:env].to_sym == :production | |
print red("Are you sure you want to deploy to production? (y/n) ") and STDOUT.flush | |
char = $stdin.getc | |
if char != ?y && char != ?Y | |
puts "Deploy aborted" | |
exit | |
end | |
end | |
end | |
task :after_deploy, :env, :branch do |t, args| | |
puts "Deployment Complete" | |
end | |
task :update_code, :env, :branch do |t, args| | |
FileUtils.cd Rails.root do | |
puts "Updating #{ENVIRONMENTS[args[:env]]} with branch #{args[:branch]}" | |
puts `git push #{args[:env]} master` | |
end | |
end | |
end |
Author
owainlewis
commented
Jul 12, 2013
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment