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:
https://github.com/chezou/tabula-py/blob/master/.github/workflows/pythontest.yml
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
with:
java-version: '12'
java-package: jdk
architecture: x64
The downside of GitHub Actions are:
- Unable to write Windows temp directory
- Hard to find the resources for debugging on the internet and unable to ssh to the instance
Releasing Python package from GitHub Actions to PyPI
I created the workflow like the following sequence:
- Push a tag from local, or create a tag on GitHub. Using setuptools-scm enables you to make a new version from Git tag
- GitHub Actions creates GitHub release from the tag
- GitHub Actions publishes wheel to PyPI by using PyPI API Token
You can see the actual workflow on GitHub:
https://github.com/chezou/tabula-py/blob/master/.github/workflows/pythonpublish.yml
The key points are:
- Triggering the workflow from Git tag
on:
push:
tags:
- 'v\*'
2. Adding dependency for deploy task
deploy:
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.
https://pypi.org/help/#apitoken
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.