Skip to content

Commit 1084639

Browse files
authored
feat: release from the changelog (#7)
Generator releases no longer need a hand-made tag. When `CHANGELOG.md` has entries under `## Unreleased`, the tibia-sh App opens `chore: release <next>`. Merging it is the one human step: the App tags the merge, `Release` builds and attests as today, and it then tells tibiawiki-mcp, which moves to the new wheel by itself. changes: - added `scripts/release_pr.py`, which picks the next `+tibiash.N`, releases the changelog section, and decides whether a push is the approved release merge - added `release-pr.yml`, which opens or updates the release PR and tags only a release PR merged by the maintainer - sent `generator-release` to tibiawiki-mcp after a published tag release, never after a dry run - added `alert.yml`, which comments on "Automation needs a look" when a release, release PR or upstream check fails - documented the flow, the recovery and the upstream sync rules in the README
2 parents c685c76 + a1d69bb commit 1084639

7 files changed

Lines changed: 1513 additions & 11 deletions

File tree

‎.github/workflows/alert.yml‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
name: Alert
2+
3+
# Tells the maintainer when a run of the release, release PR or upstream check workflow did not pass. It comments
4+
# `<workflow> <conclusion>: <run url>` on the open issue titled `Automation needs a look`, or opens that issue assigned
5+
# to drptbl. workflow_run matches a workflow by its name:, so the names below are release.yml's, release-pr.yml's and
6+
# upstream.yml's. A pull request runs none of them, and one never alerts. Runs of every workflow that writes the issue
7+
# share the concurrency group automation-alert, wait for each other and are never replaced, so no alert is dropped and
8+
# no second issue is opened.
9+
on:
10+
workflow_run:
11+
workflows: [Release, Release PR, Upstream check]
12+
types: [completed]
13+
14+
permissions: {}
15+
16+
defaults:
17+
run:
18+
shell: bash
19+
20+
jobs:
21+
alert:
22+
if: github.event.workflow_run.conclusion != 'success' &&
23+
github.event.workflow_run.conclusion != 'skipped' &&
24+
github.event.workflow_run.conclusion != 'neutral' &&
25+
github.event.workflow_run.event != 'pull_request'
26+
runs-on: ubuntu-latest
27+
timeout-minutes: 5
28+
permissions:
29+
issues: write
30+
concurrency:
31+
group: automation-alert
32+
cancel-in-progress: false
33+
queue: max
34+
steps:
35+
# No checkout: gh alone, on this repository, with GITHUB_TOKEN. The event's fields reach the shell through env.
36+
# The issue is found by its exact title in the open issues, not by a search, whose index can lag behind an issue
37+
# opened moments ago.
38+
- name: Comment on the issue, or open it
39+
run: |
40+
title='Automation needs a look'
41+
body="$WORKFLOW $CONCLUSION: $RUN_URL"
42+
number=$(TITLE=$title gh issue list --state open --limit 1000 --json number,title \
43+
--jq 'map(select(.title == env.TITLE) | .number) | min // empty')
44+
if [[ -n $number ]]; then
45+
gh issue comment "$number" --body "$body"
46+
else
47+
gh issue create --title "$title" --assignee drptbl --body "$body"
48+
fi
49+
env:
50+
GH_TOKEN: ${{ github.token }}
51+
GH_REPO: ${{ github.repository }}
52+
WORKFLOW: ${{ github.event.workflow_run.name }}
53+
CONCLUSION: ${{ github.event.workflow_run.conclusion }}
54+
RUN_URL: ${{ github.event.workflow_run.html_url }}

‎.github/workflows/release-pr.yml‎

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
name: Release PR
2+
3+
# Keeps one release pull request, chore: release <version>, open from release/next while CHANGELOG.md has entries
4+
# under ## Unreleased. scripts/release_pr.py renames that section to the next +tibiash.N version and sets __version__
5+
# to it. The tibia-sh App pushes the branch and opens the pull request, so the pull request's CI runs, which it would
6+
# not for one opened with GITHUB_TOKEN. Nothing merges it by itself: drptbl's merge is the release. The tag job of that
7+
# merge's run tags v<version> on the merge commit, and the tag starts release.yml.
8+
on:
9+
push:
10+
branches:
11+
- main
12+
workflow_dispatch:
13+
14+
permissions: {}
15+
16+
# Runs take turns, and a pending run is never replaced, so the run of every push to main, a release merge's included,
17+
# runs to the end.
18+
concurrency:
19+
group: release-pr
20+
cancel-in-progress: false
21+
queue: max
22+
23+
defaults:
24+
run:
25+
shell: bash
26+
27+
jobs:
28+
# Mints the App's token in the release-trigger environment, which deploys from main alone. The job installs
29+
# nothing, restores no cache and runs no dependency code: only git, gh and the standard-library
30+
# scripts/release_pr.py from its own checkout, under python3 -I. It commits only the two files the script wrote.
31+
propose:
32+
runs-on: ubuntu-latest
33+
timeout-minutes: 10
34+
environment: release-trigger
35+
permissions:
36+
contents: read # the checkout and its tags
37+
pull-requests: read # the open release pull request
38+
steps:
39+
- name: Check out
40+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
41+
with:
42+
fetch-depth: 0
43+
fetch-tags: true
44+
persist-credentials: false
45+
# Exit 3 means CHANGELOG.md has no unreleased entries and nothing was written. The script checks that before it
46+
# works out a version, so a release merge's run, whose version is not tagged yet, ends here green.
47+
- name: Write the release
48+
id: propose
49+
run: |
50+
tags="$RUNNER_TEMP/tags.txt"
51+
git show-ref --tags --dereference > "$tags"
52+
if version="$(python3 -I scripts/release_pr.py propose --tags "$tags" --body "$RUNNER_TEMP/body.md")"; then
53+
if [[ ! $version =~ ^[0-9]+\.[0-9]+\.[0-9]+\+tibiash\.[0-9]+$ ]]; then
54+
echo "::error::release_pr.py propose printed a version that is not x.y.z+tibiash.N."
55+
exit 1
56+
fi
57+
echo "Proposing $version."
58+
echo "version=$version" >> "$GITHUB_OUTPUT"
59+
else
60+
status=$?
61+
if [ "$status" -ne 3 ]; then
62+
exit "$status"
63+
fi
64+
echo "CHANGELOG.md has no unreleased entries, so there is nothing to release."
65+
fi
66+
# With github.token. The App's login is its slug with [bot], as GitHub shows its pull requests.
67+
- name: Find the open release pull request
68+
id: pr
69+
env:
70+
GH_TOKEN: ${{ github.token }}
71+
GH_REPO: ${{ github.repository }}
72+
APP_LOGIN: ${{ vars.TIBIA_SH_APP_SLUG }}[bot]
73+
run: |
74+
if [[ ! $APP_LOGIN =~ ^[a-z0-9-]+\[bot\]$ ]]; then
75+
echo "::error::The App's login, from vars.TIBIA_SH_APP_SLUG, is not <slug>[bot]."
76+
exit 1
77+
fi
78+
query="head=$GITHUB_REPOSITORY_OWNER:release/next&base=main&state=open"
79+
number="$(timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/pulls?$query" \
80+
--jq 'map(select(.user.login == env.APP_LOGIN)) | .[0].number // empty')"
81+
if [[ ! $number =~ ^[0-9]*$ ]]; then
82+
echo "::error::The open release pull request has no plain number."
83+
exit 1
84+
fi
85+
echo "number=$number" >> "$GITHUB_OUTPUT"
86+
# A token of the App for this repository alone, which expires within the hour: pull requests, to close the one
87+
# that has nothing left to release.
88+
- id: close_token
89+
if: ${{ steps.propose.outputs.version == '' && steps.pr.outputs.number != '' }}
90+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
91+
with:
92+
client-id: ${{ vars.TIBIA_SH_APP_CLIENT_ID }}
93+
private-key: ${{ secrets.TIBIA_SH_APP_PRIVATE_KEY }}
94+
owner: tibia-sh
95+
repositories: tibiawiki-sql
96+
permission-pull-requests: write
97+
- name: Close the release pull request
98+
if: ${{ steps.propose.outputs.version == '' && steps.pr.outputs.number != '' }}
99+
env:
100+
GH_TOKEN: ${{ steps.close_token.outputs.token }}
101+
GH_REPO: ${{ github.repository }}
102+
PR: ${{ steps.pr.outputs.number }}
103+
run: |
104+
timeout --kill-after=10 120 gh pr close "$PR" \
105+
--comment "CHANGELOG.md on main has no unreleased entries, so there is nothing to release."
106+
- name: Commit the release
107+
if: ${{ steps.propose.outputs.version != '' }}
108+
env:
109+
VERSION: ${{ steps.propose.outputs.version }}
110+
GIT_AUTHOR_NAME: github-actions[bot]
111+
GIT_AUTHOR_EMAIL: 41898282+github-actions[bot]@users.noreply.github.com
112+
GIT_COMMITTER_NAME: github-actions[bot]
113+
GIT_COMMITTER_EMAIL: 41898282+github-actions[bot]@users.noreply.github.com
114+
run: |
115+
if [[ "$(git status --porcelain)" != $' M CHANGELOG.md\n M tibiawikisql/__init__.py' ]]; then
116+
echo "::error::propose must change CHANGELOG.md and tibiawikisql/__init__.py, and nothing else."
117+
exit 1
118+
fi
119+
git add CHANGELOG.md tibiawikisql/__init__.py
120+
git commit --quiet -m "chore: release $VERSION"
121+
# A token of the App for this repository alone, which expires within the hour: contents, to push release/next,
122+
# and pull requests, to open or update its pull request. Minted after the release is committed, so nothing
123+
# before this step runs with it.
124+
- id: push_token
125+
if: ${{ steps.propose.outputs.version != '' }}
126+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
127+
with:
128+
client-id: ${{ vars.TIBIA_SH_APP_CLIENT_ID }}
129+
private-key: ${{ secrets.TIBIA_SH_APP_PRIVATE_KEY }}
130+
owner: tibia-sh
131+
repositories: tibiawiki-sql
132+
permission-contents: write
133+
permission-pull-requests: write
134+
# Force-pushes release/next, which replaces the previous proposal, then opens the pull request or updates the open
135+
# one. The token reaches git through a credential helper that reads it from the environment, so it is never
136+
# written to .git/config and never appears on a command line. Auto-merge stays off.
137+
- name: Push release/next and open or update its pull request
138+
if: ${{ steps.propose.outputs.version != '' }}
139+
env:
140+
GH_TOKEN: ${{ steps.push_token.outputs.token }}
141+
GH_REPO: ${{ github.repository }}
142+
VERSION: ${{ steps.propose.outputs.version }}
143+
PR: ${{ steps.pr.outputs.number }}
144+
run: |
145+
title="chore: release $VERSION"
146+
# shellcheck disable=SC2016 # git's shell expands $GH_TOKEN when it runs the helper
147+
timeout --kill-after=10 120 git -c credential.helper= \
148+
-c 'credential.helper=!f() { echo username=x-access-token; echo "password=$GH_TOKEN"; }; f' \
149+
push --force origin HEAD:refs/heads/release/next
150+
if [[ -n $PR ]]; then
151+
timeout --kill-after=10 120 gh api --method PATCH "repos/{owner}/{repo}/pulls/$PR" --silent \
152+
-f title="$title" -F body=@"$RUNNER_TEMP/body.md"
153+
else
154+
timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/pulls" --silent \
155+
-f head=release/next -f base=main -f title="$title" -F body=@"$RUNNER_TEMP/body.md"
156+
fi
157+
158+
# Tags the release that drptbl's merge of the release pull request made. The list of a commit's pull requests has no
159+
# merged_by, so the job reads, with github.token, the pull request whose merge commit is the pushed commit on its
160+
# own, and scripts/release_pr.py decides from it. Only the App's release/next pull request, merged by drptbl, tags
161+
# v<version> at the merge commit. The same isolation as the propose job, and the App's token, which the release tags
162+
# ruleset lets create v* tags, is minted only to create the tag. The tag's push starts release.yml.
163+
tag:
164+
runs-on: ubuntu-latest
165+
timeout-minutes: 10
166+
environment: release-trigger
167+
permissions:
168+
contents: read # the checkout and its tags
169+
pull-requests: read # the pull request the pushed commit merged
170+
steps:
171+
- name: Check out
172+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
173+
with:
174+
fetch-depth: 0
175+
fetch-tags: true
176+
persist-credentials: false
177+
# With no merged pull request, as for a direct push, the script is given null. It fails when the version is
178+
# untagged, and skips otherwise.
179+
- name: Read the merged pull request
180+
env:
181+
GH_TOKEN: ${{ github.token }}
182+
GH_REPO: ${{ github.repository }}
183+
run: |
184+
numbers="$(timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/commits/$GITHUB_SHA/pulls" \
185+
--jq '[.[] | select(.merge_commit_sha == env.GITHUB_SHA and .merged_at) | "\(.number)"] | join(" ")')"
186+
if [[ -z $numbers ]]; then
187+
echo null > "$RUNNER_TEMP/pr.json"
188+
elif [[ $numbers =~ ^[0-9]+$ ]]; then
189+
timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/pulls/$numbers" > "$RUNNER_TEMP/pr.json"
190+
else
191+
echo "::error::More than one merged pull request has $GITHUB_SHA as its merge commit."
192+
exit 1
193+
fi
194+
# fail turns the run red: the version is not x.y.z+tibiash.N, its tag points at another commit, or it has no
195+
# tag and no merged pull request was found, as for a release merge GitHub did not list yet.
196+
- name: Decide
197+
id: decide
198+
env:
199+
APP_LOGIN: ${{ vars.TIBIA_SH_APP_SLUG }}[bot]
200+
run: |
201+
tags="$RUNNER_TEMP/tags.txt"
202+
git show-ref --tags --dereference > "$tags"
203+
result="$(python3 -I scripts/release_pr.py tag-decision --tags "$tags" --pr "$RUNNER_TEMP/pr.json" \
204+
--head "$GITHUB_SHA" --app-login "$APP_LOGIN")"
205+
decision="$(sed -n 's/^decision=//p' <<< "$result")"
206+
version="$(sed -n 's/^version=//p' <<< "$result")"
207+
case $decision in
208+
tag) echo "Tagging v$version at $GITHUB_SHA." ;;
209+
noop) echo "v$version already points at $GITHUB_SHA." ;;
210+
skip) echo "$GITHUB_SHA is not drptbl's merge of the App's release pull request, so nothing is tagged." ;;
211+
fail)
212+
echo "::error::v$version cannot be tagged at $GITHUB_SHA. The version is not x.y.z+tibiash.N," \
213+
"its tag points at another commit, or it has no tag and no merged pull request was found." \
214+
"For a release merge, re-run this run once the pull request shows as merged."
215+
exit 1
216+
;;
217+
*)
218+
echo "::error::release_pr.py tag-decision printed an unknown decision."
219+
exit 1
220+
;;
221+
esac
222+
echo "decision=$decision" >> "$GITHUB_OUTPUT"
223+
echo "version=$version" >> "$GITHUB_OUTPUT"
224+
# A token of the App for this repository alone, which expires within the hour: contents, to create the tag.
225+
- id: tag_token
226+
if: ${{ steps.decide.outputs.decision == 'tag' }}
227+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
228+
with:
229+
client-id: ${{ vars.TIBIA_SH_APP_CLIENT_ID }}
230+
private-key: ${{ secrets.TIBIA_SH_APP_PRIVATE_KEY }}
231+
owner: tibia-sh
232+
repositories: tibiawiki-sql
233+
permission-contents: write
234+
- name: Create the tag
235+
if: ${{ steps.decide.outputs.decision == 'tag' }}
236+
env:
237+
GH_TOKEN: ${{ steps.tag_token.outputs.token }}
238+
GH_REPO: ${{ github.repository }}
239+
VERSION: ${{ steps.decide.outputs.version }}
240+
run: |
241+
if [[ ! $VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+\+tibiash\.[0-9]+$ || ! $GITHUB_SHA =~ ^[0-9a-f]{40}$ ]]; then
242+
echo "::error::The version is not x.y.z+tibiash.N, or the pushed commit is not a full SHA."
243+
exit 1
244+
fi
245+
timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/git/refs" --silent \
246+
-f ref="refs/tags/v$VERSION" -f sha="$GITHUB_SHA"
247+
echo "Tagged v$VERSION at $GITHUB_SHA."

