Skip to content

Instantly share code, notes, and snippets.

@rafaelp
Created March 21, 2012 20:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rafaelp/2152534 to your computer and use it in GitHub Desktop.
Save rafaelp/2152534 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 git@heroku.com:#{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 git@heroku.com:#{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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment