Skip to content

Instantly share code, notes, and snippets.

@anatomic
Created November 5, 2013 07:07
Show Gist options
  • Save anatomic/7315047 to your computer and use it in GitHub Desktop.
Save anatomic/7315047 to your computer and use it in GitHub Desktop.
Hooks for tddium to deploy to heroku that I personally find very useful - with plenty of scope for improvement no doubt!
# RAILS_ROOT/lib/task/tddium.rake
#
# http://blog.tddium.com/2012/05/09/heroku-continuous-deployment/
# https://gist.github.com/semipermeable/2639790
# https://github.com/javierjulio
namespace :tddium do
desc "Tddium pre-worker hook that sets up test db"
task :worker_setup_hook do
Bundler.with_clean_env do
system "rake db:test:prepare"
end
end
desc "Tddium post build hook that deploys to Heroku staging if tests pass"
task :post_build_hook do
# This build hook should only run after CI builds.
#
# There are other cases where we'd want to run something after every build,
# or only after manual builds.
abort "Not started from CI" unless ENV["TDDIUM_MODE"] == "ci"
abort "Build failed" unless ENV["TDDIUM_BUILD_STATUS"] == "passed"
heroku_email = ENV["HEROKU_EMAIL"]
heroku_api_key = ENV["HEROKU_API_KEY"]
current_branch = `git symbolic-ref HEAD 2>/dev/null | cut -d"/" -f 3-`.strip
app_name = ENV["HEROKU_APP_NAME"]
push_target = "[email protected]:#{app_name}.git"
# We could improve this script by checking which branch we are on and using
# different heroku apps to push to
puts "Current branch: #{current_branch}"
abort "invalid current branch" unless current_branch
File.open(File.expand_path("~/.netrc"), "a+") do |f|
['api', 'code'].each do |host|
f.puts "machine #{host}.heroku.com"
f.puts " login #{heroku_email}"
f.puts " password #{heroku_api_key}"
end
end
puts "Pushing to heroku: #{push_target}"
system "git push #{push_target} HEAD:master" or abort "could not push to #{push_target}"
# http://stackoverflow.com/questions/14452734/heroku-toolbelt-not-working-with-rake-tasks
# Related issue in db.rake also using Bundler.with_clean_env
Bundler.with_clean_env do
# http://blog.carbonfive.com/2013/08/12/minimizing-downtime-when-deploying-to-heroku/
# This provided the inspiration for these amendments, though we've stopped short of
# precompiling the slug and assets
puts "Checking to see if there are new migrations"
system "git remote add heroku #{push_target}"
system "git fetch heroku"
s = `git diff HEAD heroku/master --name-only -- db | wc -l | tr -d ' \n'`.to_i
if s > 0
puts "Enabling Maintenance mode..."
system "heroku maintenance:on --app #{app_name}" or abort "could not enable for app #{app_name}"
puts "Running Heroku Migrations..."
system "heroku run rake db:migrate --app #{app_name}" or abort "aborted migrations for app #{app_name}"
puts "Disabling Maintenance mode..."
system "heroku maintenance:off --app #{app_name}" or abort "could not disable for app #{app_name}"
puts "Restarting Heroku..."
system "heroku ps:restart --app #{app_name}" or abort "aborted heroku restart for app #{app_name}"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment