Trigger CI badges after workflow completion

This commit is contained in:
2026-10-18 19:47:34 -06:00
parent a4f499ce12
commit 017a0eb22c
3 changed files with 15 additions and 8 deletions
+3 -2
View File
@@ -1,8 +1,9 @@
name: completed-ci-badge
on:
schedule:
- cron: '*/15 * * * *'
workflow_run:
workflows: [main-release-gate, next-build]
types: [completed]
workflow_dispatch:
concurrency:
+2 -2
View File
@@ -55,8 +55,8 @@ def publish(api: str, repo: str, branch: str, data: bytes, token: str) -> None:
def main() -> None:
if os.environ["GITHUB_EVENT_NAME"] not in ("schedule", "workflow_dispatch"):
raise RuntimeError("badge publisher must run on a timer or manual dispatch")
if os.environ["GITHUB_EVENT_NAME"] not in ("workflow_run", "workflow_dispatch"):
raise RuntimeError("badge publisher must follow CI completion or manual dispatch")
if os.environ["GITHUB_REF"] != "refs/heads/main":
raise RuntimeError("badge publisher must use the main branch")
repo = os.environ["GITHUB_REPOSITORY"]
+10 -4
View File
@@ -36,7 +36,7 @@ assert b">fail</text>" in svg
assert b"#d1242f" in svg
assert b">pass</text>" in module.badge("next", "success", 108)
environment = {
"GITHUB_EVENT_NAME": "schedule",
"GITHUB_EVENT_NAME": "workflow_run",
"GITHUB_REF": "refs/heads/main",
"GITHUB_REPOSITORY": "owner/repo",
"GITHUB_API_URL": "https://example.invalid/api/v1",
@@ -49,13 +49,19 @@ with patch.dict(os.environ, environment), \
module.main()
assert latest.call_count == 2
assert [call.args[2] for call in published.call_args_list] == ["main", "next"]
with patch.dict(os.environ, {**environment, "GITHUB_EVENT_NAME": "workflow_run"}):
with patch.dict(os.environ, {**environment, "GITHUB_EVENT_NAME": "workflow_dispatch"}), \
patch.object(module, "latest_completed", side_effect=[(201, "success"),
(202, "failure")]), \
patch.object(module, "publish") as published:
module.main()
assert published.call_count == 2
with patch.dict(os.environ, {**environment, "GITHUB_EVENT_NAME": "schedule"}):
try:
module.main()
except RuntimeError as error:
assert "timer or manual dispatch" in str(error)
assert "CI completion or manual dispatch" in str(error)
else:
raise AssertionError("workflow_run event entered badge publisher")
raise AssertionError("scheduled event entered badge publisher")
for invalid in ("", "/main.svg", "../main.svg", "badges//main.svg"):
try:
publish_file("https://example.invalid/api/v1", "owner/repo", invalid,