Tuesday, October 2, 2012

Heroku for Staging and Production, Without Confusion

   When you want to put up a Ruby on Rails app on Heroku, chances are you configure it so that when you deploy, you use the classic
git push heroku master
   Right?  But... with a little change, you can make it easy to deploy to both staging and production versions, both on Heroku, with little to no confusion, as can happen if you still refer to one or the other as heroku.  Here's the .git/config of one of my projects, with the important bits in bold:
  repositoryformatversion = 0
  filemode = true
  bare = false
  logallrefupdates = true
  ignorecase = true
[remote "staging"]
  url = git@heroku.com:jobsmith-staging.git
  fetch = +refs/heads/*:refs/remotes/heroku/*
[remote "production"]
  url = git@heroku.com:jobsmith-production.git
  fetch = +refs/heads/*:refs/remotes/heroku/*
   (Of course, this is after specifying or changing the names, to be $APP-staging and $APP-production.)  Note the [remote "staging"] and [remote "production"] parts. When I want to push to staging, it's:
git push staging master
and a push to to production is:
git push production master
No need to worry about whether just plain heroku is staging or production.

   When might you not want to do this?  Mainly when you don't want to bother having some other URL for the production site.  I've still got a few little apps that are addressed by their herokuapp.com URLs, like rebalancR (http://rebalancR.herokuapp.com) and The Decider (http://TheDecider.herokuapp.com).  Using those URLs just looks ugly.  In these cases I didn't really care.  The first was a prototype for a client.  The second was basically an early proof that yes I could do Rails even though I didn't yet have any public RoR projects I could show.

  However, the one with the above git config file, is also available as http://TheJobsmith.com.  In fact, my main web site (http://www.codosaur.us) is technically a Rails app -- and also available as http://codosaurus-production.herokuapp.com, with a staging site at http://codosaurus-staging.herokuapp.com (and a git config file very similar to the above).