为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

问题解决。

相关推荐
用户84913717547162 小时前
想做护眼工具却脑子一片空白?我用 OpenSpec 把模糊想法聊成了 v0.1
github·vibecoding
wangruofeng2 小时前
git-filter-repo 把 .git 从 112MB 砍到 1.4MB,但漏推 tag 让 clone 又胖回来
github·devops
峰向AI3 小时前
Block 放出大招!Buzz:一个中继统一代码、聊天、CI 全流程
github
dong_junshuai3 小时前
每天一个开源项目#47 4.4K Stars 的 LikeC4:让架构图随代码进化
github
独立开阀者_FwtCoder6 小时前
最近做了一个健身小程序:智形健身助手,健身的佬们来提点意见
前端·javascript·github
夕夕木各10 小时前
从第一个 PR 到 Vite 官方中文文档维护者
github·vite
AOwhisky10 小时前
云原生 DevOps 工具链从入门到实战(第一期)——DevOps概述与GitLab部署——从理念到工具落地
运维·ci/cd·云原生·gitlab·开发·devops
2301_7736436211 小时前
1.SDLC 与模型(简述)
ci/cd
隔窗听雨眠11 小时前
GitHub Actions自动化运维实战:从零构建一体化CI/CD流水线
运维·自动化·github
lbb 小魔仙1 天前
Git + Python 项目工作流最佳实践:pre-commit、CI、CHANGELOG 自动化
git·python·ci/cd