Skip to content

Build and Publish to PyPI #3

Build and Publish to PyPI

Build and Publish to PyPI #3

Workflow file for this run

name: Build and Publish to PyPI
on:
# Trigger when the python-tests workflow completes.
workflow_run:
workflows: ["Run Pytest on Push"]
types: [completed]
# Also allow manual triggering.
workflow_dispatch:
inputs:
force:
description: 'Force publish regardless of version check'
required: false
default: 'false'
jobs:
build-and-publish:
# Only run if the triggering workflow (python-tests) succeeded.
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
# Check out the commit that triggered the tests.
ref: ${{ github.event.workflow_run.head_sha }}
- name: Check if new version compared to PyPI or force publish
id: version_check
run: |
PACKAGE_NAME="tensorgrad" # Update this if your PyPI package name is different.
FORCE="${{ github.event.inputs.force }}"
if [ "$FORCE" = "true" ]; then
echo "Force publish enabled."
echo "version_changed=true" >> $GITHUB_OUTPUT
else
# Extract the version from pyproject.toml (expects a line like: version = "1.2.3")
current_version=$(grep '^version' pyproject.toml | head -n 1 | cut -d'"' -f2)
echo "Current version in pyproject.toml: $current_version"
# Get the latest version from PyPI using its JSON API.
latest_pypi_version=$(curl -s "https://pypi.org/pypi/${PACKAGE_NAME}/json" | jq -r '.info.version')
# Default to 0.0.0 if nothing is returned.
if [ "$latest_pypi_version" = "null" ] || [ -z "$latest_pypi_version" ]; then
latest_pypi_version="0.0.0"
fi
echo "Latest version on PyPI: $latest_pypi_version"
# Compare versions using dpkg (ensures proper version ordering).
if dpkg --compare-versions "$current_version" gt "$latest_pypi_version"; then
echo "New version detected, ready to publish."
echo "version_changed=true" >> $GITHUB_OUTPUT
else
echo "No new version detected. Skipping publish."
echo "version_changed=false" >> $GITHUB_OUTPUT
fi
fi
shell: bash
- name: Install uv
if: steps.version_check.outputs.version_changed == 'true'
uses: astral-sh/setup-uv@v5
- name: Set up Python
if: steps.version_check.outputs.version_changed == 'true'
run: uv python install 3.13
- name: Build the package
if: steps.version_check.outputs.version_changed == 'true'
run: uv build
- name: Publish package to PyPI
if: steps.version_check.outputs.version_changed == 'true'
env:
UV_PUBLISH_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
run: uv publish