Hosting Your Open Source Project on Heroku with Private Configuration

by Buddy Lindsey on December 12, 2011

My first major open source application, the GoDjango site, is teaching me a lot about deployment and maintaining secrecy of custom configuration like username/passwords and api keys. I don’t want them to get into the main branch of the GoDjango github repo, but I want to minimize pain while pushing to heroku and have everything work.

Strategies

There are two key strategies I have found work the “best” with deploying to heroku with custom configuration.

  1. Separate Private Git Branch
  2. Environment Variables

Separate Private Git Branch

The separate branch you will keep locally and when you are ready to launch just do a merge of your master branch to the private one. I called mine deploy so I would just merge master into deploy and push deploy to heroku.

This method actually unnerves me a bit dealing with branches since I want to compulsively merge deploy back into master. It also makes it a bit more difficult for other developers when doing changes locally.

Environment Variables

The other option here is to set environment variables and call them from your code. The good thing about this is you can set them on heroku and each individual developer can set them on their local machine so code can be freely passed around. This is actually heroku’s recommended solution even though it is hard to find it mentioned anywhere. I only found out about it because a friend went a presentation from a Java heroku guy, and that is what they mentioned in the presentation.

I actually like this method and am currently doing it on a couple of projects now, I am also moving GoDjango over to it. It is such a clean easy solution I am not sure why I didn’t think of it before.

Setting and Using Environment Variables

Setting up on Heroku

Make sure you are in the folder of your application and run the following:

heroku config:add SOME_VAR_NAME1=somevalue SOME_VAR_NAME2=someothervalue

Setting up Locally

Add to your .bashrc or .bash_profie:

export SOME_VAR_NAME1=localvalue
export SOME_VAR_NAME2=otherlocalvalue

Use Variables in Ruby

some_config_option = ENV['SOME_VAR_NAME1']
other_config_option = ENV['SOME_VAR_NAME2']

Use Variables in Python

import os

some_config_option = os.environ['SOME_VAR_NAME1']
other_config_option = os.environ['SOME_VAR_NAME2']

Conclusion

As a fan of heroku for the fact it allows me to deploy my sites without thinking too much about “how”> I am happy to have figured these out because deployments were starting to get annoying while maintaining the code as open source. I hope these help others when trying to think through how to configure things like their SMTP gateway without giving away the passwords and/or api keys.

Related Posts:

  • No Related Posts
Was this Helpful?

If you found this article useful you might find others useful as well. Please browse the archives and subscribe to the RSS Feed to stay up-to-date.

Leave a Comment

Previous post:

Next post: