Skip to content

Instantly share code, notes, and snippets.

@J3RN
Forked from njvitto/deploy.rake
Last active February 26, 2020 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save J3RN/157806a91adefbd35cfc to your computer and use it in GitHub Desktop.
Save J3RN/157806a91adefbd35cfc to your computer and use it in GitHub Desktop.
# Deploy and rollback on Heroku in staging and production
task :deploy_staging => ['deploy:set_staging_app',
'deploy:push',
'deploy:migrate',
'deploy:restart']
task :deploy_production => ['deploy:set_production_app',
'deploy:push',
'deploy:migrate',
'deploy:post_deploy',
'deploy:restart']
namespace :deploy do
PRODUCTION_APP = 'YOUR_PRODUCTION_APP_NAME_ON_HEROKU'
STAGING_APP = 'YOUR_STAGING_APP_NAME_ON_HEROKU'
task :set_staging_app do
APP = STAGING_APP
BRANCH = 'development'
end
task :set_production_app do
APP = PRODUCTION_APP
BRANCH = 'master'
end
task :push do
puts "Let's check your tests!"
puts `rake db:migrate RAILS_ENV=test`
if system 'bundle exec rspec --fail-fast'
puts "Deploying #{BRANCH} to #{APP}..."
puts `git push -f git@heroku.com:#{APP}.git #{BRANCH}:master`
else
puts "FIX YOUR TESTS"
fail
end
end
task :restart do
puts 'Restarting app servers...'
run_clean "heroku restart --app #{APP}"
end
task :migrate do
puts 'Running database migrations...'
run_clean "heroku run rake db:migrate --app #{APP}"
end
task :off do
puts 'Putting the app into maintenance mode...'
run_clean "heroku maintenance:on --app #{APP}"
end
task :on do
puts 'Taking the app out of maintenance mode...'
run_clean "heroku maintenance:off --app #{APP}"
end
task :post_deploy do
# NOTE: Tasks to be run when the app is deployed to production should be
# put here
Rake::Task["data:preload"].invoke
end
def run_clean command
Bundler.with_clean_env {
puts `#{command}`
}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment