为aicomm项目添加CI/CD 配置 让github帮我们自动测试

CI/CD 配置是什么

CI/CD 流水线 是自动化软件开发流程的工具:

CI (Continuous Integration) - 持续集成

每次代码提交后自动运行测试,确保新代码不会破坏现有功能。

CD (Continuous Deployment) - 持续部署

测试通过后自动将代码部署到服务器。

实际例子:

复制代码
开发者提交代码 → GitHub 自动运行测试 → 测试通过 → 自动部署到服务器

对 kotti_ai_community 项目的好处:

  1. 自动测试 - 每次提交代码,自动运行 67 个测试
  2. 防止回归 - 如果修改破坏了功能,立即发现
  3. 代码质量 - 可以加入代码风格检查
  4. 团队协作 - 多人开发时自动验证

GitHub Actions 配置示例:

复制代码
# .github/workflows/test.yml
name: Tests
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: '3.12'
      - run: pip install -e .
      - run: python -m unittest discover -s kotti_ai_community/tests

配置后,每次 git push 都会自动运行测试,结果会显示在 GitHub 页面上(绿色 ✓ 或红色 ✗)。

配置

CI/CD 配置已完成!

创建的文件:

复制代码
G:\dumate\aicomm\
├── .github/
│   └── workflows/
│       ├── test.yml    # 测试工作流
│       └── lint.yml    # 代码检查工作流
└── pyproject.toml      # 工具配置

test.yml - 测试工作流:

  • 支持 Python 3.10、3.11、3.12 多版本测试
  • 支持 SQLAlchemy 1.4 和 2.0 兼容性测试
  • 自动运行 67 个单元测试
  • 生成测试覆盖率报告并上传到 Codecov

lint.yml - 代码检查工作流:

  • Black - 代码格式检查
  • isort - import 排序检查
  • flake8 - 语法和风格检查
  • Bandit - 安全漏洞扫描

使用方式:

  1. 将代码推送到 GitHub:

    git add .
    git commit -m "Add CI/CD configuration"
    git push origin main

  2. GitHub 会自动运行测试和检查,结果会显示在仓库的 Actions 页面

  3. README 中添加了 CI 徽章(需要替换 YOUR_USERNAME 为你的 GitHub 用户名)

触发条件:

  • Push 到 mainmasterdevelop 分支
  • Pull Request 到这些分支

跑通

经过多次调试后,跑通

前面8次尝试,主要的问题是:

setuptools版本没有固定为80.10.2

代码没有flake8规范

需要依赖安装Kotti新版本,也就是kotti-py312,在requirements文件里设置通过github安装

调试

提交到github后,有报错

估计是flake8方面有问题。

解决flake8问题

CI/CD flake8 问题已修复!

