Last active
August 29, 2015 14:01
-
-
Save PetrKaleta/b9f3a14e427d60cc36b2 to your computer and use it in GitHub Desktop.
Heroku deploy rake tasks with hooks. Demonstrates how to trigger Sidekiq to quiet or terminate via API before deploy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# List of environments and their heroku git remotes (id: 'remote_name') | |
HEROKU_ENVIRONMENTS = { | |
staging: 'staging-remote-name', | |
production: 'production-remote-name' | |
} | |
namespace :deploy do | |
# Create rake tasks to deploy on Heroku environments | |
# $ rake -T deploy | |
# rake deploy:production # Deploy to production | |
# rake deploy:staging # Deploy to stage | |
HEROKU_ENVIRONMENTS.keys.each do |heroku_env| | |
desc "Deploy to #{ heroku_env }" | |
task heroku_env, :skip_hooks do |t, args| | |
Rake::Task['deploy:before_deploy'].invoke(heroku_env) unless args[:skip_hooks] | |
Rake::Task['deploy:deploy_code'].invoke(heroku_env) | |
Rake::Task['deploy:after_deploy'].invoke(heroku_env) unless args[:skip_hooks] | |
end | |
end | |
# Runs before deploy | |
task :before_deploy, :heroku_env do |t, args| | |
puts "Deploying to #{ args[:heroku_env] }" | |
# Call rake sidekiq:quiet on remote | |
Bundler.with_clean_env { sh "heroku run rake sidekiq:quiet --remote #{ HEROKU_ENVIRONMENTS[args[:heroku_env]] }" } | |
end | |
# Doing actual deploy | |
task :deploy_code, :heroku_env do |t, args| | |
puts "Pushing to #{ HEROKU_ENVIRONMENTS[args[:heroku_env]] }" | |
Bundler.with_clean_env { sh "git push #{ HEROKU_ENVIRONMENTS[args[:heroku_env]] } master" } | |
end | |
# Runs after deploy | |
task :after_deploy, :heroku_env do |t, args| | |
puts 'Deployment complete' | |
end | |
end | |
namespace :sidekiq do | |
# Remotely trigger Sidekiq to quiet or terminate via API, without signals. | |
# This is most useful on JRuby or Heroku which does not support the USR1 'quiet' signal. | |
# Now you can run a rake task like this at the start of your deploy to quiet your set of Sidekiq processes. | |
task :quiet do | |
require 'sidekiq/api' | |
Sidekiq::ProcessSet.new.each(&:quiet!) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment