Publish package to PyPI workflow (xarray-contrib/xarray-spatial)
The Publish package to PyPI workflow from xarray-contrib/xarray-spatial, explained and optimized by Latchkey.
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.
What it does
This is the Publish package to PyPI workflow from the xarray-contrib/xarray-spatial 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
name: Publish package to PyPI
on:
push:
tags:
- "*"
jobs:
publish:
runs-on: ubuntu-22.04
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0 # ensures full history/tags are available if you ever need them
- name: Set up Python 3.13
uses: actions/setup-python@v6
with:
python-version: "3.13"
- name: Get release version
shell: bash
run: |
echo "CHANGELOG_VERSION=$(grep -oP '(?<=###\s)(.*)(?=\s\-)' CHANGELOG.md | head -n 1 | sed 's/Version\s/v/')" >> "$GITHUB_ENV"
echo "TAG_VERSION=${GITHUB_REF_NAME}" >> "$GITHUB_ENV"
- name: Check changelog release version
shell: bash
run: |
if [[ "${TAG_VERSION}" != "${CHANGELOG_VERSION}" ]]; then
echo "CHANGELOG_VERSION(${CHANGELOG_VERSION}) is different from TAG_VERSION(${TAG_VERSION})"
exit 1
fi
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
- name: Build package
run: |
python -m build --sdist --wheel
- name: Check package size (100MB max)
shell: bash
run: |
PKG_SIZE="$(find dist -maxdepth 1 -name '*.tar.gz' -print0 | xargs -0 stat --format='%s' | head -n 1)"
echo "PKG_SIZE=${PKG_SIZE}" >> "$GITHUB_ENV"
if [[ -n "${PKG_SIZE}" ]] && (( PKG_SIZE > 100000000 )); then
echo "PKG_SIZE(${PKG_SIZE} bytes) is greater than 100MB"
exit 1
fi
- name: Publish package
if: startsWith(github.ref, 'refs/tags/')
uses: pypa/gh-action-pypi-publish@release/v1
with:
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
skip-existing: true
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 package to PyPI on: push: tags: - "*" concurrency: group: ${{ github.workflow }}-${{ github.ref }} cancel-in-progress: true jobs: publish: timeout-minutes: 30 runs-on: latchkey-small steps: - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0 # ensures full history/tags are available if you ever need them - name: Set up Python 3.13 uses: actions/setup-python@v6 with: cache: 'pip' python-version: "3.13" - name: Get release version shell: bash run: | echo "CHANGELOG_VERSION=$(grep -oP '(?<=###\s)(.*)(?=\s\-)' CHANGELOG.md | head -n 1 | sed 's/Version\s/v/')" >> "$GITHUB_ENV" echo "TAG_VERSION=${GITHUB_REF_NAME}" >> "$GITHUB_ENV" - name: Check changelog release version shell: bash run: | if [[ "${TAG_VERSION}" != "${CHANGELOG_VERSION}" ]]; then echo "CHANGELOG_VERSION(${CHANGELOG_VERSION}) is different from TAG_VERSION(${TAG_VERSION})" exit 1 fi - name: Install dependencies run: | python -m pip install --upgrade pip pip install build - name: Build package run: | python -m build --sdist --wheel - name: Check package size (100MB max) shell: bash run: | PKG_SIZE="$(find dist -maxdepth 1 -name '*.tar.gz' -print0 | xargs -0 stat --format='%s' | head -n 1)" echo "PKG_SIZE=${PKG_SIZE}" >> "$GITHUB_ENV" if [[ -n "${PKG_SIZE}" ]] && (( PKG_SIZE > 100000000 )); then echo "PKG_SIZE(${PKG_SIZE} bytes) is greater than 100MB" exit 1 fi - name: Publish package if: startsWith(github.ref, 'refs/tags/') uses: pypa/gh-action-pypi-publish@release/v1 with: user: __token__ password: ${{ secrets.PYPI_API_TOKEN }} skip-existing: true
What changed
- Run on Latchkey managed runners with one line (
runs-on), which apply the fixes below automatically and self-heal transient failures. This example useslatchkey-small; pick the runner size that fits the job. - Cancel superseded runs when a branch or PR gets a newer push.
- Cache dependency installs on the setup step so they are served from cache.
- Add a job timeout so a hung step cannot burn hours of runner time.
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:
- Dependency installs
This workflow runs 1 job per trigger. On Latchkey the same minutes cost up to 58% less than GitHub-hosted, with zero queue time.