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