‎.github/workflows/release.yml‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ jobs:
147147
contents: write # gh release create
148148
id-token: write # sign the build provenance attestations
149149
attestations: write # store the attestations
150+
outputs:
151+
published: ${{ steps.publish.outputs.published }} # 'true' once the release and its assets are uploaded
150152
env:
151153
UV_PYTHON: "3.10"
152154
VERSION: ${{ needs.check.outputs.version }}
@@ -229,6 +231,7 @@ jobs:
229231
${{ steps.build.outputs.sdist }}
230232
dist/SHA256SUMS
231233
- name: Create the release
234+
id: publish
232235
if: github.event_name == 'push'
233236
env:
234237
GH_TOKEN: ${{ github.token }}
@@ -238,3 +241,58 @@ jobs:
238241
run: |
239242
gh release create "$GITHUB_REF_NAME" "$WHEEL" "$SDIST" dist/SHA256SUMS \
240243
--verify-tag --notes-file "$RUNNER_TEMP/notes.md"
244+
echo "published=true" >> "$GITHUB_OUTPUT"
245+
246+
# Tells tibia-sh/tibiawiki-mcp about the release, so its generator.yml pins the version. That workflow verifies the
247+
# wheel's attestation, so this job runs only once the release job has uploaded the wheel it attested. A dry run
248+
# publishes nothing and never gets here. A job of its own, because it mints the tibia-sh App's token in the
249+
# release-trigger environment, which deploys from main and v* tags alone. It checks nothing out and runs only gh
250+
# and jq.
251+
downstream:
252+
needs: [check, release]
253+
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') &&
254+
needs.release.outputs.published == 'true'
255+
runs-on: ubuntu-latest
256+
timeout-minutes: 8
257+
environment: release-trigger
258+
permissions: {}
259+
env:
260+
VERSION: ${{ needs.check.outputs.version }}
261+
steps:
262+
# A token of the App for tibiawiki-mcp alone, which expires within the hour. A repository_dispatch needs
263+
# contents write, and the token can do nothing else.
264+
- id: token
265+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
266+
with:
267+
client-id: ${{ vars.TIBIA_SH_APP_CLIENT_ID }}
268+
private-key: ${{ secrets.TIBIA_SH_APP_PRIVATE_KEY }}
269+
owner: tibia-sh
270+
repositories: tibiawiki-mcp
271+
permission-contents: write
272+
# gh api exits non-zero on an HTTP error and prints it, and timeout stops a request that hangs after 120
273+
# seconds, or kills it 10 seconds later if it ignores that. A dispatch that got through twice is harmless,
274+
# because generator.yml finds the version pinned or its pull request open, so a failed attempt is tried again
275+
# 30 seconds later, three attempts in all. Three attempts that all hang take 7.5 minutes, inside the job's 8.
276+
- name: Tell tibiawiki-mcp about the release
277+
env:
278+
GH_TOKEN: ${{ steps.token.outputs.token }}
279+
run: |
280+
if [[ ! $VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+\+tibiash\.[0-9]+$ || $GITHUB_REF_NAME != "v$VERSION" ]]; then
281+
echo "::error::The released version is not x.y.z+tibiash.N, or it is not the tag's."
282+
exit 1
283+
fi
284+
# shellcheck disable=SC2016 # $version is jq's, not the shell's
285+
payload='{event_type: "generator-release", client_payload: {version: $version}}'
286+
body="$(jq -n --arg version "$VERSION" "$payload")"
287+
for attempt in 1 2 3; do
288+
if [ "$attempt" -gt 1 ]; then
289+
sleep 30
290+
fi
291+
if timeout --kill-after=10 120 gh api repos/tibia-sh/tibiawiki-mcp/dispatches --input - <<< "$body"; then
292+
echo "Told tibia-sh/tibiawiki-mcp about $VERSION in attempt $attempt."
293+
exit 0
294+
fi
295+
done
296+
echo "::error::Could not tell tibia-sh/tibiawiki-mcp about $VERSION in 3 attempts, 30 seconds apart." \
297+
"Run generator.yml there by hand with the version, as README.md describes."
298+
exit 1

‎MANIFEST.in‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ recursive-include tibiawikisql
77
recursive-exclude * __pycache__
88
recursive-exclude * *.py[co]
99
exclude tests/test_upstream_check.py
10+
exclude tests/test_release_pr.py

0 commit comments

Comments
 (0)