From 8f1d935a8c513323c65fa27b7f8d62465fc4eda6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E5=B0=8F=E4=BA=91?= Date: Sat, 10 Jan 2026 13:34:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96DevOps=E9=83=A8=E7=BD=B2?= =?UTF-8?q?=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增update_all_submodules方法,智能更新所有子模块到配置的分支 - 支持git submodule foreach批量更新(当所有子模块使用相同分支时) - 修改commit_submodule_update,推送前先执行git pull --rebase避免冲突 - 删除deploy方法中的自动提交功能,避免并发推送冲突 - 确保所有子模块在分支上而不是detached HEAD状态 --- .devops/deployer.py | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/.devops/deployer.py b/.devops/deployer.py index 078961e..bf40d49 100644 --- a/.devops/deployer.py +++ b/.devops/deployer.py @@ -132,18 +132,37 @@ class Deployer: def update_all_submodules(self, repo_path): """更新所有子模块到各自配置的分支""" - # 遍历所有配置的仓库,更新子模块到对应分支 - for repo_config in self.config['repositories']: - branch = repo_config['branch'] - submodule_path = repo_config['path'] + self.logger.info("更新所有子模块到各自配置的分支...") - self.logger.info(f"更新子模块 {repo_config['name']} 到分支 {branch}") + # 构建分支映射:子模块路径 -> 分支名 + # 注意:这里假设所有子模块使用相同的分支,如果需要不同分支,需要逐个处理 + # 先尝试使用 git submodule foreach 批量更新 - # 构建命令:进入子模块,切换分支并拉取 - 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 + # 检查是否所有子模块使用相同的分支 + 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): + self.logger.warning("批量更新子模块失败") + 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 @@ -342,10 +361,6 @@ class Deployer: if not self.run_deploy_script(repo_config): return False - # 6. 提交子模块更新 - if not self.commit_submodule_update(repo_config): - return False - self.logger.info(f"部署完成: {repo_config['name']}") return True