-
Notifications
You must be signed in to change notification settings - Fork 3
70 lines (59 loc) · 1.92 KB
/
pythonpublish.yml
File metadata and controls
70 lines (59 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
name: Upload Python Package to Pypi
on:
push:
tags:
- '[0-9]+.[0-9]+.[0-9]+'
jobs:
deploy:
runs-on: ubuntu-latest
permissions:
contents: write # needed to create the GitHub release for the tag
id-token: write # needed for PyPI trusted publishing (OIDC)
steps:
- name: Validate tag format (X.Y.Z)
shell: bash
run: |
TAG="${GITHUB_REF_NAME}"
echo "Pushed tag: $TAG"
if ! [[ "$TAG" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "Tag must be X.Y.Z (e.g. 1.2.3). Got: $TAG"
exit 1
fi
- uses: actions/checkout@v5
- name: Set up Python
uses: actions/setup-python@v6
with:
python-version: '3.12'
- name: Validate pyproject.toml version matches tag
run: |
python - <<'PY'
import os, pathlib, tomllib
tag = os.environ["GITHUB_REF_NAME"]
data = tomllib.loads(pathlib.Path("pyproject.toml").read_text(encoding="utf-8"))
py_ver = data["project"]["version"]
print(f"pyproject.toml version: {py_ver}")
if py_ver != tag:
raise SystemExit(f"Version mismatch: tag={tag} but pyproject.toml={py_ver}")
PY
- name: Build distributions
run: |
python -m pip install --upgrade pip
pip install build
python -m build
- name: Publish to PyPI (OIDC)
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: dist
- name: Create GitHub release for tag
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
TAG="${GITHUB_REF_NAME}"
if gh release view "$TAG" >/dev/null 2>&1; then
echo "Release $TAG already exists, skipping."
else
gh release create "$TAG" \
--title "$TAG" \
--generate-notes \
dist/*
fi