|
| 1 | +name: Publish to PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + |
| 7 | +jobs: |
| 8 | + build: |
| 9 | + name: Build distribution |
| 10 | + runs-on: ubuntu-latest |
| 11 | + permissions: |
| 12 | + contents: read |
| 13 | + |
| 14 | + steps: |
| 15 | + - name: Check out repository |
| 16 | + uses: actions/checkout@v4 |
| 17 | + with: |
| 18 | + ref: ${{ github.event.release.tag_name }} |
| 19 | + |
| 20 | + - name: Set up Python |
| 21 | + uses: actions/setup-python@v5 |
| 22 | + with: |
| 23 | + python-version: "3.14" |
| 24 | + |
| 25 | + - name: Install build dependencies |
| 26 | + run: | |
| 27 | + python -m pip install --upgrade pip |
| 28 | + python -m pip install build twine |
| 29 | +
|
| 30 | + - name: Validate release tag matches package version |
| 31 | + run: | |
| 32 | + python - <<'PY' |
| 33 | + import pathlib |
| 34 | + import re |
| 35 | + import sys |
| 36 | +
|
| 37 | + tag = "${{ github.event.release.tag_name }}" |
| 38 | + version_file = pathlib.Path("src/abracatabra/__about__.py") |
| 39 | + match = re.search(r'__version__\s*=\s*["\']([^"\']+)["\']', version_file.read_text()) |
| 40 | +
|
| 41 | + if match is None: |
| 42 | + sys.exit("Could not find __version__ in src/abracatabra/__about__.py") |
| 43 | +
|
| 44 | + version = match.group(1) |
| 45 | + expected_tag = f"v{version}" |
| 46 | +
|
| 47 | + if tag != expected_tag: |
| 48 | + sys.exit(f"Release tag {tag!r} does not match package version {version!r}; expected {expected_tag!r}") |
| 49 | + PY |
| 50 | +
|
| 51 | + - name: Build package |
| 52 | + run: | |
| 53 | + python -m build |
| 54 | +
|
| 55 | + - name: Check package metadata |
| 56 | + run: | |
| 57 | + twine check dist/* |
| 58 | +
|
| 59 | + - name: Upload distributions |
| 60 | + uses: actions/upload-artifact@v4 |
| 61 | + with: |
| 62 | + name: dist |
| 63 | + path: dist/* |
| 64 | + if-no-files-found: error |
| 65 | + |
| 66 | + publish: |
| 67 | + name: Publish distribution |
| 68 | + runs-on: ubuntu-latest |
| 69 | + needs: build |
| 70 | + environment: pypi |
| 71 | + permissions: |
| 72 | + id-token: write |
| 73 | + |
| 74 | + steps: |
| 75 | + - name: Download distributions |
| 76 | + uses: actions/download-artifact@v4 |
| 77 | + with: |
| 78 | + name: dist |
| 79 | + path: dist |
| 80 | + |
| 81 | + - name: Publish package to PyPI |
| 82 | + uses: pypa/gh-action-pypi-publish@release/v1 |
0 commit comments