优化DevOps部署脚本

- 新增update_all_submodules方法,智能更新所有子模块到配置的分支
- 支持git submodule foreach批量更新(当所有子模块使用相同分支时)
- 修改commit_submodule_update,推送前先执行git pull --rebase避免冲突
- 删除deploy方法中的自动提交功能,避免并发推送冲突
- 确保所有子模块在分支上而不是detached HEAD状态
This commit is contained in:
孙小云 2026-01-10 13:34:50 +08:00
parent 4962ae4b2f
commit 8f1d935a8c
1 changed files with 29 additions and 14 deletions

View File

@ -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