This is a Github Actions workflow for automatic publications to PyPi. Version data from a python file is extracted and then used by the PyPi setup process which will publish the package to PyPi Test and Prod.
The workflow will only be triggered for the publication of new repo releases.
- Create token secrets for both PyPi Test and PyPi Prod (
Account Settings
>API Tokens
>Add API token
). - In your Github project, goto
Settings
>Secrets
>Actions
- Create two keys
TEST_PYPI_API_TOKEN
andPROD_PYPI_API_TOKEN
and assign the previously created token secrets to these keys
This repo contains three files that you may need to amend and copy to your Github repository:
setup.py
: this is a regular Pythonsetup.py
file; amend the file content with your package information and then save the file in your repo's root directorypublish-to-pypi.yml
: Edit this file, amend the configuration settings (see next chapter) and then save the file in your repo's Github Actions directory (.github/workflows
). You may also need to activate the new workflow - see documentation on Github.
Open the file. You will notice a section which looks like this:
env:
# relative path to your file, e.g.
# ./src/MyLib/MyLib.py
SOURCE_FILE: ./REPLACE/ME
# Regex pattern used for extracting the version data from your fil
# (usually, this does not need to be changed)
REGEX_PATTERN: __version__\s*=\s*"(.*)"
Replace the placeholder for the source file with the relative path to your Python file which contains the version information. Amend the RegEx, if necessary.
This Github action will do the following whenever a new release is published:
- Read the Python file and extract the version information, based on the given Regex. Abort job if no match was found.
- Check if the Github
ref_type
has the valuetag
. This is only the case when you drafted a new release. Otherwise, this value is likely set tomaster
. Abort job in case of a mismatch. - Check if the Github
ref_name
is equal to the extracted version from you Python file. Abort job in case of a mismatch. - Build the PyPi package. Deploy it to PyPi Test and (if successful) PyPi Prod.
The PyPi Prod deployment branch comes with a built-in safeguard which prevents accidental deployments to PyPi Prod for cases where you want to do some testing. If you change the default for the Github Action trigger from
on:
release:
types: [published]
to
on:
push:
then every change to your Github repo will trigger the Github Action but should not lead to a publication to PyPi Prod unless you label the release. When in doubt, you may also want to remove the PROD_PYPI_API_TOKEN
's secret from your Github account to ensure that this workflow cannot write to PyPi Prod.