From b8b3a854df6c9e4c1f2b2dd93c53455b93f8bc56 Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Tue, 14 Jul 2026 16:25:45 +0800 Subject: [PATCH] =?UTF-8?q?fix(ci):=20deploy=5Fnotify.py=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=20black=20=E6=A0=BC=E5=BC=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/deploy_notify.py | 76 +++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 33 deletions(-) diff --git a/scripts/deploy_notify.py b/scripts/deploy_notify.py index d3b0d4219..6e8139c89 100644 --- a/scripts/deploy_notify.py +++ b/scripts/deploy_notify.py @@ -16,7 +16,6 @@ import os import sys import urllib.request - STATUS_CONFIG = { "success": { "emoji": "✅", @@ -39,13 +38,13 @@ STATUS_CONFIG = { def build_card(status: str, detail: str) -> dict: """构建飞书卡片消息。""" cfg = STATUS_CONFIG.get(status, STATUS_CONFIG["failure"]) - + commit = os.environ.get("GITHUB_SHA", "unknown")[:8] branch = os.environ.get("GITHUB_REF_NAME", "unknown") actor = os.environ.get("GITHUB_ACTOR", "system") run_id = os.environ.get("GITHUB_RUN_ID", "-") repo = os.environ.get("GITHUB_REPOSITORY", "xiaoxia/xiaoxia-saas") - + elements = [ { "tag": "div", @@ -61,33 +60,37 @@ def build_card(status: str, detail: str) -> dict: }, }, ] - + # 如果有 run_id,加一个查看详情按钮 if run_id and run_id != "-": run_url = f"https://git.xiaoxiajianji.com/{repo}/actions/runs/{run_id}" - elements.append({ - "tag": "action", - "actions": [ + elements.append( + { + "tag": "action", + "actions": [ + { + "tag": "button", + "text": {"tag": "plain_text", "content": "查看构建详情"}, + "url": run_url, + "type": "primary", + } + ], + } + ) + + # 加 Staging 访问链接 + elements.append( + { + "tag": "note", + "elements": [ { - "tag": "button", - "text": {"tag": "plain_text", "content": "查看构建详情"}, - "url": run_url, - "type": "primary", + "tag": "plain_text", + "content": "Staging: https://staging.xiaoxiajianji.com", } ], - }) - - # 加 Staging 访问链接 - elements.append({ - "tag": "note", - "elements": [ - { - "tag": "plain_text", - "content": "Staging: https://staging.xiaoxiajianji.com", - } - ], - }) - + } + ) + return { "msg_type": "interactive", "card": { @@ -107,14 +110,14 @@ def send_notification(webhook: str, status: str, detail: str) -> bool: """发送通知到 webhook。""" payload = build_card(status, detail) data = json.dumps(payload).encode("utf-8") - + req = urllib.request.Request( webhook, data=data, headers={"Content-Type": "application/json"}, method="POST", ) - + try: with urllib.request.urlopen(req, timeout=10) as resp: resp.read() @@ -127,18 +130,25 @@ def send_notification(webhook: str, status: str, detail: str) -> bool: def main(): parser = argparse.ArgumentParser(description="Staging 部署通知") - parser.add_argument("--status", required=True, choices=["success", "failure", "rollback"], - help="部署状态") + parser.add_argument( + "--status", + required=True, + choices=["success", "failure", "rollback"], + help="部署状态", + ) parser.add_argument("--detail", default="", help="详情描述") - parser.add_argument("--webhook", default=os.environ.get("CI_NOTIFY_WEBHOOK", ""), - help="Webhook URL (也可通过 CI_NOTIFY_WEBHOOK 环境变量设置)") - + parser.add_argument( + "--webhook", + default=os.environ.get("CI_NOTIFY_WEBHOOK", ""), + help="Webhook URL (也可通过 CI_NOTIFY_WEBHOOK 环境变量设置)", + ) + args = parser.parse_args() - + if not args.webhook: print("未配置 webhook URL,跳过通知") return 0 - + send_notification(args.webhook, args.status, args.detail) return 0