添加webhook

This commit is contained in:
孙小云 2026-01-27 14:39:51 +08:00
parent 1ab8622ca6
commit 838a198804
2 changed files with 73 additions and 10 deletions

View File

@ -10,6 +10,7 @@ import sys
import time
import yaml
import subprocess
import socket
from datetime import datetime
from pathlib import Path
@ -110,6 +111,22 @@ class GitMonitor:
except Exception as e:
Logger.warning(f"钉钉通知初始化失败: {e}")
def get_server_ip(self):
"""获取服务器IP地址"""
try:
# 创建一个UDP socket连接到外部地址来获取本机IP
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
except Exception:
try:
# 备用方案获取主机名对应的IP
return socket.gethostbyname(socket.gethostname())
except Exception:
return "unknown"
def get_remote_commit(self, repo_url, branch):
"""获取远程仓库的最新提交 hash"""
try:
@ -124,6 +141,19 @@ class GitMonitor:
Logger.error(f"获取远程提交失败 {repo_url}: {e}")
return None
def get_commit_message(self, repo_url, commit_hash):
"""获取指定 commit 的提交消息"""
try:
cmd = f"git ls-remote --heads {repo_url} | grep {commit_hash[:8]}"
result = subprocess.run(
cmd, shell=True, capture_output=True, text=True, timeout=10
)
# 由于 ls-remote 无法获取 commit message我们返回简短的 hash
return f"提交 {commit_hash[:8]}"
except Exception as e:
Logger.error(f"获取提交消息失败: {e}")
return f"提交 {commit_hash[:8]}"
def check_repository(self, repo_config):
"""检查单个仓库是否有新提交"""
repo_name = repo_config['name']
@ -292,19 +322,40 @@ class GitMonitor:
Logger.info(f"开始部署: {repo_name}")
Logger.separator()
# 发送构建开始通知
if self.dingtalk_notifier:
self.dingtalk_notifier.send_build_start(
repo_name=repo_name,
branch=self.global_branch,
commit_hash=commit_hash
)
try:
# 1. 更新主仓库和子模块
if not self.update_main_repo():
return False
# 获取子仓库的 commit message
commit_message = None
submodule_path = repo_path / repo_config['path']
if submodule_path.exists():
try:
cmd = f"git log -1 --pretty=format:'%s' {commit_hash}"
result = subprocess.run(
cmd, shell=True, cwd=submodule_path,
capture_output=True, text=True, timeout=10
)
if result.returncode == 0 and result.stdout:
commit_message = result.stdout.strip()
Logger.info(f"提交消息: {commit_message}")
except Exception as e:
Logger.warning(f"获取提交消息失败: {e}")
# 获取服务器 IP
server_ip = self.get_server_ip()
# 发送构建开始通知(包含 commit message 和服务器 IP
if self.dingtalk_notifier:
self.dingtalk_notifier.send_build_start(
repo_name=repo_name,
branch=self.global_branch,
commit_hash=commit_hash,
commit_message=commit_message,
server_ip=server_ip
)
# 2. 初始化基础设施
if not self.init_infrastructure():
return False

View File

@ -160,7 +160,7 @@ class DingTalkNotifier:
"""
return self.send_markdown(title, text, at_all=at_all)
def send_build_start(self, repo_name, branch, commit_hash):
def send_build_start(self, repo_name, branch, commit_hash, commit_message=None, server_ip=None):
"""
发送构建开始通知
@ -168,17 +168,29 @@ class DingTalkNotifier:
repo_name: 仓库名称
branch: 分支名称
commit_hash: 提交哈希
commit_message: 提交消息可选
server_ip: 服务器IP可选
Returns:
发送结果 (True/False)
"""
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
title = f"🚀 构建开始 - {repo_name}"
# 构建消息内容
text = f"""### 🚀 构建开始
**仓库**: {repo_name}
**分支**: {branch}
**提交**: {commit_hash[:8]}
**提交**: {commit_hash[:8]}"""
if commit_message:
text += f"\n**消息**: {commit_message}"
if server_ip:
text += f"\n**服务器**: {server_ip}"
text += f"""
**时间**: {now}
---