Hashrocket.com / blog

Bg default article large

Heroku Deploy Scripts

posted on and written by Paul Elliott in

Image 100x100 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}"

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.

Posted in Development and tagged with Deployment, Git