Travis

PyPI Deployment

This page documents deployments using the dpl v2. Please see our blog post for details. You can check previous dpl v1 documentation here.

Travis CI can automatically release your Python package to PyPI after a successful build.

For a minimal configuration, add the following to your .travis.yml:

deploy:
  provider: pypi
  username: <username>
  password: <encrypted password>
  edge: true # opt in to dpl v2

Status #

Support for deployments to PyPI is stable.

Known options #

Use the following options to further configure the deployment.

username PyPI Username — required, type: string, alias: user
password PyPI Password — required, secret, type: string
server Release to a different index — type: string, default: https://upload.pypi.org/legacy/
distributions Space-separated list of distributions to be uploaded to PyPI — type: string, default: sdist
docs_dir Path to the directory to upload documentation from — type: string, default: build/docs
skip_existing Do not overwrite an existing file with the same name on the server. — type: boolean
upload_docs Upload documentation — type: boolean, default: false, note: most PyPI servers, including upload.pypi.org, do not support uploading documentation
twine_check Whether to run twine check — type: boolean, default: true
remove_build_dir Remove the build dir after the upload — type: boolean, default: true
setuptools_version type: string, format: /\A\d+(?:\.\d+)*\z/
twine_version type: string, format: /\A\d+(?:\.\d+)*\z/
wheel_version type: string, format: /\A\d+(?:\.\d+)*\z/

Shared options #

cleanup Clean up build artifacts from the Git working directory before the deployment — type: boolean
run Commands to execute after the deployment finished successfully — type: string or array of strings

Environment variables #

All options can be given as environment variables if prefixed with PYPI_.

For example, password can be given as PYPI_PASSWORD=<password>.

Securing secrets #

Secret option values should be given as either encrypted strings in your build configuration (.travis.yml file) or environment variables in your repository settings.

Environment variables can be set on the settings page of your repository, or using travis env set:

travis env set PYPI_PASSWORD <password>

In order to encrypt option values when adding them to your .travis.yml file use travis encrypt:

travis encrypt <password>

Or use --add to directly add it to your .travis.yml file. Note that this command has to be run in your repository’s root directory:

travis encrypt --add deploy.password <password>

Releasing to a self hosted PyPI #

To release to a different PyPI index:

deploy:
  provider: pypi
  # ⋮
  server: https://mypackageindex.com/index

Uploading different distributions #

By default, only a source distribution (‘sdist’) will be uploaded to PyPI. If you would like to upload different distributions, specify them using the distributions option, like this:

deploy:
  provider: pypi
  # ⋮
  distributions: "sdist bdist_wheel" # Your distributions here

If you specify bdist_wheel in the distributions, the wheel package will automatically be installed.

Upload artifacts only once #

By default, Travis CI runs the deploy step for each python and environment that you specify. Many of these will generate competing build artifacts that will fail to upload to pypi with a message something like this:

HTTPError: 400 Client Error: File already exists.

To avoid this, use the skip_existing flag:

deploy:
  provider: pypi
  # ⋮
  skip_existing: true

Deploying tags #

Most likely, you would only want to deploy when a new version of your package is cut.

To do this, you can include a tags condition like so:

deploy:
  provider: pypi
  # ⋮
  on:
    tags: true

If you tag a commit locally, remember to run git push --tags to ensure that your tags are uploaded to GitHub.

Pull Requests #

Note that pull request builds skip the deployment step altogether.

See also #