Travis setup

Tue 12 May 2020
One job in continuous integration pipeline (Photo credit: Wikipedia)

The goal is to setup a CI pipeline based on Travis with external dependencies integrated to a Github repository

Travis basics

To enable Travis integration in Github, one must edit ./.travis.yml file.

I won't go into detail. The setup is documented in the official tutorials and is straight forward. All your Github repository will be listed on your Travis home page. The official documentation propose a default file:

language: python
   python:
     - "2.7"
     - "3.4"
     - "3.5"
     - "3.6"      # current default Python on Travis CI
     - "3.7"
     - "3.8"
     - "3.8-dev"  # 3.8 development branch
     - "nightly"  # nightly build
     # command to install dependencies
   install:
     - pip install -r requirements.txt
     # command to run tests
   script:
     - pytest

This works (if you have setup tests runnable with pytest). Yet, python version does not need to include the older one, neither the unstable one. That means when updating your python version, you must update your Travis file.

When pushing a commit to Github, the Travis pipeline will be triggered, and if all tests pass, a tick will appear in the list of commit on Github.

Small tick appearing on Github commit list

Debug

When the CI tests fail, you can see Travis logs. A commit trigger a Travis build, and a build can be composed of several jobs. For example, there is one job per python version. Each job generate its log. The log contain all command run and its results.

Package installation

Your project may require extra dependencies. Let's see how to install them.

Extra scripts

You can custom the install section of your Travis file. You can put multiple steps on this section: If you use a requirements.txt:

language: python
   python:
     - "3.8"
   install:
     - pip install -r requirements.txt
     - make install
   script:
     - pytest

Python setup.py

For python projects, given that you may have more steps to do for installation, the prefered way to do this is to use a setup.py <https://docs.python.org/3/distutils/setupscript.html>_. The minimal setup.py I recommand use only the setup() function with the following arguments:

  • install_requires for requirements (if needed, read from requirements.txt)
  • packages
  • package_dir to avoid manipulating PYTHONPATH manually

Thus, the setup.py, located at the root of the project, contains

#!/usr/bin/env python

from distutils.core import setup


setup(
  package_dir={"": "src"},
  packages=["egg"],
  install_requires=["faker"],
)

To run this setup.py, you can pip install your project:

...
install:
  - pip install .
...

apt-get

The CI runs on ubuntu virtual machines. Thus, you can add a list of packages that can be installed. This is done through the add-on section:

language: python
   add_ons:
     apt_packages:
       - texlive-full
       - pandoc
   python:
     - "3.8"
   install:
     - pip install .
   script:
     - pytest -v

Category: howto Tagged: travis ci howto