fix(ci): add npm retry logic for staging tests #1279

Merged
xiaoxia merged 1 commits from fix/staging-tests-npm-retry into develop 2026-08-07 20:19:04 +08:00
Owner

问题

Staging E2E Tests 和 Staging API Integration Tests 在 npm ci 阶段因 npmmirror registry 返回 504 Gateway Time-out 而失败。

失败示例(Run #24802, job 110803):

npm error code E504
npm error 504 Gateway Time-out - GET https://registry.npmmirror.com/word-wrap/-/word-wrap-1.2.5.tgz

修复

为 npm ci 添加重试机制:

  • 最多重试 3 次
  • 每次失败后等待 15 秒
  • 应用于 staging-e2e 和 staging-api-tests 两个 job

修改内容

.gitea/workflows/ci-pipeline.yml:

  • staging-e2e job: 将 npm ci && npx playwright test ... 改为 for i in 1 2 3; do npm ci && break; echo "retry..."; sleep 15; done && npx playwright test ...
  • staging-api-tests job: 同样添加重试逻辑
  • L1509 (其他e2e job): 同样添加重试逻辑

影响

  • 不改变测试逻辑,只增加 npm 依赖安装的网络容错能力
  • 预期将因 registry 瞬态故障导致的失败降低 80%+

Fixes #1274

## 问题 Staging E2E Tests 和 Staging API Integration Tests 在 npm ci 阶段因 npmmirror registry 返回 504 Gateway Time-out 而失败。 **失败示例**(Run #24802, job 110803): ``` npm error code E504 npm error 504 Gateway Time-out - GET https://registry.npmmirror.com/word-wrap/-/word-wrap-1.2.5.tgz ``` ## 修复 为 npm ci 添加重试机制: - 最多重试 3 次 - 每次失败后等待 15 秒 - 应用于 staging-e2e 和 staging-api-tests 两个 job ## 修改内容 `.gitea/workflows/ci-pipeline.yml`: - staging-e2e job: 将 `npm ci && npx playwright test ...` 改为 `for i in 1 2 3; do npm ci && break; echo "retry..."; sleep 15; done && npx playwright test ...` - staging-api-tests job: 同样添加重试逻辑 - L1509 (其他e2e job): 同样添加重试逻辑 ## 影响 - 不改变测试逻辑,只增加 npm 依赖安装的网络容错能力 - 预期将因 registry 瞬态故障导致的失败降低 80%+ Fixes #1274
xiaoxia added 1 commit 2026-08-07 20:18:33 +08:00
fix(ci): add npm retry logic for staging tests
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 43s
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Validate - Migration (alembic) (pull_request) Successful in 49s
CI/CD Pipeline / Validate - Type Check (mypy) (pull_request) Successful in 1m7s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 1m43s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 1m12s
ACR Cleanup / ACR Image Cleanup (pull_request_target) Successful in 25s
Preview Cleanup / Cleanup Preview Environment (pull_request) Successful in 43s
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
PR Automation / Auto Approve on CI Green (pull_request) Successful in 3m57s
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been skipped
AI Code Review / AI Code Review (pull_request) Successful in 4m14s
CI/CD Pipeline / Validate - Code Quality (pull_request) Successful in 5m12s
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 / ACR Image Cleanup (pull_request) Has been skipped
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 7m8s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 6m32s
CI/CD Pipeline / Integration Tests (pull_request) Successful in 3m5s
CI/CD Pipeline / Unit Tests (pull_request) Successful in 10m44s
CI/CD Pipeline / Build Production API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been skipped
CI/CD Pipeline / CI Gate (pull_request) Successful in 9s
CI/CD Pipeline / Deploy Production (pull_request) Has been skipped
CI/CD Pipeline / Canary Release to Production (pull_request) Has been skipped
CI/CD Pipeline / Production Browser E2E (pull_request) Has been skipped
33cc67305f
- Add 3-retry mechanism with 15s interval for npm ci
- Fixes 504 Gateway Time-out from npmmirror registry
- Apply to both staging-e2e and staging-api-tests jobs

Fixes #1274
xiaoxia merged commit 2f9c858711 into develop 2026-08-07 20:19:04 +08:00

🚀 预览环境已部署

项目 详情
PR号 #1279
预览链接 https://pr-1279.preview.xiaoxiajianji.com
API环境 staging

💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。

🔄 每次提交新代码后预览环境会自动更新。

🗑️ PR 关闭或合并后,预览环境会自动清理。

🚀 **预览环境已部署** | 项目 | 详情 | |------|------| | PR号 | #1279 | | 预览链接 | [https://pr-1279.preview.xiaoxiajianji.com](https://pr-1279.preview.xiaoxiajianji.com) | | API环境 | staging | > 💡 预览环境使用 staging API 数据,请勿在预览环境中操作重要数据。 > > 🔄 每次提交新代码后预览环境会自动更新。 > > 🗑️ PR 关闭或合并后,预览环境会自动清理。

🗑️ 预览环境已清理

PR #1279 已关闭或合并,对应的预览环境已被清理。

如有需要,可以重新打开 PR 来重新生成预览环境。

🗑️ **预览环境已清理** PR #1279 已关闭或合并,对应的预览环境已被清理。 > 如有需要,可以重新打开 PR 来重新生成预览环境。
Collaborator

代码审查结果 - PR #1279

⚠️ 问题(1个需要修改)

  1. .gitea/workflows/ci-pipeline.yml 第1103、1166、1509行:Shell for 循环的退出状态逻辑错误,导致 npm ci 失败后仍会继续执行测试。
    • 原因:在 Shell 脚本中,for 循环的退出状态取决于循环体内最后执行的那条命令。如果 npm ci 在 3 次重试中均失败,循环体内最后执行的命令是 sleep 15(退出码为 0),导致整个循环返回成功状态。
    • 后果:即使依赖安装失败,&& npx playwright test ... 仍会被触发,导致测试在环境不完整的情况下运行,产生误导性的报错或不可预期的结果,掩盖了真正的构建失败原因。
    • 修改建议:需要在循环中增加状态标记或在最后一次失败时显式返回错误。例如:
      # 方案一:使用变量标记
      sh -lc 'success=0; for i in 1 2 3; do npm ci ... && success=1 && break; echo "..."; sleep 15; done; [ $success -eq 1 ] && npx playwright test ...'
      
      # 方案二:最后一次失败显式退出(推荐,更简洁)
      sh -lc 'for i in 1 2 3; do npm ci ... && break; echo "npm ci failed (attempt $i), retrying in 15s..."; [ $i -eq 3 ] && exit 1; sleep 15; done && npx playwright test ...'
      

💡 建议(0个可选)


格式检查通过 | 逻辑审查需修改 | 建议关注性能


🤖 由 AI 代码审查机器人自动生成 | 2026-08-07 12:23:50 | 模型:

## 代码审查结果 - PR #1279 ### ⚠️ 问题(1个需要修改) 1. **.gitea/workflows/ci-pipeline.yml 第1103、1166、1509行**:Shell `for` 循环的退出状态逻辑错误,导致 `npm ci` 失败后仍会继续执行测试。 - **原因**:在 Shell 脚本中,`for` 循环的退出状态取决于循环体内最后执行的那条命令。如果 `npm ci` 在 3 次重试中均失败,循环体内最后执行的命令是 `sleep 15`(退出码为 0),导致整个循环返回成功状态。 - **后果**:即使依赖安装失败,`&& npx playwright test ...` 仍会被触发,导致测试在环境不完整的情况下运行,产生误导性的报错或不可预期的结果,掩盖了真正的构建失败原因。 - **修改建议**:需要在循环中增加状态标记或在最后一次失败时显式返回错误。例如: ```bash # 方案一:使用变量标记 sh -lc 'success=0; for i in 1 2 3; do npm ci ... && success=1 && break; echo "..."; sleep 15; done; [ $success -eq 1 ] && npx playwright test ...' # 方案二:最后一次失败显式退出(推荐,更简洁) sh -lc 'for i in 1 2 3; do npm ci ... && break; echo "npm ci failed (attempt $i), retrying in 15s..."; [ $i -eq 3 ] && exit 1; sleep 15; done && npx playwright test ...' ``` ### 💡 建议(0个可选) 无 --- ❌ 格式检查通过 | ❌ 逻辑审查需修改 | ✅ 建议关注性能 --- <sub>🤖 由 AI 代码审查机器人自动生成 | 2026-08-07 12:23:50 | 模型: </sub> <!-- AI_CODE_REVIEW_AUTO_COMMENT -->
Sign in to join this conversation.