|
| 1 | +name: Build and publish to PyPI |
| 2 | + |
| 3 | +on: |
| 4 | + release: |
| 5 | + types: [published] |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +jobs: |
| 9 | + build: |
| 10 | + name: Build distributions |
| 11 | + runs-on: ubuntu-latest |
| 12 | + outputs: |
| 13 | + version: ${{ steps.version.outputs.version }} |
| 14 | + steps: |
| 15 | + - uses: actions/checkout@v4 |
| 16 | + |
| 17 | + - name: Set up Python |
| 18 | + uses: actions/setup-python@v5 |
| 19 | + with: |
| 20 | + python-version: "3.11" |
| 21 | + |
| 22 | + - name: Install build tools |
| 23 | + run: python -m pip install --upgrade pip build twine |
| 24 | + |
| 25 | + - name: Read package version |
| 26 | + id: version |
| 27 | + run: | |
| 28 | + python - <<'PY' |
| 29 | + import os |
| 30 | + import pathlib |
| 31 | + import tomllib |
| 32 | + version = tomllib.loads(pathlib.Path("pyproject.toml").read_bytes())["project"][ |
| 33 | + "version" |
| 34 | + ] |
| 35 | + print(version) |
| 36 | + with open(os.environ["GITHUB_OUTPUT"], "a", encoding="utf-8") as f: |
| 37 | + f.write(f"version={version}\n") |
| 38 | + PY |
| 39 | +
|
| 40 | + - name: Validate release tag matches version |
| 41 | + if: github.event_name == 'release' |
| 42 | + env: |
| 43 | + TAG_NAME: ${{ github.event.release.tag_name }} |
| 44 | + run: | |
| 45 | + python - <<'PY' |
| 46 | + import os |
| 47 | + tag = os.environ["TAG_NAME"] |
| 48 | + if tag.startswith("v"): |
| 49 | + tag = tag[1:] |
| 50 | + # `steps.version.outputs.version` is available via GHA expressions only, so re-read here. |
| 51 | + import pathlib, tomllib |
| 52 | + version = tomllib.loads(pathlib.Path("pyproject.toml").read_bytes())["project"]["version"] |
| 53 | + if tag != version: |
| 54 | + raise SystemExit(f"Release tag ({tag}) does not match pyproject.toml version ({version})") |
| 55 | + print("Tag matches version:", tag) |
| 56 | + PY |
| 57 | +
|
| 58 | + - name: Build sdist and wheel |
| 59 | + run: python -m build --sdist --wheel --outdir dist/ |
| 60 | + |
| 61 | + - name: Twine check |
| 62 | + run: python -m twine check --strict dist/* |
| 63 | + |
| 64 | + - uses: actions/upload-artifact@v4 |
| 65 | + with: |
| 66 | + name: dist |
| 67 | + path: dist/ |
| 68 | + |
| 69 | + publish: |
| 70 | + name: Publish to PyPI |
| 71 | + needs: build |
| 72 | + runs-on: ubuntu-latest |
| 73 | + if: github.event_name == 'release' |
| 74 | + permissions: |
| 75 | + id-token: write |
| 76 | + contents: read |
| 77 | + steps: |
| 78 | + - uses: actions/download-artifact@v4 |
| 79 | + with: |
| 80 | + name: dist |
| 81 | + path: dist/ |
| 82 | + |
| 83 | + - name: Publish |
| 84 | + uses: pypa/gh-action-pypi-publish@release/v1 |
| 85 | + with: |
| 86 | + packages-dir: dist/ |
0 commit comments