Skip to content

Publish to PyPI

Publish to PyPI #2

name: Publish to PyPI
on:
release:
types:
- published
workflow_dispatch:
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install build tooling
run: |
python -m pip install --upgrade pip
python -m pip install build twine
- name: Build artifacts
run: python -m build
- name: Configure publish credentials
id: publish-config
env:
EVENT_NAME: ${{ github.event_name }}
RELEASE_NAME: ${{ github.event.release.name }}
DEFAULT_USERNAME: ${{ secrets.twine_username }}
DEFAULT_PASSWORD: ${{ secrets.twine_password }}
TEST_USERNAME: ${{ secrets.twine_test_username }}
TEST_PASSWORD: ${{ secrets.twine_test_password }}
run: |
set -euo pipefail
repository="pypi"
username="$DEFAULT_USERNAME"
password="$DEFAULT_PASSWORD"
if [[ "${EVENT_NAME}" == "release" && -n "${RELEASE_NAME}" ]]; then
if [[ "${RELEASE_NAME}" =~ ^[Tt]est ]]; then
if [[ -z "${TEST_USERNAME}" || -z "${TEST_PASSWORD}" ]]; then
echo "::error::Test PyPI credentials are required for releases starting with 'Test'"
exit 1
fi
repository="testpypi"
username="$TEST_USERNAME"
password="$TEST_PASSWORD"
fi
fi
if [[ -z "${username}" || -z "${password}" ]]; then
echo "::error::PyPI credentials are not configured"
exit 1
fi
{
echo "repository=${repository}"
echo "username=${username}"
echo "password=${password}"
} >> "$GITHUB_OUTPUT"
- name: Publish distribution to PyPI
env:
TWINE_USERNAME: ${{ steps.publish-config.outputs.username }}
TWINE_PASSWORD: ${{ steps.publish-config.outputs.password }}
TWINE_REPOSITORY: ${{ steps.publish-config.outputs.repository }}
run: |
twine upload --repository "${TWINE_REPOSITORY}" dist/*