Skip to content

Upload Python Package #23

Upload Python Package

Upload Python Package #23

Workflow file for this run

# This workflow will upload a Python Package to PyPI when a release is created
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python#publishing-to-package-registries
# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.
name: Upload Python Package
on:
workflow_run:
workflows: ["Auto Release"]
types:
- completed
permissions:
contents: read
jobs:
check-workflow:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
outputs:
version: ${{ steps.get-version.outputs.version }}
should_publish: ${{ steps.check-release.outputs.should_publish }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.workflow_run.head_branch }}
- name: Get version from pyproject.toml
id: get-version
run: |
VERSION=$(grep -m 1 "version = " pyproject.toml | sed 's/version = "\(.*\)"/\1/')
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "Current version: $VERSION"
- name: Check if release should be published
id: check-release
run: |
# Check if most recent commit was a version bump
COMMIT_MSG=$(git log -1 --pretty=%B)
if [[ "$COMMIT_MSG" =~ [Bb][Uu][Mm][Pp]\ [Vv][Ee][Rr][Ss][Ii][Oo][Nn] ]]; then
echo "should_publish=true" >> $GITHUB_OUTPUT
echo "Version bump detected, should publish to PyPI"
else
echo "should_publish=false" >> $GITHUB_OUTPUT
echo "Not a version bump commit, skipping PyPI publishing"
fi
- name: Debug workflow info
run: |
echo "Auto-Release workflow completed successfully"
echo "Version to publish: ${{ steps.get-version.outputs.version }}"
echo "Should publish: ${{ steps.check-release.outputs.should_publish }}"
release-build:
runs-on: ubuntu-latest
needs: check-workflow
if: ${{ needs.check-workflow.outputs.should_publish == 'true' }}
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_branch }}
- uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Build release distributions
run: |
python -m pip install --upgrade pip
python -m pip install build
python -m build
- name: Upload distributions
uses: actions/upload-artifact@v4
with:
name: release-dists
path: dist/
pypi-publish:
runs-on: ubuntu-latest
needs:
- check-workflow
- release-build
if: ${{ needs.check-workflow.outputs.should_publish == 'true' }}
environment:
name: pypi
steps:
- name: Retrieve release distributions
uses: actions/download-artifact@v4
with:
name: release-dists
path: dist/
- name: Publish release distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.PYPI_TOKEN }}
packages-dir: dist/
verbose: true
- name: Publish success
run: |
echo "✅ Successfully published version ${{ needs.check-workflow.outputs.version }} to PyPI"