Skip to content
Latchkey

Publish to PyPI workflow (hakancelikdev/unimport)

The Publish to PyPI workflow from hakancelikdev/unimport, explained and optimized by Latchkey.

C

CI health: C - fair

Point runs-on at Latchkey and get 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: hakancelikdev/unimport.github/workflows/pypi.ymlLicense MITView source

What it does

This is the Publish to PyPI workflow from the hakancelikdev/unimport repository, a real project running GitHub Actions. It is shown here with attribution under its MIT license.

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

The workflow

workflow (.yml)
name: Publish to PyPI

on:
  push:
    tags:
      - "**"

env:
  PYTHON_VERSION: "3.13"

jobs:
  test:
    name: Test package build
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Python ${{ env.PYTHON_VERSION }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}
          architecture: x64
          cache: "pip"

      - name: Cache pip dependencies
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key:
            ${{ runner.os }}-pip-${{ env.PYTHON_VERSION }}-${{
            hashFiles('**/pyproject.toml') }}
          restore-keys: |
            ${{ runner.os }}-pip-${{ env.PYTHON_VERSION }}-

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

      - name: Build package
        run: python -m build
        timeout-minutes: 5

      - name: Publish to TestPyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.TEST_PYPI_API_TOKEN }}
          repository_url: https://test.pypi.org/legacy/
          verbose: true
          skip_existing: true

  publish:
    name: Publish to PyPI
    runs-on: ubuntu-latest
    needs: test
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')

    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Python ${{ env.PYTHON_VERSION }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}
          architecture: x64
          cache: "pip"

      - name: Cache pip dependencies
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key:
            ${{ runner.os }}-pip-${{ env.PYTHON_VERSION }}-${{
            hashFiles('**/pyproject.toml') }}
          restore-keys: |
            ${{ runner.os }}-pip-${{ env.PYTHON_VERSION }}-

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

      - name: Build package
        run: python -m build
        timeout-minutes: 5

      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.PYPI_API_TOKEN }}
          verbose: true
          skip_existing: true

The same workflow, on Latchkey

Removes redundant runs and caps runaway jobs. Added and changed lines are highlighted.

name: Publish to PyPI
 
on:
  push:
    tags:
      - "**"
 
env:
  PYTHON_VERSION: "3.13"
 
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true
 
jobs:
  test:
    timeout-minutes: 30
    name: Test package build
    runs-on: latchkey-small
 
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
 
      - name: Set up Python ${{ env.PYTHON_VERSION }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}
          architecture: x64
          cache: "pip"
 
      - name: Cache pip dependencies
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key:
            ${{ runner.os }}-pip-${{ env.PYTHON_VERSION }}-${{
            hashFiles('**/pyproject.toml') }}
          restore-keys: |
            ${{ runner.os }}-pip-${{ env.PYTHON_VERSION }}-
 
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install --upgrade build
 
      - name: Build package
        run: python -m build
        timeout-minutes: 5
 
      - name: Publish to TestPyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.TEST_PYPI_API_TOKEN }}
          repository_url: https://test.pypi.org/legacy/
          verbose: true
          skip_existing: true
 
  publish:
    timeout-minutes: 30
    name: Publish to PyPI
    runs-on: latchkey-small
    needs: test
    if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')
 
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
 
      - name: Set up Python ${{ env.PYTHON_VERSION }}
        uses: actions/setup-python@v5
        with:
          python-version: ${{ env.PYTHON_VERSION }}
          architecture: x64
          cache: "pip"
 
      - name: Cache pip dependencies
        uses: actions/cache@v4
        with:
          path: ~/.cache/pip
          key:
            ${{ runner.os }}-pip-${{ env.PYTHON_VERSION }}-${{
            hashFiles('**/pyproject.toml') }}
          restore-keys: |
            ${{ runner.os }}-pip-${{ env.PYTHON_VERSION }}-
 
      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          python -m pip install --upgrade build
 
      - name: Build package
        run: python -m build
        timeout-minutes: 5
 
      - name: Publish to PyPI
        uses: pypa/gh-action-pypi-publish@release/v1
        with:
          password: ${{ secrets.PYPI_API_TOKEN }}
          verbose: true
          skip_existing: true
 

What changed

1 third-party action is referenced by a movable tag. Pin it 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 per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.

Actions used in this workflow