How to release Python package from GitHub Actions
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
usessyntax, which uses another workflow
For example, installing JDK can be written as:
- uses: actions/setup-java@v1
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:
The key points are:
- Triggering the workflow from Git tag
2. Adding dependency for deploy task
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
PYPI_USERS , and a token starting with
pypi- got on PyPI configuration for
Now, you can publish Python package to PyPI by just tagging on GitHub.