Skip to content

Build & Publish to PyPI #3

Build & Publish to PyPI

Build & Publish to PyPI #3

Workflow file for this run

name: Build & Publish to PyPI
on:
# Dry-run triggers
pull_request:
branches:
- main
workflow_dispatch:
# Publish trigger: only when the PR merge commits are pushed to main
push:
branches:
- main
permissions:
contents: read # for checkout
id-token: write # for OIDC
packages: write # for GitHub Packages
jobs:
dry-run:
name: Dry-Run Build & Check
runs-on: ubuntu-latest
# Allow dry-run on all events except when pushing to main
if: github.event_name != 'push' || github.ref != 'refs/heads/main'
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install build tools
run: |
python -m pip install --upgrade pip setuptools wheel build twine
- name: Build the package
run: python -m build --sdist --wheel
- name: Validate the package with Twine
run: python -m twine check dist/*
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: built-package
path: dist/*
publish:
name: Publish to PyPI
runs-on: ubuntu-latest
needs: dry-run
# Only fire on the push-to-main that completes the PR
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Install publish tools
run: |
python -m pip install --upgrade pip setuptools wheel build twine
- name: Build the package
run: python -m build --sdist --wheel
- name: Publish to PyPI
env:
TWINE_USERNAME: __token__
run: |
# avoid failures if you accidentally re-publish the same version
python -m twine upload --skip-existing dist/*