Skip to content

Instantly share code, notes, and snippets.

@ografael
Forked from rafaelp/deploy.rake
Created March 21, 2012 23:13
Show Gist options
  • Save ografael/2154026 to your computer and use it in GitHub Desktop.
Save ografael/2154026 to your computer and use it in GitHub Desktop.
Rake task to deploy Rails project to Heroku
# -*- encoding : utf-8 -*-
namespace :heroku do
namespace :deploy do
PRODUCTION_APP = 'nomedoprojeto-production'
STAGING_APP = 'nomedoprojeto-staging'
def run(*cmd)
system(*cmd)
raise "Command #{cmd.inspect} failed!" unless $?.success?
end
def confirm(message)
print "\n#{message}\nAre you sure? [yN] "
raise 'Ok. Bye...' unless STDIN.gets.chomp.downcase == 'y'
end
desc "Deploy to staging"
task :staging do
# This constant is defined to avoid problemd of copying and pasting from one environment to another
APP = STAGING_APP
if ENV['SKIP_BACKUP'] != "true"
puts "-----> Backing up database via Heroku…"
run "heroku pgbackups:capture --expire --app #{APP}"
end
puts "-----> Pushing…"
run "git push [email protected]:#{APP}.git HEAD:master -f"
puts "-----> Migrating…"
run "heroku run rake db:migrate --app #{APP}"
puts "-----> Seeding…"
run "heroku run rake db:seed --app #{APP}"
puts "-----> Restarting…"
run "heroku restart --app #{APP}"
end
desc "Deploy to production"
task :production do
# This constant is defined to avoid problemd of copying and pasting from one environment to another
APP = PRODUCTION_APP
confirm("Going deploy to production.")
if ENV['SKIP_TESTS'] != "true"
puts "-----> Running all specs…"
Rake::Task['spec'].invoke
end
print "\nPut in maintenance mode? [Yn] "
maintenance = (ENV['MAINTENANCE'] == "true" or (STDIN.gets.chomp.downcase == 'y'))
if maintenance
puts "-----> Setting Maintenance on…"
run "heroku maintenance:on --app #{APP}"
puts "-----> Restarting…"
run "heroku restart --app #{APP}"
puts "-----> Waiting 20 seconds to app come back (in maintenance mode)…"
sleep(20)
end
if ENV['SKIP_BACKUP'] != "true"
puts "-----> Backing up database via Heroku…"
run "heroku pgbackups:capture --expire --app #{APP}"
end
iso_date = Time.now.strftime('%Y-%m-%dT%H%M%S')
tag_name = "production-#{iso_date}"
puts "-----> Tagging as #{tag_name}…"
run "git tag #{tag_name} master"
puts "-----> Pushing…"
run "git push origin #{tag_name}"
run "git push [email protected]:#{APP}.git #{tag_name}:master"
puts "-----> Migrating…"
run "heroku run rake db:migrate --app #{APP}"
puts "-----> Seeding…"
run "heroku run rake db:seed --app #{APP}"
if maintenance
puts "Setting Maintenance off…"
run "heroku maintenance:off --app #{APP}"
end
puts "-----> Restarting…"
run "heroku restart --app #{APP}"
end
end
end
@dalezak
Copy link

dalezak commented Dec 15, 2019

Nice rake task @ografael 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment