Building a Python project using Travis CI

Travis CI is a service to provide continuous integration of projects hosted on GitHub. It is entirely free for the projects hosted on public repository, and is also available via a subscription for private repositories and GitHub Enterprise projects.

The steps required to enable a project to be build, tested, deployed by Travis CI is:

  1. Create an account in Travis CI using your GitHub credentials
  2. Navigate to Accounts, then press Sync account to refresh your list of projects (n.b. this step will need to be repeated for each new project)
  3. On the list of repositories that has been created, enable the project you want to be built on each commit, and click Settings to add any required environment variables (such as path, urls, or secure credentials)
  4. Within the local copy of the repository, commit a file named  .travis.yml containing the instructions to be followed by Travis to build your project sources

.travis.yml

language: python

python:
  - "2.7"

env:
  global:
    - PRODUCT=flask-app
    - FLASK_DIR=/opt/flask-app

before_install:
  - export VERSION=1.$(date "+%Y%m%d").$TRAVIS_BUILD_NUMBER
  - echo "Build version=$VERSION"

install:
  - pip --version

before_script:
  - export PATH=$PATH:$HOME/.local/bin

script:
  - echo "$VERSION" > VERSION
  - git rev-parse HEAD > REVISION

after_success:
  - echo "Build successful. TRAVIS_TEST_RESULT=$TRAVIS_TEST_RESULT"

after_failure:
  - echo "Build failed. TRAVIS_TEST_RESULT=$TRAVIS_TEST_RESULT"

deploy:
  # Upload the build to the repository
  - provider: script 
    script: echo "Uploading files..."
    on:
      condition: '-n "$CREDENTIALS"' 

after_script:
  - echo "Run integrations or functional tests"

References

https://docs.travis-ci.com/user/languages/python/
https://docs.python.org/2/library/unittest.html
https://docs.travis-ci.com/user/languages/python/