From 1b42ac77658604cd2290fcc45e8b38514e923a34 Mon Sep 17 00:00:00 2001 From: zheromiku <836283459@qq.com> Date: Mon, 28 Sep 2026 18:38:36 +0800 Subject: [PATCH] =?UTF-8?q?ci:=20=E6=8E=A5=E5=85=A5=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E8=A7=84=E8=8C=83=E9=97=B8=E9=97=A8=E4=B8=8E=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E7=BC=96=E8=AF=91=E6=B5=81=E6=B0=B4=E7=BA=BF(=E6=9C=AC?= =?UTF-8?q?=E6=9C=BARunner+=E5=BE=AE=E4=BF=A1=E9=80=9A=E7=9F=A5)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/scripts/ci_report.py | 82 +++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 .gitea/scripts/ci_report.py diff --git a/.gitea/scripts/ci_report.py b/.gitea/scripts/ci_report.py new file mode 100644 index 0000000..42dcd5d --- /dev/null +++ b/.gitea/scripts/ci_report.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 +"""把 CI 结果上报给主机侧 ci-bridge(bridge 负责落报告页 + 推微信)。""" +import json +import os +import subprocess +import urllib.request + + +def rd(path, default=""): + try: + with open(path, encoding="utf-8") as fh: + return fh.read().strip() + except Exception: + return default + + +def git(*args): + try: + return subprocess.run(["git"] + list(args), capture_output=True, + text=True).stdout.strip() + except Exception: + return "" + + +def main(): + repo = os.environ.get("GITHUB_REPOSITORY", "unknown/unknown") + sha = rd("/tmp/ci-sha") or git("rev-parse", "HEAD") + branch = os.environ.get("GITHUB_REF_NAME") or \ + os.environ.get("GITHUB_REF", "").replace("refs/heads/", "") + gate = rd("/tmp/ci-gate-status", "skipped") + build = rd("/tmp/ci-build-status", "failure") + status = "success" if (gate == "success" and build == "success") else "failure" + + log = rd("/tmp/ci-build-tail") + if gate != "success": + log = "提交信息规范校验未通过\n" + log + + try: + count = int(rd("/tmp/ci-commits-count", "1") or 1) + except Exception: + count = 1 + + rep = { + "repo": repo, + "sha": sha, + "branch": branch, + "actor": os.environ.get("GITHUB_ACTOR", ""), + "status": status, + "commit_subject": git("log", "-1", "--format=%s"), + "commit_count": count, + "duration_s": rd("/tmp/ci-duration", "?"), + "preview_url": rd("/tmp/ci-preview-url"), + "log": log[-3500:], + "steps": [ + {"name": "提交信息规范校验", "status": gate}, + {"name": "编译与自检", "status": build}, + ], + } + if rep["preview_url"]: + print("\U0001F310 网页预览: %s" % rep["preview_url"]) + print("结果: %s | 构建: %s | 提交闸门: %s | 耗时: %ss" + % (status, build, gate, rep["duration_s"])) + + base = os.environ.get("CI_BRIDGE_RESOLVED", "").rstrip("/") + if not base: + print("跳过上报:未找到可达的 ci-bridge") + return 0 + data = json.dumps(rep, ensure_ascii=False).encode("utf-8") + req = urllib.request.Request( + base + "/api/ci/report", data=data, method="POST", + headers={"Content-Type": "application/json", + "X-CI-Token": os.environ.get("BRIDGE_TOKEN", "")}) + try: + with urllib.request.urlopen(req, timeout=30) as resp: + print("\u2705 已上报 ci-bridge:", resp.read().decode()[:300]) + except Exception as e: + print("\u274c 上报 ci-bridge 失败:", e) + return 0 + + +if __name__ == "__main__": + raise SystemExit(main())