Heading image for post: Heroku Deploy Scripts

Heroku Deploy Scripts

Profile picture of Paul Elliott

Deploying to Heroku is so easy, right? It's just a git push away! And that's true unless you need to run any rake tasks, like a db:migrate. Well the good news is it is really easy to write a quick script to execute all your deployment tasks.

Here is an example task that is similar to what we would use to deploy one of our applications. We would typically have separate tasks for each environment, but they essentially look like this.

namespace :deploy do
  desc 'Deploy the app'
  task :production do
    app = "my-amazing-heroku-app-name"
    remote = "git@heroku.com:#{app}.git"

    system "heroku maintenance:on --app #{app}"
    system "git push #{remote} master"
    system "heroku run rake db:migrate --app #{app}"
    system "heroku maintenance:off --app #{app}"
  end
end

As you can see, it is pretty straightforward stuff. It just wraps up all the commands you need to run.

One important thing to note is that each of these system commands is a separate operation for Heroku. That means that in one call, your code is deployed and the app is restarted. Once that is done, it runs rake db:migrate. Heroku won't actually load the classes in your application until the first request is made. That is important to understand because if you add a table or column in a migration and someone hits the application between the push and the migration, the models will be loaded without that information. 500s will be generated if those resources are accessed. You can easily solve this problem by turning on the maintenance pages while deploying or restarting after the migrate.

More posts about Deployment Git Development

  • Adobe logo
  • Barnes and noble logo
  • Aetna logo
  • Vanderbilt university logo
  • Ericsson logo

We're proud to have launched hundreds of products for clients such as LensRentals.com, Engine Yard, Verisign, ParkWhiz, and Regions Bank, to name a few.

Let's talk about your project