Commit b4a575a6 authored by Cai Wei's avatar Cai Wei

fix(ci): 修复ci语法错误

parent 0cbd10e1
Pipeline #1304 failed
# GitLab CI配置文件 - DC-TOM项目Cypress测试集成 # GitLab CI配置文件 - DC-TOM项目Cypress测试集成
# 项目: dctomproject # 项目: dctomproject
# 分支: projects/e2e-demo # 分支: projects
# GitLab版本: 12.0.3 兼容配置
stages: stages:
- build - build
...@@ -11,7 +12,6 @@ variables: ...@@ -11,7 +12,6 @@ variables:
NODE_VERSION: "18" NODE_VERSION: "18"
CYPRESS_CACHE_FOLDER: "$CI_PROJECT_DIR/cache/Cypress" CYPRESS_CACHE_FOLDER: "$CI_PROJECT_DIR/cache/Cypress"
npm_config_cache: "$CI_PROJECT_DIR/cache/npm" npm_config_cache: "$CI_PROJECT_DIR/cache/npm"
# 基于项目配置
VITE_PORT: "3000" VITE_PORT: "3000"
CYPRESS_baseUrl: "http://localhost:3000" CYPRESS_baseUrl: "http://localhost:3000"
...@@ -40,17 +40,17 @@ build: ...@@ -40,17 +40,17 @@ build:
- dist/ - dist/
- node_modules/ - node_modules/
expire_in: 2 hours expire_in: 2 hours
rules: only:
- if: $CI_PIPELINE_SOURCE == "web" # 手动触发 - web
- if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "projects" # projects分支触发 - projects
- if: $CI_PIPELINE_SOURCE == "merge_request_event" # MR触发 - merge_requests
- when: manual # 允许手动触发
# 基础测试 - 核心功能验证 # 基础测试 - 核心功能验证
cypress-basic-tests: cypress-basic-tests:
stage: test stage: test
image: cypress/browsers:node-18.16.0-chrome-114.0.5735.133-1-ff-114.0.2-edge-114.0.1823.51-1 image: cypress/browsers:node16.14.2-slim-chrome103-ff102
needs: ["build"] dependencies:
- build
before_script: before_script:
- echo "准备运行Cypress基础测试..." - echo "准备运行Cypress基础测试..."
- npm ci --cache $npm_config_cache --prefer-offline - npm ci --cache $npm_config_cache --prefer-offline
...@@ -85,16 +85,17 @@ cypress-basic-tests: ...@@ -85,16 +85,17 @@ cypress-basic-tests:
reports: reports:
junit: cypress/reports/basic/*.xml junit: cypress/reports/basic/*.xml
expire_in: 1 week expire_in: 1 week
rules: only:
- if: $CI_PIPELINE_SOURCE == "web" - web
- if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "projects" - projects
- if: $CI_PIPELINE_SOURCE == "merge_request_event" - merge_requests
# 完整测试套件 - 手动触发 # 完整测试套件 - 手动触发
cypress-full-tests: cypress-full-tests:
stage: test stage: test
image: cypress/browsers:node-18.16.0-chrome-114.0.5735.133-1-ff-114.0.2-edge-114.0.1823.51-1 image: cypress/browsers:node16.14.2-slim-chrome103-ff102
needs: ["build"] dependencies:
- build
before_script: before_script:
- echo "准备运行Cypress完整测试套件..." - echo "准备运行Cypress完整测试套件..."
- npm ci --cache $npm_config_cache --prefer-offline - npm ci --cache $npm_config_cache --prefer-offline
...@@ -124,15 +125,16 @@ cypress-full-tests: ...@@ -124,15 +125,16 @@ cypress-full-tests:
reports: reports:
junit: cypress/reports/full/*.xml junit: cypress/reports/full/*.xml
expire_in: 1 week expire_in: 1 week
rules: when: manual
- if: $CI_PIPELINE_SOURCE == "web" && $FULL_TEST_SUITE == "true" only:
- when: manual - web
# 并行测试组1 - 业务功能模块 # 并行测试组1 - 业务功能模块
cypress-business-tests: cypress-business-tests:
stage: test stage: test
image: cypress/browsers:node-18.16.0-chrome-114.0.5735.133-1-ff-114.0.2-edge-114.0.1823.51-1 image: cypress/browsers:node16.14.2-slim-chrome103-ff102
needs: ["build"] dependencies:
- build
before_script: before_script:
- echo "准备运行业务功能测试..." - echo "准备运行业务功能测试..."
- npm ci --cache $npm_config_cache --prefer-offline - npm ci --cache $npm_config_cache --prefer-offline
...@@ -158,15 +160,16 @@ cypress-business-tests: ...@@ -158,15 +160,16 @@ cypress-business-tests:
reports: reports:
junit: cypress/reports/business/*.xml junit: cypress/reports/business/*.xml
expire_in: 1 week expire_in: 1 week
rules: when: manual
- if: $CI_PIPELINE_SOURCE == "web" && $PARALLEL_TESTS == "true" only:
- when: manual - web
# 并行测试组2 - 数据管理模块 # 并行测试组2 - 数据管理模块
cypress-data-tests: cypress-data-tests:
stage: test stage: test
image: cypress/browsers:node-18.16.0-chrome-114.0.5735.133-1-ff-114.0.2-edge-114.0.1823.51-1 image: cypress/browsers:node16.14.2-slim-chrome103-ff102
needs: ["build"] dependencies:
- build
before_script: before_script:
- echo "准备运行数据管理测试..." - echo "准备运行数据管理测试..."
- npm ci --cache $npm_config_cache --prefer-offline - npm ci --cache $npm_config_cache --prefer-offline
...@@ -192,26 +195,19 @@ cypress-data-tests: ...@@ -192,26 +195,19 @@ cypress-data-tests:
reports: reports:
junit: cypress/reports/data/*.xml junit: cypress/reports/data/*.xml
expire_in: 1 week expire_in: 1 week
rules: when: manual
- if: $CI_PIPELINE_SOURCE == "web" && $PARALLEL_TESTS == "true" only:
- when: manual - web
# 测试报告汇总 # 测试报告汇总
generate-test-reports: generate-test-reports:
stage: reports stage: reports
image: node:18-alpine image: node:16-alpine
needs: dependencies:
- job: cypress-basic-tests - cypress-basic-tests
artifacts: true - cypress-full-tests
- job: cypress-full-tests - cypress-business-tests
artifacts: true - cypress-data-tests
optional: true
- job: cypress-business-tests
artifacts: true
optional: true
- job: cypress-data-tests
artifacts: true
optional: true
before_script: before_script:
- echo "准备生成测试报告..." - echo "准备生成测试报告..."
- npm install -g mochawesome-merge mochawesome-report-generator - npm install -g mochawesome-merge mochawesome-report-generator
...@@ -291,15 +287,16 @@ generate-test-reports: ...@@ -291,15 +287,16 @@ generate-test-reports:
paths: paths:
- public/ - public/
expire_in: 1 month expire_in: 1 month
rules: only:
- if: $CI_PIPELINE_SOURCE == "web" - web
- if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "projects" - projects
- if: $CI_PIPELINE_SOURCE == "merge_request_event" - merge_requests
# Pages部署 - 展示测试报告 # Pages部署 - 展示测试报告
pages: pages:
stage: reports stage: reports
needs: ["generate-test-reports"] dependencies:
- generate-test-reports
script: script:
- echo "部署测试报告到GitLab Pages..." - echo "部署测试报告到GitLab Pages..."
- echo "访问地址: https://liuzhaohui.gitlab.io/dctomproject/" - echo "访问地址: https://liuzhaohui.gitlab.io/dctomproject/"
...@@ -307,6 +304,6 @@ pages: ...@@ -307,6 +304,6 @@ pages:
paths: paths:
- public/ - public/
expire_in: 1 month expire_in: 1 month
rules: only:
- if: $CI_PIPELINE_SOURCE == "web" - web
- if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "projects" - projects
\ No newline at end of file \ No newline at end of file
# GitLab 12.0.3 兼容性配置说明
## 版本信息
- **GitLab版本**: 12.0.3 (2019年发布)
- **项目**: DC-TOM dctomproject
- **分支**: projects
## 兼容性调整
### 1. 语法替换
#### 使用 `only` 替代 `rules`
GitLab 12.0.3 不支持现代的 `rules` 语法,需要使用传统的 `only/except` 语法:
```yaml
# ✅ GitLab 12.0.3 兼容语法
only:
- web # 手动触发
- projects # 分支触发
- merge_requests # MR触发
# ❌ 不兼容语法 (GitLab 12.8+)
rules:
- if: $CI_PIPELINE_SOURCE == "web"
```
#### 使用 `dependencies` 替代 `needs`
`needs` 关键字在 GitLab 12.3 之后才引入:
```yaml
# ✅ GitLab 12.0.3 兼容语法
dependencies:
- build
# ❌ 不兼容语法 (GitLab 12.3+)
needs: ["build"]
```
### 2. Docker镜像优化
使用更稳定且兼容的Docker镜像版本:
```yaml
# ✅ 稳定兼容的镜像
image: cypress/browsers:node16.14.2-slim-chrome103-ff102
image: node:16-alpine
# 原配置使用更新的镜像可能有兼容性问题
```
### 3. 功能配置
#### 触发方式
- **自动触发**: 推送到 `projects` 分支、创建MR
- **手动触发**: 通过Web界面手动运行pipeline
#### 测试策略保持不变
- ✅ 基础测试 (login, dashboard, navigation)
- ✅ 完整测试 (所有测试用例) - 手动触发
- ✅ 并行测试 (业务功能组 + 数据管理组) - 手动触发
#### 报告生成
- ✅ 使用 mochawesome 生成HTML报告
- ✅ 自动部署到GitLab Pages
- ✅ 保存截图和视频用于调试
## 使用方法
### 1. 提交代码
```bash
git add .gitlab-ci.yml
git commit -m "feat: GitLab 12.0.3兼容的CI配置"
git push origin projects
```
### 2. 手动触发测试
1. **访问GitLab项目页面**:
```
https://app.bmetech.com/liuzhaohui/dctomproject
```
2. **进入CI/CD → Pipelines**
3. **点击"Run Pipeline"**
4. **选择运行的测试**:
- 默认运行:build + cypress-basic-tests
- 手动运行:cypress-full-tests (完整测试)
- 手动运行:cypress-business-tests (业务测试)
- 手动运行:cypress-data-tests (数据测试)
### 3. 查看结果
- **Pipeline状态**: 在Pipelines页面查看
- **测试报告**: https://liuzhaohui.gitlab.io/dctomproject/
- **下载Artifacts**: 包含截图、视频、详细报告
## GitLab 12.0.3 特殊注意事项
### 限制和特性
1. **需要手动管理依赖关系**
- `dependencies` 字段需要明确列出前置任务
- 不支持并行依赖的自动解析
2. **Pipeline可视化有限**
- 依赖关系图显示相对简单
- 需要通过任务执行时间推断并行度
3. **缓存功能相对基础**
- 支持基本的文件缓存
- 缓存键的表达式功能有限
4. **错误处理相对简单**
- 重试机制需要手动实现
- 错误处理逻辑相对基础
### 推荐实践
1. **简化Pipeline结构**
- 避免过于复杂的依赖关系
- 使用清晰的stage分层
2. **充分测试配置**
- 在推送前验证YAML语法
- 使用GitLab CI Lint工具
3. **监控Pipeline执行**
- 定期检查pipeline执行状态
- 及时处理失败的任务
## 升级路径
如果后续升级GitLab版本,可以考虑以下优化:
### GitLab 12.3+
- 使用 `needs` 替代 `dependencies`
- 支持更精细的DAG (有向无环图) 控制
### GitLab 12.8+
- 使用 `rules` 替代 `only/except`
- 支持更复杂的条件逻辑
### GitLab 13.0+
- 支持更多的pipeline特性
- 更好的可视化和监控功能
---
## 总结
当前配置完全兼容GitLab 12.0.3,提供了:
-**稳定的CI/CD流程**
-**完整的测试覆盖**
-**详细的测试报告**
-**灵活的手动触发选项**
-**良好的错误调试支持**
可以安全地提交和使用!
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment