Using Travis CI with Django
If you run an open source project and haven't had a look at Travis CI you probably should.
As it happens I'm running an open source project, the footbag database, which is a web app running on the Django platform.
Here's how I got it set up with Travis CI:
.travis.yml file
I'm going with the standard approach of having all the requirements that pip will install in requirements.txt. Travis CI comes with pip already installed so this is particularly easy.
language: python python: - "3.4" services: mysql env: global: - DJANGO_SETTINGS_MODULE="my_site.settings" - PYTHONPATH="/home/travis/build/my_github_name/my_github_project" - PIP_USE_MIRRORS=true - BUILD_ON_TRAVIS=true matrix: - DJANGO=1.7 DB=mysql #commands to install dependencies install: - pip install -q Django==$DJANGO - pip install -q -r requirements.txt before_script: - mysql -e 'create database travis_ci_db;' - python manage.py syncdb --noinput #command to run the test suite script: - python manage.py test
If you need to add anything else the before_script and after_script sections are the place to do it. For example if you wanted to test against PEP8 or run something like PyLint you could put that in before_script.
Django Settings
We will need to change our settings in Django to use the Travis CI database. Note that in the YAML file I added a global environment variable BUILD_ON_TRAVIS. That lets us change only the settings we need for Travis in settings.py:
import os #Use the following live settings to build on Travis CI if os.getenv('BUILD_ON_TRAVIS', None): SECRET_KEY = "SecretKeyForUseOnTravis" DEBUG = False TEMPLATE_DEBUG = True DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'travis_ci_db', 'USER': 'travis', 'PASSWORD': '', 'HOST': '127.0.0.1', } } else: #Non-travis DB configuration goes here
Now all you need to do is get the repository set up and try pushing your changes!
Alternative approach
An alternative approach is to have a travis_settings.py file which is essentially the same as your Django project's settings.py but has the relevant changes for use with the Travis CI databases. Note that this removes the need for the environment variable but instead adds the need for an additional settings file. With this approach the .travis.yml file would look something like this:
language: python python: - "3.4" services: mysql env: global: - DJANGO_SETTINGS_MODULE="my_site.travis_settings" - PYTHONPATH="/home/travis/build/my_github_name/my_github_project" - PIP_USE_MIRRORS=true matrix: - DJANGO=1.7 DB=mysql #commands to install dependencies install: - pip install -q Django==$DJANGO - pip install -q -r requirements.txt before_script: - mysql -e 'create database travis_ci_db;' - python manage.py syncdb --noinput #command to run the test suite script: - python manage.py test
Python 3.7+ compatibility
Recent versions of Python won't work this same way on TravisCI. This is something that came up in my post about A day in the life of a package maintainer where I show you why this is an issue and how to fix it.
The quick summary is that the default image on Travis is too old to run Python 3.7 (see https://github.com/travis-ci/travis-ci/issues/9815) To fix this you have to explicitly set the distro in .travis.yml to be xenial. See my other post for an example .travis.yml file that will support Python 3.5, 3.6 and 3.7.