Skip to content

Instantly share code, notes, and snippets.

@atvenu
Forked from blairio/heroku.rake
Created September 3, 2013 20:38
Show Gist options
  • Save atvenu/6429235 to your computer and use it in GitHub Desktop.
Save atvenu/6429235 to your computer and use it in GitHub Desktop.
def migrate_db(remote)
# sh %{heroku maintenance:on --remote #{remote}} || fail
sh %{heroku run --remote #{remote} bundle exec rake db:migrate} || fail
# sh %{heroku maintenance:off --remote #{remote}} || fail
end
def zero_deploy(environment, remote, branch)
original_path = pwd
deploy_name = "#{environment}_#{Time.now.strftime("%Y-%m-%d-%H-%M")}"
puts "-> bundle exec rake deploy:monitor[#{deploy_name}]"
Dir.mktmpdir do |dir|
cd dir
sh %{git clone --no-hardlinks -b #{branch} --local #{original_path}} || fail
cd "qcast"
sh %{git checkout -b #{deploy_name}} || fail
File.open("deploy.txt", 'w') do |f|
f.puts(deploy_name)
f.puts(`git log --pretty=format:'%h' -n 1`)
end
sh %{bundle exec rake assets:precompile RAILS_ENV=#{environment} } || fail
sh %{git add .} || fail
sh %{git commit -m 'Created deploy #{deploy_name}'} || fail
sh %{git push origin #{deploy_name}} || fail
cd original_path
sh %{git push -f #{remote} #{deploy_name}:master} || fail
end
end
namespace :deploy do
desc "Deploy code to heroku"
task :production, :branch do |t, args|
args.with_defaults(:branch => "master")
zero_deploy("production", "heroku", args[:branch])
end
task :staging, :branch do |t, args|
args.with_defaults(:branch => "master")
zero_deploy("staging", "staging", args[:branch])
end
task :monitor, :deployment do |t, args|
while !system %{curl -s http://#{args[:deployment][/production/] ? "www.nextread.me" : "staging-qcast.herokuapp.com"}/admin/monitor | grep #{args[:deployment]}}
print '*'
sleep 5
end
puts " Done \a\a\a"
end
namespace :db do
desc "Migrate the DB on Heroku"
task :production do
migrate_db("heroku")
end
task :staging do
migrate_db("staging")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment