How to release Python package from GitHub Actions

Photo by Hitesh Choudhary on Unsplash

Recently, I changed my CI from Travis to GitHub Actions. GitHub Actions is handy and useful for testing, publishing Python packages.

Testing Python code on GitHub Actions

Migration from Travis is super easy, just writing a simple workflow like:

The benefits of GitHub Actions for Python are:

  • We can use build matrix (e.g., OS and Python versions) like Travis
  • Launch time of GitHub is faster than Travis
  • Easy for additional dependency installation by using uses syntax, which uses another workflow

For example, installing JDK can be written as:

- uses: actions/setup-java@v1  
      java-version: '12'  
      java-package: jdk  
      architecture: x64

The downside of GitHub Actions are:

Releasing Python package from GitHub Actions to PyPI

I created the workflow like the following sequence:

  1. Push a tag from local, or create a tag on GitHub. Using setuptools-scm enables you to make a new version from Git tag
  2. GitHub Actions creates GitHub release from the tag
  3. GitHub Actions publishes wheel to PyPI by using PyPI API Token

You can see the actual workflow on GitHub:

The key points are:

  1. Triggering the workflow from Git tag
       - 'v\*'

2. Adding dependency for deploy task

     needs: release

needs syntax supports to write dependency. In this case, I describe release job for creating GitHub release, and then deploy job publishes the package to PyPI.

3. Preparation secrets for PyPI

Recently, PyPI provides API tokens for package publishments so that you can get an API token for the specific project. See details on the official document since it is under beta, and spec might change.

After getting API Token from PyPI, you can set secrets on GitHub by clicking “Settings” -> “Secrets” on the project page. Using my example workflow, you should set __token__ for PYPI_USERS , and a token starting with pypi- got on PyPI configuration for PYPI_PASSWORD .

Now, you can publish Python package to PyPI by just tagging on GitHub.

Aki Ariga
Aki Ariga
Machine Learning Engineer

Interested in Machine Learning, ML Ops, and Data driven business. If you like my blog post, I’m glad if you can buy me a tea 😉

  Gift a cup of Tea