Skip to content
Latchkey

Publish to (Test)PyPI workflow (tenpy/tenpy)

The Publish to (Test)PyPI workflow from tenpy/tenpy, explained and optimized by Latchkey.

F

CI health: F - at risk

Point runs-on at Latchkey and get caching, run de-duplication, job timeouts, SHA-pinned actions, self-healing for flaky steps, and up to 58% lower cost, applied automatically.

Grade your own workflow free or run it on Latchkey →
Source: tenpy/tenpy.github/workflows/publish.ymlLicense Apache-2.0View source

What it does

This is the Publish to (Test)PyPI workflow from the tenpy/tenpy repository, a real project running GitHub Actions. It is shown here with attribution under its Apache-2.0 license.

Below, Latchkey shows a faster, safer version produced by its optimization engine.

The workflow

workflow (.yml)
name: Publish to (Test)PyPI
# Build wheels and publish them to either TestPyPI (on pushing a tag)
#  or live PyPI (on creating a release)

on:
  # when a release is created (-> publish to live PyPI)
  release:
    types: [created]

  # when a tagged commit is pushed to *any* branch (-> publish to testPyPI)
  push:
    tags:
      - '**'  # on any tag

jobs:
  build-native-wheels:
    name: Build wheels on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest, macos-14]

    steps:
    - name: Checkout
      uses: actions/checkout@v5

    - name: Set up Python
      uses: actions/setup-python@v6
      with:
        python-version: '3.x'

    - name: Build wheels
      # configuration in pyproject.toml
      uses: pypa/cibuildwheel@v3.2.1
      with:
        output-dir: dist

    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: wheels-${{ matrix.os }}
        path: dist/*.whl

  build-sdist-and-upload:
    runs-on: ubuntu-latest
    needs: 'build-native-wheels'
    permissions:
      # allow trusted publishing / OIDC authentication for PyPI
      # corresponding authentication settings on {test.}pypi.org
      id-token: write
    steps:
    - name: Checkout
      uses: actions/checkout@v4

    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.x'

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        python -m pip install --upgrade build twine

    - name: Download wheels from build artifacts
      uses: actions/download-artifact@v4
      with:
        pattern: wheels-*
        merge-multiple: true
        path: dist-wheels/

    - name: Build package
      run: |
        python -m build --sdist
        python -m twine check --strict dist/*
        python -m twine check --strict dist-wheels/*

    # Publish to TestPyPI, only  if it is *not* a release
    #  For a release, pushing the unreleased tag should have already caused this workflow to
    #  publish to TestPyPI. Thus, we can not publish the same version number again.
    - name: Publish wheels to TestPyPI
      if: github.event_name != 'release'
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        repository-url: https://test.pypi.org/legacy/
        packages-dir: dist-wheels/
    - name: Publish sdist to TestPyPI
      if: github.event_name != 'release'
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        repository-url: https://test.pypi.org/legacy/

    # Publish to live PyPI *only on release*
    - name: Publish wheels to PyPI
      if: github.event_name == 'release'
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        packages-dir: dist-wheels/
    - name: Publish sdist to PyPI
      if: github.event_name == 'release'
      uses: pypa/gh-action-pypi-publish@release/v1

The same workflow, on Latchkey

Estimated ~20% faster on cache hits, plus fewer wasted runs and a safer supply chain. Added and changed lines are highlighted.

name: Publish to (Test)PyPI
# Build wheels and publish them to either TestPyPI (on pushing a tag)
#  or live PyPI (on creating a release)
 
on:
  # when a release is created (-> publish to live PyPI)
  release:
    types: [created]
 
  # when a tagged commit is pushed to *any* branch (-> publish to testPyPI)
  push:
    tags:
      - '**'  # on any tag
 
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
 
jobs:
  build-native-wheels:
    timeout-minutes: 30
    name: Build wheels on ${{ matrix.os }}
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-latest, windows-latest, macos-14]
 
    steps:
    - name: Checkout
      uses: actions/checkout@v5
 
    - name: Set up Python
      uses: actions/setup-python@v6
      with:
        cache: 'pip'
        python-version: '3.x'
 
    - name: Build wheels
      # configuration in pyproject.toml
      uses: pypa/cibuildwheel@v3.2.1
      with:
        output-dir: dist
 
    - name: Upload artifacts
      uses: actions/upload-artifact@v4
      with:
        name: wheels-${{ matrix.os }}
        path: dist/*.whl
 
  build-sdist-and-upload:
    timeout-minutes: 30
    runs-on: latchkey-small
    needs: 'build-native-wheels'
    permissions:
      # allow trusted publishing / OIDC authentication for PyPI
      # corresponding authentication settings on {test.}pypi.org
      id-token: write
    steps:
    - name: Checkout
      uses: actions/checkout@v4
 
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        cache: 'pip'
        python-version: '3.x'
 
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        python -m pip install --upgrade build twine
 
    - name: Download wheels from build artifacts
      uses: actions/download-artifact@v4
      with:
        pattern: wheels-*
        merge-multiple: true
        path: dist-wheels/
 
    - name: Build package
      run: |
        python -m build --sdist
        python -m twine check --strict dist/*
        python -m twine check --strict dist-wheels/*
 
    # Publish to TestPyPI, only  if it is *not* a release
    #  For a release, pushing the unreleased tag should have already caused this workflow to
    #  publish to TestPyPI. Thus, we can not publish the same version number again.
    - name: Publish wheels to TestPyPI
      if: github.event_name != 'release'
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        repository-url: https://test.pypi.org/legacy/
        packages-dir: dist-wheels/
    - name: Publish sdist to TestPyPI
      if: github.event_name != 'release'
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        repository-url: https://test.pypi.org/legacy/
 
    # Publish to live PyPI *only on release*
    - name: Publish wheels to PyPI
      if: github.event_name == 'release'
      uses: pypa/gh-action-pypi-publish@release/v1
      with:
        packages-dir: dist-wheels/
    - name: Publish sdist to PyPI
      if: github.event_name == 'release'
      uses: pypa/gh-action-pypi-publish@release/v1
 

What changed

2 third-party actions are referenced by a movable tag. Pin them to the commit SHA (Latchkey resolves and applies this automatically) so a repointed tag cannot change what runs.

What Latchkey heals here

This workflow has steps that commonly fail on transient issues (network, registries, flaky browsers). On Latchkey managed runners they are detected, retried, and self-healed instead of failing your build:

This workflow runs 2 jobs (4 with the matrix expanded) per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.

Actions used in this workflow