提交配置文件
Showing
2 changed files
with
140 additions
and
0 deletions
| ... | @@ -15,6 +15,8 @@ pipeline { | ... | @@ -15,6 +15,8 @@ pipeline { |
| 15 | IP_HOST_TEST = 'ai-test.hikoon.com' | 15 | IP_HOST_TEST = 'ai-test.hikoon.com' |
| 16 | IP_HOST_UAT = 'ai-uat.hikoon.com' | 16 | IP_HOST_UAT = 'ai-uat.hikoon.com' |
| 17 | IP_HOST_PROD = 'api.hikoon.com' | 17 | IP_HOST_PROD = 'api.hikoon.com' |
| 18 | // 企微Webhook URL - 包含完整的webhook地址和key | ||
| 19 | WECOM_WEBHOOK_URL = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=77f8f0d5-243a-442a-8946-bc713be7bf65' | ||
| 18 | } | 20 | } |
| 19 | 21 | ||
| 20 | stages { | 22 | stages { |
| ... | @@ -101,10 +103,28 @@ pipeline { | ... | @@ -101,10 +103,28 @@ pipeline { |
| 101 | 103 | ||
| 102 | success { | 104 | success { |
| 103 | echo "[SUCCESS] [${DEPLOY_ENV}环境] 所有测试用例运行成功!" | 105 | echo "[SUCCESS] [${DEPLOY_ENV}环境] 所有测试用例运行成功!" |
| 106 | |||
| 107 | script { | ||
| 108 | sh ''' | ||
| 109 | . venv/bin/activate | ||
| 110 | export WECOM_WEBHOOK_URL="${WECOM_WEBHOOK_URL}" | ||
| 111 | export BUILD_STATUS=success | ||
| 112 | python3 send_wecom_notification.py | ||
| 113 | ''' | ||
| 114 | } | ||
| 104 | } | 115 | } |
| 105 | 116 | ||
| 106 | failure { | 117 | failure { |
| 107 | echo "[FAILURE] [${DEPLOY_ENV}环境] 有测试用例执行失败!" | 118 | echo "[FAILURE] [${DEPLOY_ENV}环境] 有测试用例执行失败!" |
| 119 | |||
| 120 | script { | ||
| 121 | sh ''' | ||
| 122 | . venv/bin/activate | ||
| 123 | export WECOM_WEBHOOK_URL="${WECOM_WEBHOOK_URL}" | ||
| 124 | export BUILD_STATUS=failure | ||
| 125 | python3 send_wecom_notification.py | ||
| 126 | ''' | ||
| 127 | } | ||
| 108 | } | 128 | } |
| 109 | } | 129 | } |
| 110 | } | 130 | } | ... | ... |
send_wecom_notification.py
0 → 100644
| 1 | #!/usr/bin/env python3 | ||
| 2 | """ | ||
| 3 | 企微群通知脚本 | ||
| 4 | 用于在Jenkins流水线完成后发送通知到企微群 | ||
| 5 | """ | ||
| 6 | |||
| 7 | import requests | ||
| 8 | import json | ||
| 9 | import sys | ||
| 10 | from datetime import datetime | ||
| 11 | import os | ||
| 12 | |||
| 13 | def send_wecom_notification(webhook_url, env_name, build_status, build_number, job_name, jenkins_url): | ||
| 14 | """ | ||
| 15 | 发送企微群通知 | ||
| 16 | |||
| 17 | Args: | ||
| 18 | webhook_url: 企微群webhook URL | ||
| 19 | env_name: 运行环境 (test/uat/prod) | ||
| 20 | build_status: 构建状态 ('success' 或 'failure') | ||
| 21 | build_number: Jenkins构建号 | ||
| 22 | job_name: Jenkins任务名称 | ||
| 23 | jenkins_url: Jenkins服务器地址 | ||
| 24 | """ | ||
| 25 | |||
| 26 | # 构建报告链接 | ||
| 27 | report_url = f"{jenkins_url}/job/{job_name}/{build_number}/artifact/reports/{env_name}/" | ||
| 28 | |||
| 29 | # 根据状态构建消息 | ||
| 30 | if build_status == 'success': | ||
| 31 | status_emoji = "✅" | ||
| 32 | status_text = "运行成功" | ||
| 33 | status_color = "info" | ||
| 34 | result_text = "✅ 全部通过" | ||
| 35 | else: | ||
| 36 | status_emoji = "❌" | ||
| 37 | status_text = "运行失败" | ||
| 38 | status_color = "warning" | ||
| 39 | result_text = "❌ 存在失败用例" | ||
| 40 | |||
| 41 | # 获取当前时间 | ||
| 42 | current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') | ||
| 43 | |||
| 44 | # 构建Markdown格式的消息 | ||
| 45 | message_content = f"""<font color="{status_color}">{status_emoji} API自动化测试 - {status_text}</font> | ||
| 46 | |||
| 47 | **运行环境**:{env_name} | ||
| 48 | **运行结果**:{result_text} | ||
| 49 | **构建号**:{build_number} | ||
| 50 | **报告地址**:[点击查看报告]({report_url}) | ||
| 51 | |||
| 52 | 执行时间:{current_time}""" | ||
| 53 | |||
| 54 | # 构建企微消息体 | ||
| 55 | payload = { | ||
| 56 | "msgtype": "markdown", | ||
| 57 | "markdown": { | ||
| 58 | "content": message_content | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | try: | ||
| 63 | # 发送POST请求 | ||
| 64 | response = requests.post( | ||
| 65 | webhook_url, | ||
| 66 | json=payload, | ||
| 67 | timeout=10, | ||
| 68 | headers={'Content-Type': 'application/json'} | ||
| 69 | ) | ||
| 70 | |||
| 71 | # 检查响应状态 | ||
| 72 | response.raise_for_status() | ||
| 73 | |||
| 74 | # 解析返回结果 | ||
| 75 | result = response.json() | ||
| 76 | |||
| 77 | if result.get('errcode') == 0: | ||
| 78 | print(f"✅ 企微通知发送成功: {result.get('errmsg', 'OK')}") | ||
| 79 | return True | ||
| 80 | else: | ||
| 81 | print(f"❌ 企微通知发送失败: {result.get('errmsg', '未知错误')} (errcode: {result.get('errcode')})") | ||
| 82 | return False | ||
| 83 | |||
| 84 | except requests.exceptions.RequestException as e: | ||
| 85 | print(f"❌ 请求失败: {str(e)}") | ||
| 86 | return False | ||
| 87 | except json.JSONDecodeError as e: | ||
| 88 | print(f"❌ 解析响应失败: {str(e)}") | ||
| 89 | return False | ||
| 90 | except Exception as e: | ||
| 91 | print(f"❌ 发送通知异常: {str(e)}") | ||
| 92 | return False | ||
| 93 | |||
| 94 | |||
| 95 | if __name__ == "__main__": | ||
| 96 | # 从环境变量获取参数 | ||
| 97 | webhook_url = os.getenv('WECOM_WEBHOOK_URL') | ||
| 98 | env_name = os.getenv('DEPLOY_ENV', 'test') | ||
| 99 | build_status = os.getenv('BUILD_STATUS', 'success') | ||
| 100 | build_number = os.getenv('BUILD_NUMBER', 'unknown') | ||
| 101 | job_name = os.getenv('JOB_NAME', 'api_test') | ||
| 102 | jenkins_url = os.getenv('JENKINS_URL', '').rstrip('/') or 'http://localhost:8080' | ||
| 103 | |||
| 104 | # 验证必需参数 | ||
| 105 | if not webhook_url: | ||
| 106 | print("❌ 错误: 未设置 WECOM_WEBHOOK_URL 环境变量") | ||
| 107 | sys.exit(1) | ||
| 108 | |||
| 109 | # 发送通知 | ||
| 110 | success = send_wecom_notification( | ||
| 111 | webhook_url=webhook_url, | ||
| 112 | env_name=env_name, | ||
| 113 | build_status=build_status, | ||
| 114 | build_number=build_number, | ||
| 115 | job_name=job_name, | ||
| 116 | jenkins_url=jenkins_url | ||
| 117 | ) | ||
| 118 | |||
| 119 | # 返回退出码 | ||
| 120 | sys.exit(0 if success else 1) |
-
Please register or sign in to post a comment