This commit is contained in:
@@ -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())
|
||||
Reference in New Issue
Block a user