MBE 本地 CI/CD 指南
概述
本地 CI/CD 流程:代码提交 → 自动测试 → 自动部署
┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ 开发者 │ --> │ Git 推送 │ --> │ 自动测试 │ --> │ 自动部署 │
│ 修改代码 │ │ (Gitea) │ │ (pytest) │ │ (Docker) │
└─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘
│ │ │
▼ ▼ ▼
http://localhost:3000 测试报告 mbe-dev.hi-maker.com
快速开始
1. 启动 Gitea(本地 Git 服务器)
# 启动 Gitea
docker-compose -f docker-compose.gitea.yml up -d
# 访问 http://localhost:3000 完成初始化
# 首次访问会进入安装向导
2. 初始化 Git 仓库
# 在项目目录初始化 Git
cd D:\Mises\mises-behavior-engine
git init
git add .
git commit -m "Initial commit"
# 添加 Gitea 远程仓库(首先在 Gitea 创建仓库)
git remote add origin http://localhost:3000/<用户名>/mises-behavior-engine.git
git push -u origin main
3. 配置 Git Hook(自动部署)
在 Gitea 仓库设置中配置 Webhook:
- URL:
http://localhost:8001/webhook/deploy - 或使用本地 Git hook
本地 Git hook 方式:
# 创建 post-receive hook
# .git/hooks/post-receive
#!/bin/bash
cd /path/to/mises-behavior-engine
python scripts/auto_deploy.py
4. 运行测试
# 安装测试依赖
pip install pytest pytest-asyncio httpx
# 运行所有测试
pytest tests/ -v
# 运行特定测试
pytest tests/test_api.py::TestChatAPI -v
# 生成测试报告
pytest tests/ -v --html=reports/test_report.html
自动部署流程
手动触发部署
# 执行自动部署脚本
python scripts/auto_deploy.py
部署流程说明
- 拉取代码 -
git pull origin main - 运行测试 -
pytest tests/ -v - 重启服务 -
docker-compose restart mbe-api-dev - 健康检查 - 验证服务可访问
部署日志
# 查看部署日志
Get-Content logs/deploy.log -Tail 50
# 实时跟踪
Get-Content logs/deploy.log -Wait
测试用例
已包含的测试
| 测试类 | 说明 |
|---|---|
| TestHealthCheck | 健康检查、根路径 |
| TestChatAPI | 对话接口测试 |
| TestExpertAPI | 专家匹配测试 |
| TestKnowledgeAPI | 知识库搜索测试 |
| TestMCPAPI | MCP 工具测试 |
| TestQT2API | 量化交易接口测试 |
| TestAsyncAPI | 并发请求测试 |
添加新测试
# tests/test_new_feature.py
import pytest
import httpx
API_BASE = "http://localhost:8001"
class TestNewFeature:
def test_example(self):
response = httpx.get(f"{API_BASE}/api/v1/new-endpoint")
assert response.status_code == 200
Gitea 配置
首次安装
| 配置项 | 推荐值 |
|---|---|
| 数据库类型 | SQLite3 |
| 站点标题 | MBE Git |
| 管理员用户名 | admin |
| 管理员邮箱 | admin@localhost |
创建仓库
- 登录 Gitea
- 点击 + → 新建仓库
- 仓库名称:
mises-behavior-engine - 可见性:私有
配置 Webhook
- 进入仓库 → 设置 → Web 钩子
- 点击 添加 Web 钩子 → Gitea
- 配置:
- 目标 URL:
http://mbe-api-dev:8000/webhook/deploy - HTTP 方法: POST
- Content Type: application/json
- 触发事件: 推送
- 激活: ✅ 勾选
- 目标 URL:
- 点击 添加 Web 钩子
注意:Gitea 和 mbe-api-dev 在同一个 Docker 网络中,使用容器名和内部端口 8000
开发工作流
日常开发
# 1. 拉取最新代码
git pull origin main
# 2. 创建功能分支
git checkout -b feature/new-feature
# 3. 开发和本地测试
# ... 修改代码 ...
pytest tests/ -v
# 4. 提交代码
git add .
git commit -m "feat: 添加新功能"
# 5. 推送到远程(触发自动部署)
git push origin feature/new-feature
# 6. 创建合并请求(在 Gitea 中)
分支策略
| 分支 | 用途 | 部署环境 |
|---|---|---|
| main | 稳定版本 | 生产环境 |
| develop | 开发版本 | 开发环境 |
| feature/* | 功能开发 | 本地 |
| hotfix/* | 紧急修复 | 生产环境 |
常见问题
Q: Gitea 无法启动
# 检查端口占用
netstat -ano | findstr ":3000"
# 查看日志
docker logs mbe-gitea
Q: 测试失败
# 确保开发环境运行中
docker ps | findstr mbe-api-dev
# 检查 API 是否可访问
curl http://localhost:8001/
Q: 自动部署失败
# 查看部署日志
Get-Content logs/deploy.log -Tail 100
# 手动重启服务
docker-compose -f docker-compose.dev.yml restart mbe-api-dev
扩展:GitHub Actions(可选)
如果将来迁移到 GitHub,可以使用以下配置:
# .github/workflows/deploy.yml
name: Deploy to Dev
on:
push:
branches: [develop]
jobs:
test-and-deploy:
runs-on: self-hosted
steps:
- uses: actions/checkout@v3
- name: Run tests
run: pytest tests/ -v
- name: Deploy
run: docker-compose -f docker-compose.dev.yml restart mbe-api-dev