This commit is contained in:
孙小云 2026-01-10 14:25:01 +08:00
parent 8f1d935a8c
commit d0bf31a21e
5 changed files with 31 additions and 51 deletions

Binary file not shown.

View File

@ -1,5 +1,8 @@
# DevOps 自动化部署配置文件 # DevOps 自动化部署配置文件
# 全局分支配置(所有仓库统一使用此分支)
global_branch: main
# Git 仓库配置 # Git 仓库配置
repositories: repositories:
# 认证服务 # 认证服务
@ -199,8 +202,6 @@ monitor:
# 部署配置 # 部署配置
deploy: deploy:
docker_compose_path: ./docker/docker-compose.yml docker_compose_path: ./docker/docker-compose.yml
auto_commit: true # 是否自动提交子模块更新到主仓库
commit_message: "自动更新子模块: {repo_name} 到最新版本"
# 日志配置 # 日志配置
logging: logging:

View File

@ -33,10 +33,14 @@ class Deployer:
self.runtime_path = Path(runtime_path) self.runtime_path = Path(runtime_path)
self.main_repo_url = config['main_repository']['url'] self.main_repo_url = config['main_repository']['url']
self.main_repo_branch = config['main_repository']['branch']
# 使用全局分支配置
self.global_branch = config.get('global_branch', 'main')
self.main_repo_branch = self.global_branch
self.logger.info(f"项目根目录: {project_root}") self.logger.info(f"项目根目录: {project_root}")
self.logger.info(f"Runtime 目录: {self.runtime_path}") self.logger.info(f"Runtime 目录: {self.runtime_path}")
self.logger.info(f"全局分支: {self.global_branch}")
def run_command(self, cmd, cwd=None, timeout=600): def run_command(self, cmd, cwd=None, timeout=600):
"""执行命令""" """执行命令"""
@ -109,20 +113,17 @@ class Deployer:
else: else:
self.logger.info("主仓库已存在,更新代码...") self.logger.info("主仓库已存在,更新代码...")
# 切换到指定分支 # 切换到配置的主分支
self.logger.info(f"切换到主分支: {self.main_repo_branch}")
if not self.run_command(f"git checkout {self.main_repo_branch}", cwd=repo_path): if not self.run_command(f"git checkout {self.main_repo_branch}", cwd=repo_path):
return False return False
# 拉取最新代码 # 拉取主仓库最新代码
self.logger.info("拉取主仓库最新代码...")
if not self.run_command("git pull", cwd=repo_path): if not self.run_command("git pull", cwd=repo_path):
return False return False
# 初始化子模块(如果还没初始化) # 更新所有子模块到全局配置的分支
if not self.run_command("git submodule update --init --recursive", cwd=repo_path):
return False
# 更新所有子模块到各自配置的分支
self.logger.info("更新所有子模块到最新代码...")
if not self.update_all_submodules(repo_path): if not self.update_all_submodules(repo_path):
return False return False
@ -131,38 +132,14 @@ class Deployer:
return True return True
def update_all_submodules(self, repo_path): def update_all_submodules(self, repo_path):
"""更新所有子模块到各自配置的分支""" """更新所有子模块到全局配置的分支"""
self.logger.info("更新所有子模块到各自配置的分支...") self.logger.info(f"更新所有子模块到分支: {self.global_branch}")
# 构建分支映射:子模块路径 -> 分支名 # 使用 git submodule foreach 批量更新所有子模块到全局分支
# 注意:这里假设所有子模块使用相同的分支,如果需要不同分支,需要逐个处理 cmd = f"git submodule foreach 'git checkout {self.global_branch} && git pull'"
# 先尝试使用 git submodule foreach 批量更新
# 检查是否所有子模块使用相同的分支
branches = set(repo['branch'] for repo in self.config['repositories'])
if len(branches) == 1:
# 所有子模块使用相同分支,可以使用 foreach
branch = branches.pop()
self.logger.info(f"所有子模块使用相同分支 {branch},使用 git submodule foreach 批量更新")
cmd = f"git submodule foreach 'git checkout {branch} && git pull origin {branch}'"
if not self.run_command(cmd, cwd=repo_path, timeout=600): if not self.run_command(cmd, cwd=repo_path, timeout=600):
self.logger.warning("批量更新子模块失败") self.logger.warning("批量更新子模块失败")
return False return False
else:
# 不同子模块使用不同分支,需要逐个更新
self.logger.info("子模块使用不同分支,逐个更新...")
for repo_config in self.config['repositories']:
branch = repo_config['branch']
submodule_path = repo_config['path']
self.logger.info(f"更新子模块 {repo_config['name']} 到分支 {branch}")
# 构建命令:进入子模块,切换分支并拉取
cmd = f"cd {submodule_path} && git checkout {branch} && git pull origin {branch}"
if not self.run_command(cmd, cwd=repo_path, timeout=300):
self.logger.warning(f"子模块 {repo_config['name']} 更新失败,继续处理其他子模块")
continue
return True return True
@ -178,13 +155,12 @@ class Deployer:
self.logger.error(f"子模块目录不存在: {submodule_path}") self.logger.error(f"子模块目录不存在: {submodule_path}")
return False return False
# 切换到指定分支 # 切换到全局配置的分支
branch = repo_config['branch'] if not self.run_command(f"git checkout {self.global_branch}", cwd=submodule_path):
if not self.run_command(f"git checkout {branch}", cwd=submodule_path):
return False return False
# 拉取最新代码 # 拉取最新代码
if not self.run_command("git pull origin " + branch, cwd=submodule_path): if not self.run_command(f"git pull origin {self.global_branch}", cwd=submodule_path):
return False return False
self.logger.info(f"子模块更新成功: {repo_config['name']}") self.logger.info(f"子模块更新成功: {repo_config['name']}")

View File

@ -31,7 +31,11 @@ class GitMonitor:
self.deployer = Deployer(self.config) self.deployer = Deployer(self.config)
self.last_commits = {} # 存储每个仓库的最后一次提交 hash self.last_commits = {} # 存储每个仓库的最后一次提交 hash
# 读取全局分支配置
self.global_branch = self.config.get('global_branch', 'main')
self.logger.info("Git 监听器初始化完成") self.logger.info("Git 监听器初始化完成")
self.logger.info(f"监听分支: {self.global_branch}")
def _load_config(self): def _load_config(self):
"""加载配置文件""" """加载配置文件"""
@ -91,12 +95,11 @@ class GitMonitor:
"""检查单个仓库是否有新提交""" """检查单个仓库是否有新提交"""
repo_name = repo_config['name'] repo_name = repo_config['name']
repo_url = repo_config['url'] repo_url = repo_config['url']
branch = repo_config['branch']
self.logger.debug(f"检查仓库: {repo_name}") self.logger.debug(f"检查仓库: {repo_name} (分支: {self.global_branch})")
# 获取最新提交 # 获取最新提交(使用全局分支配置)
current_commit = self.get_remote_commit(repo_url, branch) current_commit = self.get_remote_commit(repo_url, self.global_branch)
if not current_commit: if not current_commit:
self.logger.warning(f"无法获取 {repo_name} 的最新提交") self.logger.warning(f"无法获取 {repo_name} 的最新提交")
return False return False

@ -1 +1 @@
Subproject commit 1ea1bbeb8f30d6ca34fcb8bb6cfd9b375714aa64 Subproject commit d735138af013a4771b120712b975d61fafcc1899