Skip to content

Commit 49ed8ff

Browse files
committed
ci: tag and dispatch releases as the App
1 parent c09c2d6 commit 49ed8ff

4 files changed

Lines changed: 341 additions & 5 deletions

File tree

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

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ name: Release PR
33
# Keeps one release pull request, chore: release <version>, open from release/next while CHANGELOG.md has entries
44
# under ## Unreleased. scripts/release_pr.py renames that section to the next +tibiash.N version and sets __version__
55
# 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.
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.
78
on:
89
push:
910
branches:
@@ -153,3 +154,91 @@ jobs:
153154
timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/pulls" --silent \
154155
-f head=release/next -f base=main -f title="$title" -F body=@"$RUNNER_TEMP/body.md"
155156
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 and skips.
178+
- name: Read the merged pull request
179+
env:
180+
GH_TOKEN: ${{ github.token }}
181+
GH_REPO: ${{ github.repository }}
182+
run: |
183+
numbers="$(timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/commits/$GITHUB_SHA/pulls" \
184+
--jq '[.[] | select(.merge_commit_sha == env.GITHUB_SHA and .merged_at) | "\(.number)"] | join(" ")')"
185+
if [[ -z $numbers ]]; then
186+
echo null > "$RUNNER_TEMP/pr.json"
187+
elif [[ $numbers =~ ^[0-9]+$ ]]; then
188+
timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/pulls/$numbers" > "$RUNNER_TEMP/pr.json"
189+
else
190+
echo "::error::More than one merged pull request has $GITHUB_SHA as its merge commit."
191+
exit 1
192+
fi
193+
# fail turns the run red: the version is not x.y.z+tibiash.N, or its tag points at another commit.
194+
- name: Decide
195+
id: decide
196+
env:
197+
APP_LOGIN: ${{ vars.TIBIA_SH_APP_SLUG }}[bot]
198+
run: |
199+
tags="$RUNNER_TEMP/tags.txt"
200+
git show-ref --tags --dereference > "$tags"
201+
result="$(python3 -I scripts/release_pr.py tag-decision --tags "$tags" --pr "$RUNNER_TEMP/pr.json" \
202+
--head "$GITHUB_SHA" --app-login "$APP_LOGIN")"
203+
decision="$(sed -n 's/^decision=//p' <<< "$result")"
204+
version="$(sed -n 's/^version=//p' <<< "$result")"
205+
case $decision in
206+
tag) echo "Tagging v$version at $GITHUB_SHA." ;;
207+
noop) echo "v$version already points at $GITHUB_SHA." ;;
208+
skip) echo "$GITHUB_SHA is not drptbl's merge of the App's release pull request, so nothing is tagged." ;;
209+
fail)
210+
echo "::error::v$version cannot be tagged at $GITHUB_SHA." \
211+
"The version is not x.y.z+tibiash.N, or the tag points at another commit."
212+
exit 1
213+
;;
214+
*)
215+
echo "::error::release_pr.py tag-decision printed an unknown decision."
216+
exit 1
217+
;;
218+
esac
219+
echo "decision=$decision" >> "$GITHUB_OUTPUT"
220+
echo "version=$version" >> "$GITHUB_OUTPUT"
221+
# A token of the App for this repository alone, which expires within the hour: contents, to create the tag.
222+
- id: tag_token
223+
if: ${{ steps.decide.outputs.decision == 'tag' }}
224+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
225+
with:
226+
client-id: ${{ vars.TIBIA_SH_APP_CLIENT_ID }}
227+
private-key: ${{ secrets.TIBIA_SH_APP_PRIVATE_KEY }}
228+
owner: tibia-sh
229+
repositories: tibiawiki-sql
230+
permission-contents: write
231+
- name: Create the tag
232+
if: ${{ steps.decide.outputs.decision == 'tag' }}
233+
env:
234+
GH_TOKEN: ${{ steps.tag_token.outputs.token }}
235+
GH_REPO: ${{ github.repository }}
236+
VERSION: ${{ steps.decide.outputs.version }}
237+
run: |
238+
if [[ ! $VERSION =~ ^[0-9]+\.[0-9]+\.[0-9]+\+tibiash\.[0-9]+$ || ! $GITHUB_SHA =~ ^[0-9a-f]{40}$ ]]; then
239+
echo "::error::The version is not x.y.z+tibiash.N, or the pushed commit is not a full SHA."
240+
exit 1
241+
fi
242+
timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/git/refs" --silent \
243+
-f ref="refs/tags/v$VERSION" -f sha="$GITHUB_SHA"
244+
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

‎README.md‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,32 @@ version and opens or updates the pull request `chore: release <version>` from th
3232
version counts `+tibiash.N` up from the highest tagged `N` of the same upstream version, so `9.0.0+tibiash.2` is
3333
followed by `9.0.0+tibiash.3`, and an upstream `9.1.0` by `9.1.0+tibiash.1`. When the section is empty or gone, the
3434
workflow closes that pull request. The tibia-sh App, `tibia-sh-bot`, pushes the branch and opens the pull request, so
35-
its CI runs. Nothing merges it by itself. After the merge, push the tag `v<version>`, like `v9.0.0+tibiash.1`, on the
36-
merge commit.
35+
its CI runs. Nothing merges it by itself.
36+
37+
The release is drptbl's merge of that pull request. The Release PR workflow's run for the merge tags `v<version>`,
38+
like `v9.0.0+tibiash.1`, on the merge commit, as the App, which the `release tags` ruleset lets create `v*` tags. A
39+
merge by anyone else, a pull request from another branch or author, and any other push to `main` tag nothing. The tag
40+
starts the release workflow.
3741

3842
The release workflow checks that the tag matches the version and that the commit is on `main`. It runs the tests and
3943
checks an installed build of the wheel, both without write access. The publishing job then builds the wheel and the
4044
sdist with only the hashed build dependencies in `build-constraints.txt`, checks the wheel's version, writes
4145
`SHA256SUMS`, attests all three files and publishes them with the changelog section as the release notes. A running
42-
release is left to finish. A newer run for the same tag can still replace one that is waiting to start. Running the
43-
workflow by hand on `main` is a dry run: it skips the tag checks and the release. On any other branch it fails.
46+
release is left to finish. A newer run for the same tag can still replace one that is waiting to start. Once the
47+
release is published, the workflow sends `generator-release` with the version to tibia-sh/tibiawiki-mcp, whose
48+
generator workflow verifies the wheel's attestation and pins it. Running the workflow by hand on `main` is a dry run: it
49+
skips the tag checks, the release and the dispatch. On any other branch it fails.
50+
51+
When a step fails:
52+
53+
* The Release PR workflow's `tag` job failed: re-run that run. A run dispatched on `main` checks the newest commit of
54+
`main`, not the merge commit. Until the version is tagged, the next proposal fails, so no version is proposed twice.
55+
* The release workflow's `downstream` job failed: the release is published, so don't re-run the release run. Run
56+
`generator.yml` in tibia-sh/tibiawiki-mcp by hand with the version.
57+
58+
The App's private key is the tibia-sh organization secret `TIBIA_SH_APP_PRIVATE_KEY`. If it leaks, generate a new key
59+
in the App's settings, put it in the secret and delete the leaked key. Then look for tags, releases, branches
60+
and pull requests the App made in tibia-sh's repositories that no workflow run explains, and delete them.
4461

4562
To check a download, run `sha256sum -c SHA256SUMS` and this for each file:
4663

‎tests/test_release_pr.py‎

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,3 +521,175 @@ def test_app_tokens_reach_only_the_writing_steps(self):
521521
sorted(re.findall(r"- id: (\w+_token)\n", text)),
522522
)
523523
self.assertNotIn("merge --auto", text)
524+
525+
def test_tag_job_runs_in_the_release_environment(self):
526+
self.assert_in_job("tag", """\
527+
tag:
528+
runs-on: ubuntu-latest
529+
timeout-minutes: 10
530+
environment: release-trigger
531+
permissions:
532+
contents: read # the checkout and its tags
533+
pull-requests: read # the pull request the pushed commit merged
534+
steps:
535+
""" + self.CHECKOUT)
536+
537+
def test_reads_merged_by_from_the_pull_request_itself(self):
538+
self.assert_in_job("tag", """\
539+
- name: Read the merged pull request
540+
env:
541+
GH_TOKEN: ${{ github.token }}
542+
GH_REPO: ${{ github.repository }}
543+
run: |
544+
numbers="$(timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/commits/$GITHUB_SHA/pulls" \\
545+
--jq '[.[] | select(.merge_commit_sha == env.GITHUB_SHA and .merged_at) | "\\(.number)"] | join(" ")')"
546+
if [[ -z $numbers ]]; then
547+
echo null > "$RUNNER_TEMP/pr.json"
548+
elif [[ $numbers =~ ^[0-9]+$ ]]; then
549+
timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/pulls/$numbers" > "$RUNNER_TEMP/pr.json"
550+
else
551+
echo "::error::More than one merged pull request has $GITHUB_SHA as its merge commit."
552+
exit 1
553+
fi
554+
""")
555+
556+
def test_decides_with_the_script_and_fails_red(self):
557+
self.assert_in_job("tag", """\
558+
- name: Decide
559+
id: decide
560+
env:
561+
APP_LOGIN: ${{ vars.TIBIA_SH_APP_SLUG }}[bot]
562+
run: |
563+
tags="$RUNNER_TEMP/tags.txt"
564+
git show-ref --tags --dereference > "$tags"
565+
result="$(python3 -I scripts/release_pr.py tag-decision --tags "$tags" --pr "$RUNNER_TEMP/pr.json" \\
566+
--head "$GITHUB_SHA" --app-login "$APP_LOGIN")"
567+
decision="$(sed -n 's/^decision=//p' <<< "$result")"
568+
version="$(sed -n 's/^version=//p' <<< "$result")"
569+
case $decision in
570+
tag) echo "Tagging v$version at $GITHUB_SHA." ;;
571+
noop) echo "v$version already points at $GITHUB_SHA." ;;
572+
skip) echo "$GITHUB_SHA is not drptbl's merge of the App's release pull request, so nothing is tagged." ;;
573+
fail)
574+
echo "::error::v$version cannot be tagged at $GITHUB_SHA." \\
575+
"The version is not x.y.z+tibiash.N, or the tag points at another commit."
576+
exit 1
577+
;;
578+
*)
579+
echo "::error::release_pr.py tag-decision printed an unknown decision."
580+
exit 1
581+
;;
582+
esac
583+
echo "decision=$decision" >> "$GITHUB_OUTPUT"
584+
echo "version=$version" >> "$GITHUB_OUTPUT"
585+
""")
586+
587+
def test_creates_the_tag_with_a_contents_token(self):
588+
self.assert_in_job("tag", """\
589+
- id: tag_token
590+
if: ${{ steps.decide.outputs.decision == 'tag' }}
591+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
592+
with:
593+
client-id: ${{ vars.TIBIA_SH_APP_CLIENT_ID }}
594+
private-key: ${{ secrets.TIBIA_SH_APP_PRIVATE_KEY }}
595+
owner: tibia-sh
596+
repositories: tibiawiki-sql
597+
permission-contents: write
598+
- name: Create the tag
599+
if: ${{ steps.decide.outputs.decision == 'tag' }}
600+
env:
601+
GH_TOKEN: ${{ steps.tag_token.outputs.token }}
602+
GH_REPO: ${{ github.repository }}
603+
VERSION: ${{ steps.decide.outputs.version }}
604+
run: |
605+
if [[ ! $VERSION =~ ^[0-9]+\\.[0-9]+\\.[0-9]+\\+tibiash\\.[0-9]+$ || ! $GITHUB_SHA =~ ^[0-9a-f]{40}$ ]]; then
606+
echo "::error::The version is not x.y.z+tibiash.N, or the pushed commit is not a full SHA."
607+
exit 1
608+
fi
609+
timeout --kill-after=10 120 gh api "repos/{owner}/{repo}/git/refs" --silent \\
610+
-f ref="refs/tags/v$VERSION" -f sha="$GITHUB_SHA"
611+
echo "Tagged v$VERSION at $GITHUB_SHA."
612+
""")
613+
614+
615+
class TestReleaseWorkflow(WorkflowTestCase):
616+
WORKFLOW = "release.yml"
617+
618+
def test_publish_step_reports_published_after_the_upload(self):
619+
self.assert_in_job("release", """\
620+
outputs:
621+
published: ${{ steps.publish.outputs.published }} # 'true' once the release and its assets are uploaded
622+
""")
623+
self.assert_in_job("release", """\
624+
- name: Create the release
625+
id: publish
626+
if: github.event_name == 'push'
627+
env:
628+
GH_TOKEN: ${{ github.token }}
629+
GH_REPO: ${{ github.repository }}
630+
WHEEL: ${{ steps.build.outputs.wheel }}
631+
SDIST: ${{ steps.build.outputs.sdist }}
632+
run: |
633+
gh release create "$GITHUB_REF_NAME" "$WHEEL" "$SDIST" dist/SHA256SUMS \\
634+
--verify-tag --notes-file "$RUNNER_TEMP/notes.md"
635+
echo "published=true" >> "$GITHUB_OUTPUT"
636+
""")
637+
release = job(self.WORKFLOW, "release")
638+
self.assertLess(release.index("uses: actions/attest-build-provenance@"), release.index("id: publish"))
639+
self.assertEqual(1, self.text().count("published=true"))
640+
641+
def test_downstream_runs_only_after_a_tag_push_published(self):
642+
# A dry run is a workflow_dispatch on main, which publishes nothing, so it has no downstream job.
643+
self.assert_in_job("downstream", """\
644+
downstream:
645+
needs: [check, release]
646+
if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/v') &&
647+
needs.release.outputs.published == 'true'
648+
runs-on: ubuntu-latest
649+
timeout-minutes: 8
650+
environment: release-trigger
651+
permissions: {}
652+
env:
653+
VERSION: ${{ needs.check.outputs.version }}
654+
steps:
655+
""")
656+
657+
def test_downstream_dispatches_with_a_contents_token(self):
658+
self.assert_in_job("downstream", """\
659+
- id: token
660+
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
661+
with:
662+
client-id: ${{ vars.TIBIA_SH_APP_CLIENT_ID }}
663+
private-key: ${{ secrets.TIBIA_SH_APP_PRIVATE_KEY }}
664+
owner: tibia-sh
665+
repositories: tibiawiki-mcp
666+
permission-contents: write
667+
""")
668+
self.assert_in_job("downstream", """\
669+
- name: Tell tibiawiki-mcp about the release
670+
env:
671+
GH_TOKEN: ${{ steps.token.outputs.token }}
672+
run: |
673+
if [[ ! $VERSION =~ ^[0-9]+\\.[0-9]+\\.[0-9]+\\+tibiash\\.[0-9]+$ || $GITHUB_REF_NAME != "v$VERSION" ]]; then
674+
echo "::error::The released version is not x.y.z+tibiash.N, or it is not the tag's."
675+
exit 1
676+
fi
677+
# shellcheck disable=SC2016 # $version is jq's, not the shell's
678+
payload='{event_type: "generator-release", client_payload: {version: $version}}'
679+
body="$(jq -n --arg version "$VERSION" "$payload")"
680+
for attempt in 1 2 3; do
681+
if [ "$attempt" -gt 1 ]; then
682+
sleep 30
683+
fi
684+
if timeout --kill-after=10 120 gh api repos/tibia-sh/tibiawiki-mcp/dispatches --input - <<< "$body"; then
685+
echo "Told tibia-sh/tibiawiki-mcp about $VERSION in attempt $attempt."
686+
exit 0
687+
fi
688+
done
689+
echo "::error::Could not tell tibia-sh/tibiawiki-mcp about $VERSION in 3 attempts, 30 seconds apart." \\
690+
"Run generator.yml there by hand with the version, as README.md describes."
691+
exit 1
692+
""")
693+
downstream = job(self.WORKFLOW, "downstream")
694+
self.assertNotIn("checkout", downstream)
695+
self.assertEqual(1, downstream.count("uses:"))

0 commit comments

Comments
 (0)