Created
April 29, 2016 21:03
-
-
Save paulodiovani/af45b75a083e7576c1bbe47e08b80010 to your computer and use it in GitHub Desktop.
Deploy rake task for Heroku Pipelines
This file contains hidden or 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
namespace :deploy do | |
DEVELOPMENT_APPS = ['example-development'] | |
STAGING_APPS = ['example-staging'] | |
PRODUCTION_APPS = ['example', 'example2', 'example-demo'] | |
REMOTE = ENV['REMOTE_HOST'] || '[email protected]' | |
def shell_cmd(cmd) | |
begin | |
sh "#{cmd}" | |
rescue | |
puts 'Command failed'.yellow | |
exit 0 | |
end | |
end | |
def heroku_cmd(cmd) | |
begin | |
Bundler.with_clean_env do | |
sh "heroku #{cmd}" | |
end | |
rescue | |
puts 'Command failed'.yellow | |
exit 1 | |
end | |
end | |
def choose_app(apps) | |
return apps.first if apps.size == 1 | |
puts 'There are many apps for this environment.'.cyan | |
puts 'Please, choose one:'.cyan | |
puts apps.map { |a| " - #{a}".green } | |
print 'Selected app: '.cyan | |
app = STDIN.gets.strip | |
unless apps.include?(app) | |
puts 'Invalid app selected.'.red | |
exit 0 | |
end | |
app | |
end | |
desc 'Deploy to heroku development app' | |
task :development => [:set_development_app, :check, :off, :push, :migrate, :restart, :on] | |
desc 'Deploy to heroku staging app' | |
task :staging => [:set_staging_app, :check, :off, :promote, :migrate, :restart, :on] | |
desc 'Deploy to heroku production app' | |
task :production => [:set_production_app, :check, :off, :promote, :migrate, :restart, :on] | |
task :set_development_app do | |
APP = choose_app(DEVELOPMENT_APPS) | |
DEPLOY = [APP] | |
print 'Deploy to development will be done through git push to the following app: '.yellow | |
puts APP.cyan | |
end | |
task :set_staging_app do | |
puts 'Deploy to staging is done with heroku pipelines:promote from development app.'.cyan | |
PROMOTE = choose_app(DEVELOPMENT_APPS) | |
DEPLOY = STAGING_APPS | |
print 'The app '.yellow | |
print PROMOTE.green | |
print ' will be promoted to the following apps: '.yellow | |
puts DEPLOY.to_sentence.green | |
end | |
task :set_production_app do | |
puts 'Deploy to production is done with heroku pipelines:promote from staging app.'.cyan | |
PROMOTE = choose_app(STAGING_APPS) | |
DEPLOY = PRODUCTION_APPS | |
print 'The app '.yellow | |
print PROMOTE.green | |
print ' will be promoted to the following apps: '.yellow | |
puts DEPLOY.to_sentence.green | |
end | |
task :check do | |
print 'Are you sure you want to proceed (y/n)? '.yellow | |
input = STDIN.gets.strip | |
if input == 'y' | |
puts 'Lets go'.green | |
else | |
puts [ | |
'Sorry for the confusion.', | |
'More luck next time.', | |
'That was close.' | |
].sample.green | |
exit 0 | |
end | |
end | |
task :promote do | |
puts 'Promoting app to its downstream app(s)' | |
heroku_cmd "pipelines:promote --app #{PROMOTE}" | |
end | |
task :push do | |
puts 'Pushing to heroku git remote' | |
shell_cmd "git push #{REMOTE}:#{APP}.git master" | |
end | |
task :migrate do | |
puts 'Running database migrations...' | |
DEPLOY.each do |app| | |
heroku_cmd "run rake db:migrate --app #{app}" | |
end | |
end | |
task :off do | |
puts 'Putting apps in maintenance mode...' | |
DEPLOY.each do |app| | |
heroku_cmd "maintenance:on --app #{app}" | |
end | |
end | |
task :on do | |
puts 'Taking apps out of maintenance mode...' | |
DEPLOY.each do |app| | |
heroku_cmd "maintenance:off --app #{app}" | |
end | |
end | |
task :restart do | |
puts 'Restarting app servers ...' | |
DEPLOY.each do |app| | |
heroku_cmd "restart --app #{app}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment