← All posts

Python. Using the Travis CI Continuous Integration Service

Travis CI logo

I’ve known about the great Travis-CI service for about six months, but only today did I find the time to try it out on one of my projects published on GitHub.

Travis CI is a Continuous Integration service for many programming languages (C, C++, Clojure, Erlang, Go, Groovy, Haskell, Java, JavaScript/Node.js, Perl, PHP, Python, Ruby, Scala), and naturally, its Python support caught my interest.

How to use Travis is explained in the documentation, but that’s exactly what I’ll describe below:

  1. To use the service, you need a GitHub account;
  2. Go to http://travis-ci.org/ and sign in through GitHub;
  3. After signing in, go to your Profile, where you’ll see all your repositories. Select the ones you need and switch them from OFF to ON, thereby enabling the “Travis service hook” for your repository on GitHub.

Something like this example:

Video / external material

Now let’s move on to configuration.

Configuration

You need to add a file named “.travis.yml” to the root of your repository, with contents like these, for example:


# Тут указывается используемый в проекте язык программирования, можно указать несколько
language: python

# Далее, укажите версии python для которых нужно производить сборки
python:
  - "2.6"
  - "2.7"

# Параметры окружения
env:
  - DJANGO_VERSION=1.4.1

# Команда для установки зависимостей в окружение (при этом доступны переменные из секции "env")
install:
  - pip install -q Django==$DJANGO_VERSION
  - pip install -r requirements.txt --use-mirrors

# Команда для запуска тестов, в моем случае они запускаются именно так
script: python multi_sessions/tests.py

Travis will run your project under virtualenv, with the pytest, nose, and mock packages already installed, so there’s no point in listing them in the “install” section.

Travis will run builds for each specified Python version; that is, if I specify 2 Python versions, Travis will prepare 2 builds. And if you specify several “DJANGO_VERSION” values, Travis will run a build for each “DJANGO_VERSION” with each Python version. In short, you get a matrix.

To check the configuration file, you can use https://github.com/travis-ci/travis-lint

To apply the configuration file, git-push your repository to GitHub.

Usage

Once you’ve specified which repository to monitor (through the “Travis service hook”) and pushed the configuration, the Travis workers will fetch the changes from the repository, set up the environment, and run the “script” section (in our case, our tests).

A report will be generated from the build results and emailed to you. You can also view it on the project’s Travis page—for example, for my project: https://travis-ci.org/#!/adw0rd/django-multi-sessions

There are 3 possible statuses:

  • Red — errors occurred during the build;
  • Yellow — an intermediate state, possibly warnings, or the project is simply still building;
  • Green — the build succeeded!

To display the build status, you can use an image, which you should add, for example, to README.rst:


.. image:: https://secure.travis-ci.org/adw0rd/django-multi-sessions.png
    :target: http://travis-ci.org/adw0rd/django-multi-sessions

The result will look something like this:
The django-multi-sessions page showing a passing Travis CI build

Well, that’s all. Try Travis-CI—you’re sure to like it!

What else should I read?

Comments are closed. Read the original discussion in Russian →