Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jensendarren/3839145 to your computer and use it in GitHub Desktop.
Save jensendarren/3839145 to your computer and use it in GitHub Desktop.
Tddium post-build task to deploy into Heroku
def cmd(c)
system c
end
namespace :tddium do
desc "post_build_hook"
task :post_build_hook do
cmd "git reset --hard HEAD" or fail "could not reset git workspace"
dir = File.expand_path("~/.heroku/")
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"
fail "invalid current branch" unless current_branch
FileUtils.mkdir_p(dir) or fail "Could not create #{dir}"
puts "Writing Heroku Credentials"
File.open(File.join(dir, "credentials"), "w") do |f|
f.write([heroku_email, heroku_api_key].join("\n"))
f.write("\n")
end
puts "Pushing to Heroku: #{push_target}..."
cmd "git push #{push_target} #{current_branch}:master --force" or fail "could not push to #{push_target}"
puts "Running Heroku Migrations..."
cmd "heroku run rake db:migrate --app #{app_name}" or fail "failed to run migrations"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment