included feature_request.md #9
This file contains hidden or 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: Publish Python 🐍 distribution to PyPI | |
| on: | |
| push: | |
| branches: | |
| - main | |
| - master | |
| release: | |
| types: [published] | |
| jobs: | |
| check-version: | |
| runs-on: ubuntu-latest | |
| outputs: | |
| is_new_version: ${{ steps.check.outputs.is_new_version }} | |
| version: ${{ steps.check.outputs.version }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.11" | |
| - name: Check if version is new | |
| id: check | |
| shell: python | |
| run: | | |
| import tomllib | |
| import sys | |
| import json | |
| import os | |
| from urllib.request import urlopen | |
| from urllib.error import HTTPError | |
| try: | |
| with open("pyproject.toml", "rb") as f: | |
| project = tomllib.load(f) | |
| local_version = project["project"]["version"] | |
| package_name = project["project"]["name"] | |
| print(f"Checking version {local_version} for package {package_name}...") | |
| # Output version for later jobs | |
| with open(os.environ['GITHUB_OUTPUT'], 'a') as fh: | |
| print(f"version={local_version}", file=fh) | |
| try: | |
| with urlopen(f"https://pypi.org/pypi/{package_name}/json") as response: | |
| data = json.load(response) | |
| pypi_versions = data["releases"].keys() | |
| if local_version in pypi_versions: | |
| print(f"Version {local_version} already exists on PyPI. Skipping build & publish.") | |
| with open(os.environ['GITHUB_OUTPUT'], 'a') as fh: | |
| print("is_new_version=false", file=fh) | |
| else: | |
| print(f"Version {local_version} is new! Proceeding.") | |
| with open(os.environ['GITHUB_OUTPUT'], 'a') as fh: | |
| print("is_new_version=true", file=fh) | |
| except HTTPError as e: | |
| if e.code == 404: | |
| print("Package not found on PyPI (first deployment). Proceeding.") | |
| with open(os.environ['GITHUB_OUTPUT'], 'a') as fh: | |
| print("is_new_version=true", file=fh) | |
| else: | |
| print(f"Error checking PyPI: {e}") | |
| sys.exit(1) | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| sys.exit(1) | |
| build: | |
| needs: check-version | |
| if: needs.check-version.outputs.is_new_version == 'true' | |
| name: Build distribution 📦 | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Set up Python | |
| uses: actions/setup-python@v5 | |
| with: | |
| python-version: "3.11" | |
| - name: Install build dependencies | |
| run: | | |
| python -m pip install --upgrade pip | |
| pip install build | |
| - name: Build a binary wheel and a source tarball | |
| run: python -m build | |
| - name: Store the distribution packages | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: python-package-distributions | |
| path: dist/ | |
| publish-to-pypi: | |
| name: Publish Python 🐍 distribution to PyPI | |
| needs: build | |
| runs-on: ubuntu-latest | |
| permissions: | |
| id-token: write | |
| steps: | |
| - name: Download all the dists | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: python-package-distributions | |
| path: dist/ | |
| - name: Publish distribution 📦 to PyPI | |
| uses: pypa/gh-action-pypi-publish@release/v1 | |
| with: | |
| password: ${{ secrets.PYPI_API_TOKEN }} | |
| skip-existing: true | |
| verbose: true | |
| create-release: | |
| name: Create GitHub Release 🏷️ | |
| needs: [publish-to-pypi, check-version] | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Create Release | |
| uses: softprops/action-gh-release@v2 | |
| with: | |
| tag_name: v${{ needs.check-version.outputs.version }} | |
| name: v${{ needs.check-version.outputs.version }} | |
| generate_release_notes: true | |
| draft: false | |
| prerelease: false |