Skip to content

Instantly share code, notes, and snippets.

@jeremyvdw
Forked from PetrKaleta/Rakefile
Created May 26, 2014 15:07
Show Gist options
  • Save jeremyvdw/934cc51dfd0d3f88db1a to your computer and use it in GitHub Desktop.
Save jeremyvdw/934cc51dfd0d3f88db1a to your computer and use it in GitHub Desktop.
# 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