| 1 |
--- |
| 2 |
name: Release (PyPI + GitHub) |
| 3 |
|
| 4 |
on: |
| 5 |
push: |
| 6 |
tags: |
| 7 |
- "v*.*.*" # e.g. v1.2.3 |
| 8 |
|
| 9 |
jobs: |
| 10 |
build: |
| 11 |
runs-on: ubuntu-latest |
| 12 |
outputs: |
| 13 |
tag: ${{ steps.read_ver.outputs.tag }} |
| 14 |
file_ver: ${{ steps.read_ver.outputs.file_ver }} |
| 15 |
steps: |
| 16 |
- uses: actions/checkout@v6 |
| 17 |
|
| 18 |
- uses: actions/setup-python@v6 |
| 19 |
with: |
| 20 |
python-version: "3.x" |
| 21 |
|
| 22 |
- name: Install build tools |
| 23 |
run: pip install build tomli |
| 24 |
|
| 25 |
- name: Read version from pyproject.toml & verify tag |
| 26 |
id: read_ver |
| 27 |
shell: python |
| 28 |
run: | |
| 29 |
import os, sys |
| 30 |
try: |
| 31 |
import tomllib as toml # Python 3.11+ |
| 32 |
except Exception: |
| 33 |
import tomli as toml # fallback |
| 34 |
|
| 35 |
# Read pyproject version |
| 36 |
with open("pyproject.toml","rb") as f: |
| 37 |
ver = toml.load(f)["project"]["version"] |
| 38 |
|
| 39 |
# Extract tag from GITHUB_REF (e.g. "refs/tags/v1.2.3" -> "1.2.3") |
| 40 |
ref = os.environ["GITHUB_REF"] |
| 41 |
# Safety checks & strip prefix |
| 42 |
prefix = "refs/tags/v" |
| 43 |
if not ref.startswith(prefix): |
| 44 |
print(f"Unexpected GITHUB_REF: {ref}", file=sys.stderr) |
| 45 |
sys.exit(1) |
| 46 |
tag = ref[len(prefix):] |
| 47 |
|
| 48 |
if tag != ver: |
| 49 |
print(f"❌ Tag ({tag}) != pyproject.toml ({ver})", file=sys.stderr) |
| 50 |
sys.exit(1) |
| 51 |
|
| 52 |
# Expose outputs |
| 53 |
with open(os.environ["GITHUB_OUTPUT"], "a") as fh: |
| 54 |
fh.write(f"tag={tag}\n") |
| 55 |
fh.write(f"file_ver={ver}\n") |
| 56 |
|
| 57 |
print(f"✅ Version OK: tag={tag} matches pyproject.toml={ver}") |
| 58 |
|
| 59 |
- name: Build dists |
| 60 |
run: python -m build |
| 61 |
|
| 62 |
- name: Upload dists |
| 63 |
uses: actions/upload-artifact@v5 |
| 64 |
with: |
| 65 |
name: dist |
| 66 |
path: dist/* |
| 67 |
|
| 68 |
changelog: |
| 69 |
needs: build |
| 70 |
runs-on: ubuntu-latest |
| 71 |
steps: |
| 72 |
- uses: actions/checkout@v6 |
| 73 |
|
| 74 |
- name: Extract release notes (fail if missing) |
| 75 |
id: notes |
| 76 |
shell: python |
| 77 |
env: |
| 78 |
TAG: ${{ needs.build.outputs.tag }} |
| 79 |
run: | |
| 80 |
import os, sys, re, pathlib |
| 81 |
ver = os.environ["TAG"] # e.g., 1.0.2 |
| 82 |
chlog_path = pathlib.Path("CHANGELOG.md") |
| 83 |
if not chlog_path.exists(): |
| 84 |
print("CHANGELOG.md not found", file=sys.stderr) |
| 85 |
sys.exit(2) |
| 86 |
|
| 87 |
lines = chlog_path.read_text(encoding="utf-8").splitlines() |
| 88 |
|
| 89 |
def is_heading_for_version(s: str) -> bool: |
| 90 |
s = s.strip() |
| 91 |
if not s.startswith("##"): |
| 92 |
return False |
| 93 |
s = s[2:].strip() # drop "##" |
| 94 |
s = s.strip("[]") # allow [1.0.2] |
| 95 |
s = re.sub(r"\s*-\s*.*$", "", s) # drop " - date" |
| 96 |
s = s.lstrip("v") # allow v1.0.2 |
| 97 |
return s == ver |
| 98 |
|
| 99 |
start_idx = None |
| 100 |
for i, line in enumerate(lines): |
| 101 |
if is_heading_for_version(line): |
| 102 |
start_idx = i + 1 # start AFTER heading |
| 103 |
break |
| 104 |
|
| 105 |
if start_idx is None: |
| 106 |
print(f"No changelog section found for {ver}", file=sys.stderr) |
| 107 |
sys.exit(3) |
| 108 |
|
| 109 |
end_idx = len(lines) |
| 110 |
for j in range(start_idx, len(lines)): |
| 111 |
if lines[j].lstrip().startswith("## "): |
| 112 |
end_idx = j |
| 113 |
break |
| 114 |
|
| 115 |
section_lines = lines[start_idx:end_idx] |
| 116 |
while section_lines and not section_lines[0].strip(): |
| 117 |
section_lines.pop(0) |
| 118 |
while section_lines and not section_lines[-1].strip(): |
| 119 |
section_lines.pop() |
| 120 |
|
| 121 |
section = "\n".join(section_lines) |
| 122 |
only_links = re.fullmatch(r"(?:\[[^\]]+\]:\s*\S+\s*(?:\n|$))*", section or "", flags=re.MULTILINE) |
| 123 |
if not section or only_links: |
| 124 |
print(f"Changelog section for {ver} is empty", file=sys.stderr) |
| 125 |
sys.exit(4) |
| 126 |
|
| 127 |
pathlib.Path("RELEASE_NOTES.md").write_text(section, encoding="utf-8") |
| 128 |
|
| 129 |
- name: Upload release notes |
| 130 |
uses: actions/upload-artifact@v5 |
| 131 |
with: |
| 132 |
name: release-notes |
| 133 |
path: RELEASE_NOTES.md |
| 134 |
|
| 135 |
publish: |
| 136 |
needs: [build, changelog] |
| 137 |
runs-on: ubuntu-latest |
| 138 |
environment: pypi |
| 139 |
permissions: |
| 140 |
id-token: write # required for PyPI Trusted Publishing (OIDC) |
| 141 |
contents: read |
| 142 |
steps: |
| 143 |
- uses: actions/download-artifact@v6 |
| 144 |
with: |
| 145 |
name: dist |
| 146 |
path: dist |
| 147 |
|
| 148 |
- name: Publish to PyPI via OIDC |
| 149 |
uses: pypa/gh-action-pypi-publish@release/v1 |
| 150 |
with: |
| 151 |
verbose: true |
| 152 |
|
| 153 |
github_release: |
| 154 |
needs: [publish, changelog] |
| 155 |
runs-on: ubuntu-latest |
| 156 |
permissions: |
| 157 |
contents: write |
| 158 |
steps: |
| 159 |
- uses: actions/download-artifact@v6 |
| 160 |
with: |
| 161 |
name: dist |
| 162 |
path: dist |
| 163 |
- uses: actions/download-artifact@v6 |
| 164 |
with: |
| 165 |
name: release-notes |
| 166 |
path: . |
| 167 |
- name: Create GitHub Release |
| 168 |
uses: softprops/action-gh-release@v2 |
| 169 |
with: |
| 170 |
tag_name: ${{ github.ref_name }} |
| 171 |
name: ${{ github.ref_name }} |
| 172 |
body_path: RELEASE_NOTES.md |
| 173 |
files: | |
| 174 |
dist/* |