feat(ci): Staging部署从Watchtower模式改为SSH push模式 #337

Merged
xiaoxia merged 1 commits from feat/staging-ssh-deploy into develop 2026-07-15 01:21:46 +08:00
Owner

背景

Staging 当前使用 Watchtower pull 模式部署,存在以下问题:

  1. 部署时机不可控,需等 Watchtower 检测周期(~90s)
  2. 版本号可能不匹配(旧版本healthy会误判)
  3. 回滚机制笨重(重打tag + 等Watchtower)
  4. 与 Production 部署模式不一致,维护成本高

改造内容

1. 新增 scripts/ci_staging_deploy.sh

Staging SSH 部署脚本,在 staging 服务器上执行:

  • Registry pull 新版本镜像
  • 备份 legacy assets
  • 检查基础设施容器(postgres/redis)
  • 运行数据库 migration
  • docker rm -f 旧容器 + docker run 启动新容器
  • 等待 API 和 Web 健康
  • 清理旧镜像

2. 改造 scripts/ci_staging_healthcheck.sh

从 Watchtower 模式改为 SSH 模式:

  • 移除 Watchtower 等待逻辑
  • 回滚方式改为 SSH 执行旧版本部署脚本
  • 增加 STAGING_SSH_* 环境变量配置

3. 修改 ci-cd.yml deploy-staging job

  • 替换 "Tag and push :staging images" 步骤为 SSH 部署
  • 健康检查步骤增加 SSH 相关 secrets
  • Job 名称去掉 "(Watchtower auto-deploy)" 后缀

验证方式

合并后观察 develop 分支的 Deploy Staging job:

  1. SSH 连接成功
  2. 镜像 pull + 部署成功
  3. 健康检查通过
  4. staging 实际版本更新到最新 commit

风险与回滚

  • 风险:SSH 配置问题导致部署失败
  • 回滚:如遇问题,可 revert 本 PR 恢复 Watchtower 模式
  • 注意:合并前需先配置 STAGING_SSH_* secrets
## 背景 Staging 当前使用 Watchtower pull 模式部署,存在以下问题: 1. 部署时机不可控,需等 Watchtower 检测周期(~90s) 2. 版本号可能不匹配(旧版本healthy会误判) 3. 回滚机制笨重(重打tag + 等Watchtower) 4. 与 Production 部署模式不一致,维护成本高 ## 改造内容 ### 1. 新增 `scripts/ci_staging_deploy.sh` Staging SSH 部署脚本,在 staging 服务器上执行: - Registry pull 新版本镜像 - 备份 legacy assets - 检查基础设施容器(postgres/redis) - 运行数据库 migration - docker rm -f 旧容器 + docker run 启动新容器 - 等待 API 和 Web 健康 - 清理旧镜像 ### 2. 改造 `scripts/ci_staging_healthcheck.sh` 从 Watchtower 模式改为 SSH 模式: - 移除 Watchtower 等待逻辑 - 回滚方式改为 SSH 执行旧版本部署脚本 - 增加 STAGING_SSH_* 环境变量配置 ### 3. 修改 `ci-cd.yml` deploy-staging job - 替换 "Tag and push :staging images" 步骤为 SSH 部署 - 健康检查步骤增加 SSH 相关 secrets - Job 名称去掉 "(Watchtower auto-deploy)" 后缀 ## 验证方式 合并后观察 develop 分支的 Deploy Staging job: 1. SSH 连接成功 2. 镜像 pull + 部署成功 3. 健康检查通过 4. staging 实际版本更新到最新 commit ## 风险与回滚 - 风险:SSH 配置问题导致部署失败 - 回滚:如遇问题,可 revert 本 PR 恢复 Watchtower 模式 - 注意:合并前需先配置 STAGING_SSH_* secrets
xiaoxia added 1 commit 2026-07-15 00:52:34 +08:00
feat(ci): Staging部署从Watchtower改为SSH push模式
CI/CD Pipeline / Validate Code Quality And Tests (pull_request) Successful in 2m41s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 2m44s
CI/CD Pipeline / Frontend Lint (pull_request) Successful in 2m52s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been skipped
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
CI/CD Pipeline / Integration Tests (pull_request) Successful in 1m21s
f2cea1831e
xiaoxia reviewed 2026-07-15 01:16:24 +08:00
xiaoxia left a comment
Author
Owner

Review 通过

整体架构:

  • 从 Watchtower pull 模式改为 SSH push 模式,与 production 对齐,正确
  • CI 构建镜像 → SSH 到 staging 服务器 → pull 镜像 → 部署,链路清晰

ci-cd.yml 变更:

  • 移除 :staging tag 推送步骤,改为 SSH 部署
  • SSH key 多层 fallback(builder key / home key / secret),健壮性好
  • 健康检查步骤传入 SSH 环境变量,支持回滚

ci_staging_deploy.sh:

  • 镜像 pull + tag + 容器启动流程完整
  • legacy assets 备份与清理逻辑保留
  • 数据库迁移、健康检查等待都有覆盖
  • 与 production 部署脚本结构对齐

ci_staging_healthcheck.sh:

  • save_rollback_target 改为 SSH 读取当前容器版本,比之前 digest 方案更直接
  • do_rollback 改为 SSH 执行完整回滚脚本(pull 旧镜像 + 重启容器),可靠性更高
  • 回滚不跑 migration,避免数据问题,合理

CI 全绿,可以合并。合并后建议验证一次 develop 上的 Deploy Staging 完整跑通。

Review 通过 ✅ **整体架构:** - 从 Watchtower pull 模式改为 SSH push 模式,与 production 对齐,正确 - CI 构建镜像 → SSH 到 staging 服务器 → pull 镜像 → 部署,链路清晰 **ci-cd.yml 变更:** - 移除 :staging tag 推送步骤,改为 SSH 部署 - SSH key 多层 fallback(builder key / home key / secret),健壮性好 - 健康检查步骤传入 SSH 环境变量,支持回滚 **ci_staging_deploy.sh:** - 镜像 pull + tag + 容器启动流程完整 - legacy assets 备份与清理逻辑保留 - 数据库迁移、健康检查等待都有覆盖 - 与 production 部署脚本结构对齐 **ci_staging_healthcheck.sh:** - save_rollback_target 改为 SSH 读取当前容器版本,比之前 digest 方案更直接 - do_rollback 改为 SSH 执行完整回滚脚本(pull 旧镜像 + 重启容器),可靠性更高 - 回滚不跑 migration,避免数据问题,合理 CI 全绿,可以合并。合并后建议验证一次 develop 上的 Deploy Staging 完整跑通。
xiaoxia merged commit 7d3328f462 into develop 2026-07-15 01:21:46 +08:00
Sign in to join this conversation.