一个最简 Flask 示例应用,用于演示完整的 AI-CICD 流水线:
① GitHub 代码提交
↓ webhook
② Jenkins 触发
↓
③ SonarQube 代码扫描
↓
④ AI 测试平台 Pytest 白盒测试(ai_playwright_backend 容器执行)
↓ (通过)
⑤ Jenkins 构建 Docker 镜像
↓
⑥ 部署到开发测试环境
| 文件 | 说明 |
|---|---|
app.py |
Flask 主应用 |
requirements.txt |
Python 依赖 |
templates/index.html |
首页模板 |
Dockerfile |
应用镜像构建 |
Jenkinsfile |
CI/CD 流水线 |
sonar-project.properties |
SonarQube 扫描配置 |
- Jenkins 已启动(
cicd-jenkins容器,端口 8888)。 - SonarQube 已启动(
cicd-sonarqube容器,端口 9090)。 - AI 质量平台 已启动(
ai_playwright_backend等容器)。 - Jenkins 容器已挂载
/var/run/docker.sock,可执行 Docker 命令。 - 所有服务在同一个 Docker 网络
ai_network中。
初始化脚本 01jenkins/init.groovy.d/ai-setup.groovy 已自动创建:
- ID:
ai-platform-credentials - 值:
{"username":"admin","password":"Admin@123456"}
如果缺失,请在 Jenkins 中手动添加一个 Secret text 类型凭证,ID 为 ai-platform-credentials,值为上述 JSON。
流水线会自动读取 ID 为 openai-api-key 的 Secret text 凭证,并通过 /api/config/save/openai 接口配置到 AI 测试平台。
如果 AI 测试平台已经在系统设置中配置好 OpenAI API Key,则此凭证可以省略。
注意:当前
ai_playwright_backend容器通过系统设置(数据库)读取 OpenAI 配置,若未配置,白盒测试将失败。
- 在 Jenkins 中创建一个新的 Pipeline 类型 Job,例如
helloworld-cicd-demo。 - 在 Pipeline 配置中选择 Pipeline script from SCM。
- SCM 选择 Git,填写 GitHub 仓库地址(如
https://github.com/<your-org>/helloworld.git)。 - 指定分支(如
*/main)。 - Script Path 保持默认
Jenkinsfile。 - 保存。
- 在 GitHub 仓库中进入 Settings → Webhooks → Add webhook。
- Payload URL 填写:
如果是本地测试,可使用 ngrok 等工具暴露 Jenkins 地址。
http://<你的公网IP或内网穿透地址>:8888/github-webhook/ - Content type 选择
application/json。 - 选择 Just the push event。
- 保存。
如果没有公网地址,也可以在 Jenkins Job 中启用 Poll SCM(如
H/2 * * * *)或手动点击 Build Now 测试。
在 Jenkins 中进入 helloworld-cicd-demo Job,点击 Build Now。
流水线将依次执行:
- 从 GitHub 检出代码
- SonarQube 扫描
- AI 测试平台 Pytest 白盒测试
- 构建 Docker 镜像
helloworld:<BUILD_NUMBER> - 部署容器
helloworld-dev,访问地址:http://host.docker.internal:8080
- 确认
cicd-sonarqube容器健康运行。 - 确认
sonar-project.properties中的sonar.token有效。 - 流水线中通过环境变量
SONAR_HOST_URL覆盖为http://cicd-sonarqube:9000。
- 在 Jenkins 中添加
openai-api-key凭证,流水线会自动配置。 - 或在 AI 测试平台前端(
http://host.docker.internal:3002)进入系统设置,手动填写 OpenAI API Key、Base URL 和 Model。
Jenkins 容器内通过挂载的 docker.sock 调用宿主机 Docker Daemon,直接使用 docker build . 会找不到容器内路径。本流水线使用以下方式解决:
- 复制代码到
ai_playwright_backend:tar -cf - . | docker exec -i ... tar -xf - - 构建镜像:
tar -cf - . | docker build -t ... - - SonarQube 扫描:
docker run --volumes-from cicd-jenkins ...
用户描述中提到的 "3002 容器" 是指 AI 测试平台前端(ai_playwright_frontend,端口 3002)。
实际的 Pytest 白盒测试执行发生在 后端容器 ai_playwright_backend(端口 5005),流水线通过调用其 REST API 触发。