Skip to content

Instantly share code, notes, and snippets.

@blairio
Created September 3, 2013 20:08
Show Gist options
  • Save blairio/6428891 to your computer and use it in GitHub Desktop.
Save blairio/6428891 to your computer and use it in GitHub Desktop.
Rake tasks for cutting a branch and deploying to a Heroku target. This works best when you have the Heroku Labs "preboot" feature enabled. Primarily, this is for deploys that do not have migrations. Migration deploys is still a work in progress and depends on the contents of the migration. (See http://pedro.herokuapp.com/past/2011/7/13/rails_mig…
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
@atvenu
Copy link

atvenu commented Sep 3, 2013

Nice, I'll review and see how we'd like to proceed :D

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