different pypi push strategy #2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and Publish to PyPI | |
on: | |
push: | |
paths: | |
- 'pyproject.toml' | |
workflow_dispatch: | |
inputs: | |
force: | |
description: 'Force publish regardless of version check' | |
required: false | |
default: 'false' | |
jobs: | |
build-and-publish: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v2 | |
- name: Check if new version compared to PyPI | |
id: version_check | |
run: | | |
# Define your package name as published on PyPI. | |
PACKAGE_NAME="tensorgrad" # Change this if your PyPI package name differs. | |
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" | |
# Query PyPI for the latest published version. | |
latest_pypi_version=$(curl -s "https://pypi.org/pypi/${PACKAGE_NAME}/json" | jq -r '.info.version') | |
# If the package isn’t found or no version exists, default to 0.0.0. | |
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's version comparison. | |
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: Set up Python | |
if: steps.version_check.outputs.version_changed == 'true' | |
uses: actions/setup-python@v2 | |
with: | |
python-version: '3.x' | |
- name: Upgrade pip and install build tools | |
if: steps.version_check.outputs.version_changed == 'true' | |
run: | | |
python -m pip install --upgrade pip | |
pip install build twine | |
- name: Build the package | |
if: steps.version_check.outputs.version_changed == 'true' | |
run: python -m build | |
- name: Publish package to PyPI | |
if: steps.version_check.outputs.version_changed == 'true' | |
env: | |
TWINE_USERNAME: __token__ | |
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }} | |
run: python -m twine upload dist/* |