Skip to content

Instantly share code, notes, and snippets.

@traviskroberts
Last active April 17, 2018 17:33
Show Gist options
  • Save traviskroberts/8420421 to your computer and use it in GitHub Desktop.
Save traviskroberts/8420421 to your computer and use it in GitHub Desktop.
Simple rake task to deploy staging and production apps to Heroku. Based on: https://gist.github.com/njvitto/362873

Deploying Via Rake Task

  • Edit the PRODUCTION_APP and STAGING_APP variables at the top of the file.
  • Set the heroku remotes by running rake:set_remotes
  • Once you're ready to deploy, you can run rake deploy:production or rake deploy:staging
# Deploy on Heroku in staging and production
namespace :deploy do
PRODUCTION_APP = 'my-app'
STAGING_APP = 'my-app-staging'
# colors
YELLOW = "\033[33m"
NOCOLOR = "\033[0m"
task :staging => [:set_staging_app, :push_staging, :maintenance_on, :migrate, :maintenance_off]
task :production => [:set_production_app, :push_production, :maintenance_on, :migrate, :maintenance_off]
task :set_staging_app do
APP = STAGING_APP
end
task :set_production_app do
APP = PRODUCTION_APP
end
task :set_remotes do
puts "#{YELLOW}Creating Heroku remotes in git...#{NOCOLOR}"
puts `git remote add heroku git@heroku.com:#{PRODUCTION_APP}.git`
puts `git remote add heroku-staging git@heroku.com:#{STAGING_APP}.git`
end
task :push_staging do
puts "#{YELLOW}Deploying site to Heroku staging server...#{NOCOLOR}"
puts `git push heroku-staging staging:master` # assumes you're using a "staging" branch
end
task :push_production do
puts "#{YELLOW}Deploying site to Heroku production server...#{NOCOLOR}"
puts `git push heroku master`
end
task :restart do
puts "#{YELLOW}Restarting app servers...#{NOCOLOR}"
puts `heroku restart --app #{APP}`
end
task :migrate do
puts "#{YELLOW}Running database migrations...#{NOCOLOR}"
puts `heroku rake db:migrate --app #{APP}`
end
task :maintenance_on do
puts "#{YELLOW}Turning maintenance mode on...#{NOCOLOR}"
puts `heroku maintenance:on --app #{APP}`
end
task :maintenance_off do
puts "#{YELLOW}Turning maintenance mode off...#{NOCOLOR}"
puts `heroku maintenance:off --app #{APP}`
end
end
@maxbeizer
Copy link

How about this?

  task :set_remotes do
    puts `git remote add heroku git@heroku.com:my-app.git`
    puts `git remote add heroku-staging git@heroku.com:my-app-staging.git`
  end

@maxbeizer
Copy link

Also, don't you think it's a little confusing to have the :off task be maintenance:on and vice versa? Shouldn't the :on task be maintenance:on?

@maxbeizer
Copy link

Also, might you want to put it into maintenance before you deploy and take it out of maintenance after, not just during the migration step?

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