From cb2364a363f60afb7318da147baf54b5b86df94c Mon Sep 17 00:00:00 2001 From: Xiaoxia AI Date: Mon, 15 Jun 2026 16:53:09 +0800 Subject: [PATCH] feat: add CI/CD pipeline with Gitea Actions - .gitea/workflows/tests.yml: automated testing + linting on push/PR - .gitea/workflows/deploy.yml: deployment workflow (placeholder) - docs/CI-CD.md: CI/CD configuration guide - requirements-dev.txt: development dependencies (pytest, black, flake8, mypy) - test job: run pytest with coverage report - lint job: black + flake8 + mypy code quality checks --- .gitea/workflows/deploy.yml | 35 +++++++++ .gitea/workflows/tests.yml | 65 +++++++++++++++++ README.md | 4 +- docs/CI-CD.md | 138 ++++++++++++++++++++++++++++++++++++ requirements-dev.txt | 15 ++++ 5 files changed, 255 insertions(+), 2 deletions(-) create mode 100644 .gitea/workflows/deploy.yml create mode 100644 .gitea/workflows/tests.yml create mode 100644 docs/CI-CD.md create mode 100644 requirements-dev.txt diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml new file mode 100644 index 000000000..9cb7b30ff --- /dev/null +++ b/.gitea/workflows/deploy.yml @@ -0,0 +1,35 @@ +name: Deploy + +on: + push: + branches: [ main ] + tags: + - 'v*' + +jobs: + deploy-staging: + runs-on: ubuntu-latest + if: github.ref == 'refs/heads/main' + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Deploy to staging (placeholder) + run: | + echo "Deploying to staging environment..." + echo "This is a placeholder. Add real deployment steps here." + + deploy-production: + runs-on: ubuntu-latest + if: startsWith(github.ref, 'refs/tags/v') + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Deploy to production (placeholder) + run: | + echo "Deploying to production environment..." + echo "Tag: ${{ github.ref_name }}" + echo "This is a placeholder. Add real deployment steps here." diff --git a/.gitea/workflows/tests.yml b/.gitea/workflows/tests.yml new file mode 100644 index 000000000..6164f415a --- /dev/null +++ b/.gitea/workflows/tests.yml @@ -0,0 +1,65 @@ +name: Tests + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.12' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Run tests + run: | + pytest tests/integration/ -v --cov=packages --cov=apps --cov-report=xml --cov-report=term + + - name: Upload coverage reports + uses: codecov/codecov-action@v3 + if: always() + with: + file: ./coverage.xml + fail_ci_if_error: false + + lint: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.12' + + - name: Install linting tools + run: | + python -m pip install --upgrade pip + pip install black flake8 mypy + + - name: Run Black (check only) + run: | + black --check packages/ apps/ tests/ + + - name: Run Flake8 + run: | + flake8 packages/ apps/ tests/ --max-line-length=120 --extend-ignore=E203,W503 + + - name: Run MyPy + run: | + mypy packages/ apps/ --ignore-missing-imports diff --git a/README.md b/README.md index 92bc906a8..b80403af0 100644 --- a/README.md +++ b/README.md @@ -228,8 +228,8 @@ alembic history ## 下一步计划 -### Phase 2 - 基础设施完善 -- [ ] CI/CD 流水线(GitHub Actions / Gitea Actions) +### Phase 2 - 基础设施完善(进行中) +- [x] CI/CD 流水线(Gitea Actions) - [ ] 真实文件存储(MinIO / S3) - [ ] 生产环境配置(环境变量、密钥管理) - [ ] 监控与日志(Prometheus + Grafana) diff --git a/docs/CI-CD.md b/docs/CI-CD.md new file mode 100644 index 000000000..39f3fdc6f --- /dev/null +++ b/docs/CI-CD.md @@ -0,0 +1,138 @@ +# CI/CD Configuration + +## Gitea Actions + +本项目使用 Gitea Actions 实现 CI/CD 流水线。 + +### 工作流文件 + +**1. `.gitea/workflows/tests.yml` - 自动化测试** + +触发条件: +- 每次 push 到 main 分支 +- 每次创建 Pull Request + +包含任务: +- `test` - 运行集成测试,生成覆盖率报告 +- `lint` - 代码质量检查(Black、Flake8、MyPy) + +**2. `.gitea/workflows/deploy.yml` - 自动化部署** + +触发条件: +- push 到 main 分支 → 部署到 staging 环境 +- 创建 tag (v*) → 部署到 production 环境 + +当前状态:占位,待补充真实部署脚本 + +--- + +## 本地验证 + +在推送前,可以本地运行验证: + +### 运行测试 +```bash +pytest tests/integration/ -v --cov=packages --cov=apps --cov-report=html +``` + +### 代码格式化 +```bash +black packages/ apps/ tests/ +``` + +### 代码检查 +```bash +# Flake8 +flake8 packages/ apps/ tests/ --max-line-length=120 --extend-ignore=E203,W503 + +# MyPy +mypy packages/ apps/ --ignore-missing-imports +``` + +### 排序 imports +```bash +isort packages/ apps/ tests/ +``` + +--- + +## Gitea Actions 配置 + +### 前提条件 + +1. Gitea 服务器需要启用 Actions 功能 +2. 需要配置 Actions Runner + +### 启用步骤 + +**在 Gitea 服务器上**: + +1. 编辑 `/etc/gitea/app.ini`(或对应配置文件): +```ini +[actions] +ENABLED = true +``` + +2. 重启 Gitea: +```bash +systemctl restart gitea +``` + +3. 安装 Actions Runner: +```bash +# 下载 act_runner +wget https://dl.gitea.com/act_runner/latest/act_runner-linux-amd64 +chmod +x act_runner-linux-amd64 +mv act_runner-linux-amd64 /usr/local/bin/act_runner + +# 注册 runner +act_runner register --instance http://your-gitea-server --token YOUR_RUNNER_TOKEN + +# 运行 runner +act_runner daemon +``` + +4. 在仓库设置中启用 Actions + +--- + +## 验证 CI/CD + +推送代码后,在 Gitea 仓库页面查看: +- Actions 标签页 +- 查看工作流运行状态 +- 查看测试结果和覆盖率报告 + +--- + +## 故障排查 + +### Actions 未触发 +- 检查 Gitea 是否启用 Actions +- 检查工作流文件路径是否正确(`.gitea/workflows/`) +- 检查 YAML 语法是否正确 + +### 测试失败 +- 查看 Actions 日志 +- 本地运行 `pytest tests/integration/ -v` +- 检查依赖是否完整 + +### Linting 失败 +- 本地运行 `black --check packages/` +- 本地运行 `flake8 packages/` +- 修复后重新推送 + +--- + +## 未来扩展 + +- [ ] 添加性能测试 +- [ ] 添加安全扫描 +- [ ] 添加 Docker 镜像构建 +- [ ] 添加自动化部署脚本 +- [ ] 添加 Slack/钉钉通知 + +--- + +**最后更新**: 2026-06-15 +**状态**: 配置完成,待 Gitea Actions 启用 diff --git a/requirements-dev.txt b/requirements-dev.txt new file mode 100644 index 000000000..70c14e3e2 --- /dev/null +++ b/requirements-dev.txt @@ -0,0 +1,15 @@ +# requirements-dev.txt - 开发环境依赖 + +# 测试 +pytest==8.0.0 +pytest-cov==4.1.0 +pytest-asyncio==0.23.0 + +# 代码质量 +black==24.1.0 +flake8==7.0.0 +mypy==1.8.0 +isort==5.13.0 + +# 类型检查 +types-requests==2.31.0