From 4d29a3a4f2cc0cc085bc9f92f8c8046f2f8e1f9a Mon Sep 17 00:00:00 2001 From: Chris Griffith Date: Wed, 4 Nov 2026 08:06:54 -0500 Subject: [PATCH] Publish to PyPI with Trusted Publishing and pin actions to SHAs - All build jobs (generic sdist/wheel, macOS/Windows Cython wheels, manylinux cibuildwheel) run without secrets and upload artifacts - A single publish job, gated on every build succeeding, downloads all distributions and uploads via PyPI Trusted Publishing (OIDC) in the "pypi" environment; its only permission is id-token: write - No PYPI_USERNAME / PYPI_PASSWORD secrets are used anymore - Pin every action in both workflows to a full commit SHA - Default token permissions to none, checkout without persisted credentials, add job timeouts, twine check every build Co-Authored-By: Claude Opus 5.5 --- .github/workflows/pythonpublish.yml | 108 +++++++++++++++++----------- .github/workflows/tests.yml | 16 ++--- 2 files changed, 76 insertions(+), 48 deletions(-) diff --git a/.github/workflows/pythonpublish.yml b/.github/workflows/pythonpublish.yml index ce902de6..f3a52ba3 100644 --- a/.github/workflows/pythonpublish.yml +++ b/.github/workflows/pythonpublish.yml @@ -1,5 +1,8 @@ -# This workflow will upload a Python Package using Twine when a release is created -# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries +# Builds and publishes to PyPI when a GitHub release is created. +# Every build job runs without secrets. A single publish job, whose only permission is +# id-token: write, uploads all distributions via PyPI Trusted Publishing (OIDC), so no PyPI +# password or token is stored in GitHub: https://docs.pypi.org/trusted-publishers/ +# All actions are pinned to full commit SHAs; the comment shows the matching release tag. name: Upload Python Package @@ -7,16 +10,22 @@ on: release: types: [ created ] -jobs: - deploy-generic: +permissions: {} +jobs: + build-generic: runs-on: ubuntu-latest + timeout-minutes: 20 + permissions: + contents: read steps: - - uses: actions/checkout@v7 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false - name: Set up Python - uses: actions/setup-python@v7 + uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 with: python-version: '3.15' @@ -24,27 +33,36 @@ jobs: uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 # Isolated build: Cython is not part of the build requirements, so this is the pure Python sdist and wheel - - name: Build and Publish - env: - TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} - TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} - + - name: Build and check run: | uv build - uvx twine==6.2.0 upload dist/* + uvx twine==6.2.0 check dist/* - deploy-cython: + - name: Upload distribution artifact + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: dist-generic + path: dist/* + if-no-files-found: error + + build-cython: strategy: + fail-fast: false matrix: os: [macos-latest, windows-latest] python-version: ["3.11", "3.12", "3.13", "3.14", "3.15"] runs-on: ${{ matrix.os }} + timeout-minutes: 30 + permissions: + contents: read steps: - - uses: actions/checkout@v7 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false - name: Set up Python ${{ matrix.python-version }} id: setup-python - uses: actions/setup-python@v7 + uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 with: python-version: ${{ matrix.python-version }} - name: Install uv @@ -53,21 +71,28 @@ jobs: run: | uv venv --python "${{ steps.setup-python.outputs.python-path }}" uv pip install "setuptools>=77" wheel "Cython>=3.3.0" - - name: Build and publish - env: - TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} - TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} - # No build isolation so the Cython installed above is used to build the compiled wheel + # No build isolation so the Cython installed above is used to build the compiled wheel + - name: Build and check run: | uv build --wheel --no-build-isolation - uvx twine==6.2.0 upload dist/* + uvx twine==6.2.0 check dist/* + - name: Upload wheel artifact + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: dist-cython-${{ matrix.os }}-${{ matrix.python-version }} + path: dist/*.whl + if-no-files-found: error - # Building runs the test suite (third party test dependencies), so it happens in a job without - # access to the PyPI credentials. A separate job only downloads the built wheels and uploads them. build-cython-manylinux: runs-on: ubuntu-latest + timeout-minutes: 60 + permissions: + contents: read + steps: - - uses: actions/checkout@v7 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false - name: Install uv uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 @@ -76,27 +101,30 @@ jobs: run: uvx cibuildwheel==4.3.5 --output-dir dist - name: Upload wheel artifact - uses: actions/upload-artifact@v7 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: - name: python_box_manylinux + name: dist-cython-manylinux path: dist/*-manylinux*.whl + if-no-files-found: error - deploy-cython-manylinux: - needs: build-cython-manylinux + # Publishes only after every build succeeds, so a release is never left half uploaded + publish: + needs: [ build-generic, build-cython, build-cython-manylinux ] runs-on: ubuntu-latest + timeout-minutes: 15 + environment: + name: pypi + url: https://pypi.org/p/python-box + permissions: + id-token: write # Required for Trusted Publishing; the only permission this job has + steps: - - name: Download wheels - uses: actions/download-artifact@v7 + - name: Download all distributions + uses: actions/download-artifact@37930b1c2abaa49bbe596cd826c3c89aef350131 # v7.0.0 with: - name: python_box_manylinux + pattern: dist-* path: dist + merge-multiple: true - - name: Install uv - uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 - - - name: Publish - env: - TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }} - TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }} - run: | - uvx twine==6.2.0 upload dist/*-manylinux*.whl + - name: Publish to PyPI + uses: pypa/gh-action-pypi-publish@7245906fb61fa33a13554f9d5fb856fe61758086 # v1.14.3 diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b92442d0..5a8b2b31 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -13,7 +13,7 @@ jobs: lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v7 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Install uv uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 with: @@ -30,10 +30,10 @@ jobs: os: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v7 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Set up Python ${{ matrix.python-version }} id: setup-python - uses: actions/setup-python@v7 + uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 with: python-version: ${{ matrix.python-version }} - name: Install uv @@ -64,7 +64,7 @@ jobs: Remove-item box -recurse -force uv run --no-sync pytest -vv - name: Upload wheel artifact - uses: actions/upload-artifact@v7 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: python_box_${{matrix.os}}_${{ matrix.python-version }} path: dist/*.whl @@ -72,7 +72,7 @@ jobs: package-manylinux-checks: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v7 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Install uv uses: astral-sh/setup-uv@68559e99cc807c1eb7e9238fd43a15bba460eec2 # v10.3.3 with: @@ -82,7 +82,7 @@ jobs: run: uvx cibuildwheel==4.3.5 --output-dir dist - name: Upload wheel artifact - uses: actions/upload-artifact@v7 + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 with: name: python_box_manylinux path: dist/*-manylinux*.whl @@ -94,10 +94,10 @@ jobs: os: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.os }} steps: - - uses: actions/checkout@v7 + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - name: Set up Python ${{ matrix.python-version }} id: setup-python - uses: actions/setup-python@v7 + uses: actions/setup-python@6d4fd22b20465ccc0f7354476588e8df7c9ca0e0 # v7.1.0 with: python-version: ${{ matrix.python-version }} - name: Install uv