为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

问题解决。

相关推荐
Yunzenn2 小时前
零基础复现Claude Code(五):终端篇——赋予执行命令的超能力
面试·github
Yunzenn2 小时前
零基础复现Claude Code(四):双手篇——赋予读写文件的能力
开源·github
CoderJia程序员甲2 小时前
GitHub 热榜项目 - 日榜(2026-04-23)
人工智能·ai·大模型·github·ai教程
叹一曲当时只道是寻常2 小时前
Reference 工具安装与使用教程:一条命令管理 Git 仓库引用与知识沉淀
人工智能·git·ai·开源·github
不会编程的-程序猿2 小时前
PyCharm 直接把本地项目上传到 GitHub
ide·pycharm·github
@土豆4 小时前
Jenkins CI_CD流水线案例
运维·ci/cd·jenkins
怣疯knight5 小时前
Github 2FA(Two-Factor Authentication/两因素认证)
github
MilesShi5 小时前
从零到工程化:用 GitHub Actions 高效集成 Playwright 测试
自动化·github
Cyber4K5 小时前
【DevOps专项】GitLab 与 Jenkins 介绍及部署持续集成环境
运维·ci/cd·gitlab·jenkins·devops