Deploying to Heroku using a Git branch
A simple method of deploying a webapp to Heroku without being afraid of publishing your database credentials
If you're making a webapp to host on Heroku (or other similar host) but also want to publish your code as open source you may have wondered how to store your database credentials or other private config within Git to push to the host but not to push to your chosen open source project host.
A lot of the private settings you need to store for your app can be set as environment variables within Heroku, and things like OAuth keys are recommended to be stored in that way, but when you first create your application you are given credentials for their PostgreSQL database and you are instructed to put them into your config/database.yml file.
I'm not claiming to have all the answers, this is just the approach I've taken so far as I couldn't see a better way. I'd love to hear from others that are achieving this more easily.
Traditional deployments
If you were deploying your app with Capistrano onto a normal (non-Git host) you could do this by renaming your empty database.yml file to database.template.yml, and commit that to Git, then adding database.yml to your .gitignore file. This would mean your credentials wouldn't be shared when you push to your public remote, but can be deployed to your app host.
The problem with this approach is that you're not dependent on your development machine in order to deploy, and your credentials and other private config aren't backed up in Git.
Using a branch
The way I've found works for me is to have a 'production-heroku' branch, and put any dedicated config onto that branch. The way I've done it is as follows:
- Add the Heroku repo as a remote:
git remote add heroku git@heroku.com:example.git
- Create the branch:
git branch production-heroku
- Checkout the branch:
git checkout production-heroku
- Add your credentials and other config
- Push the branch to create the remote master branch:
git push heroku production-heroku:master
- set your branch's up-stream to Heroku:
git branch --set-upstream production-heroku heroku/master
Now for future deployments you can deploy using the following few commands:
git checkout production-heroku
git merge master
git push heroku production-heroku:master
git checkout master
This will merge cleanly for most deployments, and will allow you to push your master branch to your public repo for people to fork.
You can of course chain these into a single line with &&
and add it as an alias in your chosen shell config. You can also change the merge command to merge any particular commit from your master branch in case you don't want to deploy your most recent commits.
This leaves you with a bit better off than the traditional deployment scenario, but you can easily push your branch to a private repo (BitBucket offers free private repos).
Just don't push the production branch to your public repo. It shouldn't push normally since the upstream is set, but it won't stop mishaps completely.