Repository navigation
118 lines (108 loc) · 3.38 KB
/
Copy pathrelease.yml
File metadata and controls
118 lines (108 loc) · 3.38 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
name: Release
on:
push:
tags:
- "v*"
workflow_dispatch:
inputs:
tag:
description: "Release tag, for example v0.1.0"
required: true
type: string
permissions:
contents: write
id-token: write
jobs:
validate-and-release:
name: Validate, release, and publish
runs-on: ubuntu-latest
environment: pypi
steps:
- name: Check out repository
shell: bash
run: |
git init .
git remote add origin "$GITHUB_SERVER_URL/$GITHUB_REPOSITORY.git"
git fetch --depth=1 origin "$GITHUB_SHA"
git checkout --force FETCH_HEAD
- name: Resolve tag
shell: bash
run: |
if [ "${GITHUB_EVENT_NAME}" = "workflow_dispatch" ]; then
tag="${{ inputs.tag }}"
else
tag="${GITHUB_REF_NAME}"
fi
echo "RELEASE_TAG=$tag" >> "$GITHUB_ENV"
- name: Validate release metadata
shell: bash
run: python scripts/verify_release.py "$RELEASE_TAG"
- name: Run tests
shell: bash
run: |
python -m pip install -e ".[dev]"
python -m pytest
- name: Build distributions
shell: bash
run: |
python -m pip install --upgrade uv
python -m uv build
- name: Create GitHub release
shell: bash
env:
GH_TOKEN: ${{ github.token }}
run: |
python - <<'PY'
import json
import os
from pathlib import Path
import urllib.error
import urllib.request
repo = os.environ["GITHUB_REPOSITORY"]
tag = os.environ["RELEASE_TAG"]
token = os.environ["GH_TOKEN"]
version = tag.removeprefix("v")
lines = Path("CHANGELOG.md").read_text(encoding="utf-8").splitlines()
selected = []
capture = False
for line in lines:
if line.startswith(f"## [{version}]"):
capture = True
continue
if capture and line.startswith("## ["):
break
if capture:
selected.append(line)
body = "\n".join(selected).strip() or f"Release {tag}"
url = f"https://api.github.com/repos/{repo}/releases"
payload = {
"tag_name": tag,
"name": tag,
"body": body,
"draft": False,
"prerelease": tag.startswith("v0."),
}
req = urllib.request.Request(
url,
data=json.dumps(payload).encode("utf-8"),
headers={
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {token}",
"Content-Type": "application/json",
"X-GitHub-Api-Version": "2022-11-28",
},
method="POST",
)
try:
with urllib.request.urlopen(req) as resp:
print(resp.read().decode("utf-8"))
except urllib.error.HTTPError as exc:
text = exc.read().decode("utf-8")
if exc.code == 422 and "already_exists" in text:
print(f"Release already exists for {tag}")
else:
raise
PY
- name: Publish to PyPI
shell: bash
run: python -m uv publish --trusted-publishing always dist/*