|
| 1 | +name: Publish to PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + |
| 7 | +concurrency: |
| 8 | + group: pypi-publish-${{ github.event.release.tag_name }} |
| 9 | + cancel-in-progress: false |
| 10 | + |
| 11 | +jobs: |
| 12 | + build: |
| 13 | + runs-on: ubuntu-latest |
| 14 | + permissions: |
| 15 | + contents: read |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Check out repository |
| 19 | + uses: actions/checkout@v5 |
| 20 | + |
| 21 | + - name: Set up Python |
| 22 | + uses: actions/setup-python@v5 |
| 23 | + with: |
| 24 | + python-version: "3.12" |
| 25 | + cache: pip |
| 26 | + |
| 27 | + - name: Show tool versions |
| 28 | + run: | |
| 29 | + python --version |
| 30 | + pip --version |
| 31 | +
|
| 32 | + - name: Verify release tag matches pyproject version |
| 33 | + shell: bash |
| 34 | + run: | |
| 35 | + PACKAGE_VERSION=$(python - <<'PY' |
| 36 | + import tomllib |
| 37 | +
|
| 38 | + with open("pyproject.toml", "rb") as f: |
| 39 | + print(tomllib.load(f)["tool"]["poetry"]["version"]) |
| 40 | + PY |
| 41 | + ) |
| 42 | + TAG_VERSION="${GITHUB_REF_NAME#v}" |
| 43 | +
|
| 44 | + echo "pyproject.toml: $PACKAGE_VERSION" |
| 45 | + echo "release tag: $TAG_VERSION" |
| 46 | +
|
| 47 | + test "$PACKAGE_VERSION" = "$TAG_VERSION" |
| 48 | +
|
| 49 | + - name: Check if version already exists on PyPI |
| 50 | + shell: bash |
| 51 | + run: | |
| 52 | + PACKAGE_NAME=$(python - <<'PY' |
| 53 | + import tomllib |
| 54 | +
|
| 55 | + with open("pyproject.toml", "rb") as f: |
| 56 | + print(tomllib.load(f)["tool"]["poetry"]["name"]) |
| 57 | + PY |
| 58 | + ) |
| 59 | + PACKAGE_VERSION=$(python - <<'PY' |
| 60 | + import tomllib |
| 61 | +
|
| 62 | + with open("pyproject.toml", "rb") as f: |
| 63 | + print(tomllib.load(f)["tool"]["poetry"]["version"]) |
| 64 | + PY |
| 65 | + ) |
| 66 | +
|
| 67 | + export PACKAGE_NAME PACKAGE_VERSION |
| 68 | +
|
| 69 | + python - <<'PY' |
| 70 | + import os |
| 71 | + import sys |
| 72 | + from urllib.error import HTTPError |
| 73 | + from urllib.request import urlopen |
| 74 | +
|
| 75 | + package_name = os.environ["PACKAGE_NAME"] |
| 76 | + package_version = os.environ["PACKAGE_VERSION"] |
| 77 | + url = f"https://pypi.org/pypi/{package_name}/{package_version}/json" |
| 78 | +
|
| 79 | + try: |
| 80 | + with urlopen(url) as response: |
| 81 | + if response.status == 200: |
| 82 | + print(f"Version already published: {package_name}=={package_version}") |
| 83 | + sys.exit(1) |
| 84 | + except HTTPError as exc: |
| 85 | + if exc.code == 404: |
| 86 | + print(f"Version not published yet: {package_name}=={package_version}") |
| 87 | + sys.exit(0) |
| 88 | + raise |
| 89 | + PY |
| 90 | +
|
| 91 | + - name: Install build tooling |
| 92 | + run: python -m pip install --upgrade build twine |
| 93 | + |
| 94 | + - name: Build package |
| 95 | + run: python -m build |
| 96 | + |
| 97 | + - name: Check distribution metadata |
| 98 | + run: python -m twine check dist/* |
| 99 | + |
| 100 | + - name: Upload distribution artifacts |
| 101 | + uses: actions/upload-artifact@v4 |
| 102 | + with: |
| 103 | + name: python-package-distributions |
| 104 | + path: dist/ |
| 105 | + |
| 106 | + publish: |
| 107 | + needs: build |
| 108 | + runs-on: ubuntu-latest |
| 109 | + permissions: |
| 110 | + id-token: write |
| 111 | + |
| 112 | + steps: |
| 113 | + - name: Download distribution artifacts |
| 114 | + uses: actions/download-artifact@v4 |
| 115 | + with: |
| 116 | + name: python-package-distributions |
| 117 | + path: dist/ |
| 118 | + |
| 119 | + - name: Publish package to PyPI |
| 120 | + uses: pypa/gh-action-pypi-publish@release/v1 |
0 commit comments