-
Notifications
You must be signed in to change notification settings - Fork 0
150 lines (125 loc) · 4.99 KB
/
Copy pathrelease.yml
File metadata and controls
150 lines (125 loc) · 4.99 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
name: Release
on:
workflow_run:
workflows:
- CI
types:
- completed
permissions:
contents: write
concurrency:
group: release-${{ github.event.workflow_run.head_branch }}
cancel-in-progress: true
jobs:
release:
if: >-
${{
github.event.workflow_run.conclusion == 'success' &&
(github.event.workflow_run.head_branch == 'main' || github.event.workflow_run.head_branch == 'master')
}}
runs-on: ubuntu-latest
steps:
- name: Checkout tested commit
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.workflow_run.head_sha }}
- name: Install uv
uses: astral-sh/setup-uv@v7
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Ensure tested commit is branch tip
id: tip
env:
BRANCH_NAME: ${{ github.event.workflow_run.head_branch }}
HEAD_SHA: ${{ github.event.workflow_run.head_sha }}
run: |
git fetch origin "${BRANCH_NAME}" --tags
REMOTE_SHA="$(git rev-parse "origin/${BRANCH_NAME}")"
if [ "${REMOTE_SHA}" != "${HEAD_SHA}" ]; then
echo "Branch ${BRANCH_NAME} moved from ${HEAD_SHA} to ${REMOTE_SHA}; skipping stale release."
echo "is_tip=false" >> "${GITHUB_OUTPUT}"
exit 0
fi
echo "is_tip=true" >> "${GITHUB_OUTPUT}"
- name: Decide release bump
if: steps.tip.outputs.is_tip == 'true'
id: bump
run: |
python - <<'PY' >> "${GITHUB_OUTPUT}"
import re
import subprocess
import sys
def git(*args: str) -> str:
return subprocess.check_output(["git", *args], text=True).strip()
try:
last_tag = git("describe", "--tags", "--abbrev=0")
except subprocess.CalledProcessError:
last_tag = ""
rev_range = f"{last_tag}..HEAD" if last_tag else "HEAD"
raw = git("log", rev_range, "--format=%s%n%b%x00")
entries = [entry.strip() for entry in raw.split("\x00") if entry.strip()]
if not entries:
print("should_release=false")
print("reason=no_commits")
sys.exit(0)
bump = None
release_notes = []
for entry in entries:
lines = entry.splitlines()
subject = lines[0].strip()
body = "\n".join(lines[1:])
text = f"{subject}\n{body}"
lower = subject.lower()
if "breaking change" in text.lower() or re.match(r"^[^:]+!:", subject):
bump = "major"
release_notes.append(subject)
break
if lower.startswith("feat:"):
bump = "minor" if bump != "major" else bump
release_notes.append(subject)
continue
if lower.startswith("fix:"):
if bump not in {"major", "minor"}:
bump = "patch"
release_notes.append(subject)
continue
print(f"last_tag={last_tag}")
print(f"should_release={'true' if bump else 'false'}")
print(f"bump={bump or ''}")
PY
- name: Stop when no release is needed
if: steps.tip.outputs.is_tip == 'true' && steps.bump.outputs.should_release != 'true'
run: echo "No feat/fix/breaking changes since the last tag."
- name: Bump version
if: steps.tip.outputs.is_tip == 'true' && steps.bump.outputs.should_release == 'true'
run: uv version --bump "${{ steps.bump.outputs.bump }}" --frozen
- name: Read version
if: steps.tip.outputs.is_tip == 'true' && steps.bump.outputs.should_release == 'true'
id: version
run: echo "value=$(uv version --short)" >> "${GITHUB_OUTPUT}"
- name: Commit release change
if: steps.tip.outputs.is_tip == 'true' && steps.bump.outputs.should_release == 'true'
env:
VERSION: ${{ steps.version.outputs.value }}
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git switch -C "${{ github.event.workflow_run.head_branch }}"
git add pyproject.toml
git commit -m "chore: release ${VERSION} [skip ci]"
- name: Create tag
if: steps.tip.outputs.is_tip == 'true' && steps.bump.outputs.should_release == 'true'
env:
VERSION: ${{ steps.version.outputs.value }}
run: git tag -a "v${VERSION}" -m "Release v${VERSION}"
- name: Push release commit and tag
if: steps.tip.outputs.is_tip == 'true' && steps.bump.outputs.should_release == 'true'
env:
BRANCH_NAME: ${{ github.event.workflow_run.head_branch }}
VERSION: ${{ steps.version.outputs.value }}
run: |
git push origin "HEAD:${BRANCH_NAME}"
git push origin "v${VERSION}"