Skip to main content

This is a very short guide to using Travis CI with your GitHub hosted code repository to automatically deploy a python package to Pypi. If you’re new to continuous integration or would like some more information on what Travis CI does, read Travis CI Core Concepts for Beginners first.

In this guide, we will be creating a Travis CI pipeline to automatically:

  • Start mongodb service. Requires mongo>=4.0

  • Run pytest tests

  • Distributes the package to pypi

All the code can be found on https://github.com/musyoka-morris/travis-pypi-integration

Prerequisites

To proceed, make sure you have a Travis CI account. You can easily create one by visiting https://travis-ci.org/ and then click the Sign in with Github button.

1. Install Travis CLI

First we need to install Travis CLI which is written in Ruby and published as a gem. We will be using this tool to encrypt the Pypi password.

To install the gem:

sudo gem install travis

ERROR: can’t find header files for ruby …

If you run into the error can’t find header files for ruby, then you also need to have the ruby headers installed.

The stack overflow answer saved me. In summary, you need to install ruby development headers as follows:

For Debian, and other distributions using Debian style packaging:

sudo apt-get install ruby-dev

For Ubuntu:

sudo apt-get install ruby-all-dev

2. Create a .travis.yml file

.travis.yml

language: python  # We are using Python language
install: pip install -r requirements.txt  # Install requirements
script: pytest  # Run pytest tests

MongoDB service

Add instructions to start mongodb service.

This section is optional if you are not interested in MongoDB service. I simply left it here to serve as a guide for other services of interest.

...
services:
  - mongodb
before_script:
  - sleep 15  # Sleep for 15 seconds to ensure the service is started before we issue any commands

By default, travis loads mongo v2.4. We instruct travis to load mongo v4.0

...
dist: xenial
addons:
  apt:
    sources:
      - mongodb-4.0-xenial  # As defined on Travis Source safelist

Pypi Deployment

Add instruction for deployment to pypi

...
deploy:
  provider: pypi
  user: musyoka-morris  # Replace this with your pypi username. Password will be provided later
  distributions: sdist bdist_wheel
  skip_existing: true

3. Add encrypted pypi password

Notice that so far we have not specified our pypi password. The easiest way to add the password encrypted with the public key is to use Travis CLI:

travis encrypt Your-Password-Here –add deploy.password

Note: This assumes you are running the command in your project directory. If not, add -r owner/project.

The command automatically adds your encrypted password on the .travis.yml file. your .travis.yml file should look like this:

...
deploy:
    ...
    user: musyoka-morris
    password:
        secure: YourEncryptedPasswordHere

4. Push to GIT

Travis configuration is ready. Simply push the code to the master branch and Travis will take care of the rest.

The complete .travis.yml file can be found on Github https://github.com/musyoka-morris/travis-pypi-integration

Release files for travis-pypi-integration 2.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for travis-pypi-integration 2.1.2
File Size Uploaded
travis_pypi_integration-2.1.2.tar.gz 3.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for travis-pypi-integration 2.1.2
File Interpreter ABI Platform
travis_pypi_integration-2.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 7.9 kB

Release files / travis_pypi_integration-2.1.2.tar.gz

Download URL travis_pypi_integration-2.1.2.tar.gz
Size 3.5 kB
Tags Source
SHA-256 checksum
How to use checksums
8f5b83b5c5f153e5f37e1a81a4d17847d7d1ef1a8007fc6f296c5d7175e8e492
BLAKE2b-256 checksum
How to use checksums
9b8df8ae34dbbe3426ddf93b31ad82f498544b5241680c00d0f0f63d3371f13b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

Release files / travis_pypi_integration-2.1.2-py3-none-any.whl

Download URL travis_pypi_integration-2.1.2-py3-none-any.whl
Size 4.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
07809874835d332f69f7482bb473108ca7f0f17e59c1cbb3f28b2196fee2e023
BLAKE2b-256 checksum
How to use checksums
150bc01c7f8ffb5e0889708cf7d66cd0ec6261e8cbd680d9c6c8e8b64c0c537b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.7

Release history Release notifications | RSS feed

This release

2.1.2 This release

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page