Auto-deploy your Django app to Heroku with Travis CI on git push
I was setting up a demo site for my latest open-source project, GlucoseTracker, a few weeks back and decided to run it on Heroku. The free tier (called ‘Hobby Dev’) allows 10,000 rows in a PostgreSQL database and 20 concurrent connections, which is more than good enough for me at this point.
I thought about using Jenkins CI to do the deployment to Heroku initially, but as I was installing it on my Rackspace VPS, I kept running into issues where the service would stop responding. It turned out that I didn’t have enough memory on my VPS as it only has 512MB of RAM and I’m running 2 WordPress sites on it with MySQL, a PHP app, a Django app with Gunicorn and PostgreSQL, and Apache2. Jenkins alone would probably need most of that 512MB of RAM.
So I decided to use Travis CI instead as it’s integrated with GitHub where I have my source code hosted. It’s free and setting it up was very easy. Below are the steps.
1. Log-in to your GitHub account, then go to https://travis-ci.org and click the “Sign in with GitHub” link on the top right.
2. Select the repository you want to use use with Travis and enable the Travis “service hook” for that repository.
3. Create a .travis.yml in the root of your repository and insert this line:
deploy:
4. Install the Travis command line tools (requires Ruby, more info here):
gem install travis -v 1.6.3 –no-rdoc –no-ri
5. Assuming that you have the Heroku toolbelt CLI already installed, run this command to automatically insert other parts of the configuration for you including the encrypted Heroku auth token:
travis setup heroku
6. Your config should now look something like this:
deploy: provider: heroku api_key: secure: very_long_encrypted_token app: glucosetracker-demo on: repo: jcalazan/glucose-tracker
7. Add additional settings. For example, mine looks something like this:
language: python deploy: provider: heroku buildpack: python api_key: secure: very_long_encrypted_token strategy: git app: glucosetracker-demo on: repo: jcalazan/glucose-tracker run: - "python glucosetracker/manage.py syncdb --noinput --settings=settings.heroku" - "python glucosetracker/manage.py migrate --all --settings=settings.heroku" - "python glucosetracker/manage.py load_random_glucose_data jsmith --settings=settings.heroku" - restart script: "coverage run --source=. glucosetracker/manage.py test -v 2"
There are 2 important settings here that the Travis documentation for Heroku deployment didn’t mention:language and script.
The language: setting should be set to python so the system knows it’s deploying a Python application and use the proper commands to prepare the environment.
The script: can be just a simple command or something that will execute a script file. The main thing is the command/script must return an exit code of 0 to tell the system that it ran successfully. A good script to run here is your test or build script as it’s executed before the deployment process starts. If you don’t have anything to run, simply enter any command here like “pwd” as it’s required to have something here.
The run: portion is optional and it’s used for executing commands after deployment. In my case, I want to run syncdb again, run South migrations for all apps in case there are changes in the schema, and run a custom management command to populate my database with dummy data.
That’s pretty much it for a basic setup. Travis CI will automatically be notified by GitHub whenever you push new code to the master branch and deployment will automatically start. You will also get email notifications from Travis if a build fails.