修复内容:

  1. 创建 .flake8 配置文件 - 忽略迁移文件、测试文件,设置行长度为 100

  2. 修复代码问题:

    • 移除未使用的 import(Response, view_defaults, datetime, HTTPNotFound 等)
    • 修复 SQLAlchemy 比较语法:== True.is_(True), == False.is_(False), != None.is_not(None)
    • 移除未使用的变量
    • 添加缺失的 import(view_defaults, validate_csrf_token, truncate_string, HTTPForbidden
  3. 更新的文件:

    • kotti_ai_community/__init__.py
    • kotti_ai_community/resources.py
    • kotti_ai_community/moderation.py
    • kotti_ai_community/user_profile.py
    • kotti_ai_community/notification.py
    • kotti_ai_community/views/*.py(所有视图文件)

验证结果:

  • flake8 检查通过(0 错误)
  • 67 个测试全部通过

现在可以重新提交到 GitHub,CI 应该能通过了。

测试时安装依赖有问题

5s

Run python -m pip install --upgrade pip

Requirement already satisfied: pip in /opt/hostedtoolcache/Python/3.12.13/x64/lib/python3.12/site-packages (26.0.1)

Obtaining file:///home/runner/work/aicomm/aicomm

Installing build dependencies: started

Installing build dependencies: finished with status 'done'

Checking if build backend supports build_editable: started

Checking if build backend supports build_editable: finished with status 'done'

Getting requirements to build editable: started

Getting requirements to build editable: finished with status 'done'

Preparing editable metadata (pyproject.toml): started

Preparing editable metadata (pyproject.toml): finished with status 'done'

Collecting Kotti<3.0.0,>=2.0.0 (from kotti_ai_community==0.1.0)

Downloading Kotti-2.0.9.tar.gz (748 kB)

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 748.9/748.9 kB 62.3 MB/s 0:00:00

Installing build dependencies: started

Installing build dependencies: finished with status 'done'

Getting requirements to build wheel: started

Getting requirements to build wheel: finished with status 'done'

Installing backend dependencies: started

Installing backend dependencies: finished with status 'done'

Preparing metadata (pyproject.toml): started

Preparing metadata (pyproject.toml): finished with status 'done'

INFO: pip is looking at multiple versions of kotti-ai-community to determine which version is compatible with other requirements. This could take a while.

ERROR: Could not find a version that satisfies the requirement kotti_g4f<1.0.0,>=0.1.0 (from kotti-ai-community) (from versions: none)

ERROR: No matching distribution found for kotti_g4f<1.0.0,>=0.1.0

Error: Process completed with exit code 1.

解决问题:从 GitHub 子目录安装依赖

  1. 创建 requirements.txt - 从 GitHub 子目录安装依赖:

    复制代码
    git+https://github.com/skywalk163/kotti_py312.git#subdirectory=Kotti
    git+https://github.com/skywalk163/kotti_py312.git#subdirectory=kotti_g4f
    git+https://github.com/skywalk163/kotti_py312.git#subdirectory=kotti_image
    git+https://github.com/skywalk163/kotti_py312.git#subdirectory=kotti_tinymce
  2. 更新 setup.py - 简化依赖声明,添加注释说明

  3. 更新 .github/workflows/test.yml - CI 使用 pip install -r requirements.txt

安装方式:

复制代码
# 本地安装
pip install -r requirements.txt
pip install -e .

# 或单独安装每个依赖
pip install "git+https://github.com/skywalk163/kotti_py312.git#subdirectory=Kotti"
pip install "git+https://github.com/skywalk163/kotti_py312.git#subdirectory=kotti_g4f"

报错ModuleNotFoundError: No module named 'pkg_resources'

这个问题以前碰到过:https://skywalk.blog.csdn.net/article/details/160124144

所以解决方法就是设置setuptools的版本号,在Kotti和aicomm两个项目的requirements文件里,都写上setuptools==80.10.2

问题解决。

相关推荐
哆哆啦0011 小时前
使用 Obsidian + GitHub Actions + GitHub Pages 搭建内容发布流
数据库·笔记·github·obsidian
厚皮龙13 小时前
使用 SSH 密钥上传 GitHub 仓库流程
运维·ssh·github
2601_9557819815 小时前
OpenClaw快速接入Ollama本地模型教程
github·open claw安装·open claw部署
Dvesiz15 小时前
【ClaudeCode平替(免费)】OpenCode 完整安装与 VSCode 使用指南
ide·vscode·编辑器·github·ai编程·claude·visual studio code
pzx_00117 小时前
【论文阅读】SWE-CI: Evaluating Agent Capabilities in Maintaining Codebases via Continuous Integration
论文阅读·人工智能·深度学习·神经网络·ci/cd
Hommy8820 小时前
【剪映小助手】音频信息生成接口
开源·github·剪映小助手
Kakaa21 小时前
AI 帮我写了一万行代码,但我已经看不懂自己的项目了
github
Hy行者勇哥21 小时前
Coze技能保存至GitHub:文件格式与完整存档教程
github
weelinking1 天前
2026年三大主流大模型深度对比:GPT-5.5、Claude 4.6与DeepSeek V4谁更值得选择?
java·大数据·人工智能·git·python·gpt·github
梦梦代码精1 天前
开源智能体平台 BuildingAI 深度解析:Monorepo 架构、MCP 集成及 GPT-Image-2 接入实测
前端·人工智能·后端·gpt·开源·github