307 lines
8.3 KiB
YAML
307 lines
8.3 KiB
YAML
name: Flask 提示词大师 - CI/CD 流水线
|
||
|
||
on:
|
||
push:
|
||
branches: [ main, develop ]
|
||
pull_request:
|
||
branches: [ main ]
|
||
|
||
env:
|
||
PYTHON_VERSION: '3.12'
|
||
FLASK_ENV: testing
|
||
|
||
jobs:
|
||
# 代码质量检查
|
||
code-quality:
|
||
name: 代码质量检查
|
||
runs-on: ubuntu-latest
|
||
|
||
steps:
|
||
- name: 检出代码
|
||
uses: actions/checkout@v4
|
||
|
||
- name: 设置Python环境
|
||
uses: actions/setup-python@v4
|
||
with:
|
||
python-version: ${{ env.PYTHON_VERSION }}
|
||
|
||
- name: 安装依赖
|
||
run: |
|
||
python -m pip install --upgrade pip
|
||
pip install flake8 black isort mypy
|
||
pip install -r requirements.txt
|
||
|
||
- name: 代码格式检查
|
||
run: |
|
||
echo "检查代码格式..."
|
||
black --check --diff .
|
||
isort --check-only --diff .
|
||
|
||
- name: 代码质量检查
|
||
run: |
|
||
echo "运行Flake8检查..."
|
||
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
|
||
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
|
||
|
||
- name: 类型检查
|
||
run: |
|
||
echo "运行类型检查..."
|
||
mypy src/ --ignore-missing-imports
|
||
|
||
# 单元测试
|
||
test:
|
||
name: 单元测试
|
||
runs-on: ubuntu-latest
|
||
needs: code-quality
|
||
|
||
services:
|
||
mysql:
|
||
image: mysql:8.0
|
||
env:
|
||
MYSQL_ROOT_PASSWORD: test123456
|
||
MYSQL_DATABASE: test_db
|
||
ports:
|
||
- 3306:3306
|
||
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
|
||
|
||
redis:
|
||
image: redis:7-alpine
|
||
ports:
|
||
- 6379:6379
|
||
options: --health-cmd="redis-cli ping" --health-interval=10s --health-timeout=5s --health-retries=3
|
||
|
||
steps:
|
||
- name: 检出代码
|
||
uses: actions/checkout@v4
|
||
|
||
- name: 设置Python环境
|
||
uses: actions/setup-python@v4
|
||
with:
|
||
python-version: ${{ env.PYTHON_VERSION }}
|
||
|
||
- name: 安装依赖
|
||
run: |
|
||
python -m pip install --upgrade pip
|
||
pip install -r requirements.txt
|
||
pip install pytest pytest-cov pytest-mock
|
||
|
||
- name: 创建测试环境变量
|
||
run: |
|
||
echo "FLASK_ENV=testing" >> $GITHUB_ENV
|
||
echo "DATABASE_URL=mysql+pymysql://root:test123456@localhost:3306/test_db?charset=utf8mb4" >> $GITHUB_ENV
|
||
echo "SECRET_KEY=test-secret-key-for-ci" >> $GITHUB_ENV
|
||
echo "LLM_API_KEY=test-api-key" >> $GITHUB_ENV
|
||
|
||
- name: 等待数据库就绪
|
||
run: |
|
||
echo "等待MySQL数据库启动..."
|
||
sleep 30
|
||
|
||
- name: 运行单元测试
|
||
run: |
|
||
echo "运行单元测试..."
|
||
pytest tests/ -v --cov=src --cov-report=xml --cov-report=html
|
||
|
||
- name: 上传测试覆盖率报告
|
||
uses: codecov/codecov-action@v3
|
||
with:
|
||
file: ./coverage.xml
|
||
flags: unittests
|
||
name: codecov-umbrella
|
||
|
||
# 集成测试
|
||
integration-test:
|
||
name: 集成测试
|
||
runs-on: ubuntu-latest
|
||
needs: test
|
||
|
||
services:
|
||
mysql:
|
||
image: mysql:8.0
|
||
env:
|
||
MYSQL_ROOT_PASSWORD: test123456
|
||
MYSQL_DATABASE: test_db
|
||
ports:
|
||
- 3306:3306
|
||
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
|
||
|
||
redis:
|
||
image: redis:7-alpine
|
||
ports:
|
||
- 6379:6379
|
||
options: --health-cmd="redis-cli ping" --health-interval=10s --health-timeout=5s --health-retries=3
|
||
|
||
steps:
|
||
- name: 检出代码
|
||
uses: actions/checkout@v4
|
||
|
||
- name: 设置Python环境
|
||
uses: actions/setup-python@v4
|
||
with:
|
||
python-version: ${{ env.PYTHON_VERSION }}
|
||
|
||
- name: 安装依赖
|
||
run: |
|
||
python -m pip install --upgrade pip
|
||
pip install -r requirements.txt
|
||
|
||
- name: 创建测试环境变量
|
||
run: |
|
||
echo "FLASK_ENV=testing" >> $GITHUB_ENV
|
||
echo "DATABASE_URL=mysql+pymysql://root:test123456@localhost:3306/test_db?charset=utf8mb4" >> $GITHUB_ENV
|
||
echo "SECRET_KEY=test-secret-key-for-ci" >> $GITHUB_ENV
|
||
echo "LLM_API_KEY=test-api-key" >> $GITHUB_ENV
|
||
|
||
- name: 等待数据库就绪
|
||
run: |
|
||
echo "等待MySQL数据库启动..."
|
||
sleep 30
|
||
|
||
- name: 启动应用进行集成测试
|
||
run: |
|
||
echo "启动应用进行集成测试..."
|
||
python run_dev.py &
|
||
sleep 10
|
||
|
||
- name: 运行集成测试
|
||
run: |
|
||
echo "运行集成测试..."
|
||
python -c "
|
||
import requests
|
||
import time
|
||
|
||
# 等待应用启动
|
||
time.sleep(5)
|
||
|
||
# 测试健康检查
|
||
response = requests.get('http://localhost:5000/health')
|
||
assert response.status_code == 200
|
||
print('健康检查通过')
|
||
|
||
# 测试主页
|
||
response = requests.get('http://localhost:5000/')
|
||
assert response.status_code == 200
|
||
print('主页访问通过')
|
||
|
||
# 测试API端点
|
||
response = requests.post('http://localhost:5000/api/wx/templates/intent',
|
||
json={'text': '测试文本'})
|
||
assert response.status_code == 200
|
||
print('API测试通过')
|
||
"
|
||
|
||
# 构建Docker镜像
|
||
build:
|
||
name: 构建Docker镜像
|
||
runs-on: ubuntu-latest
|
||
needs: [test, integration-test]
|
||
if: github.ref == 'refs/heads/main'
|
||
|
||
steps:
|
||
- name: 检出代码
|
||
uses: actions/checkout@v4
|
||
|
||
- name: 设置Docker Buildx
|
||
uses: docker/setup-buildx-action@v3
|
||
|
||
- name: 登录Docker Hub
|
||
uses: docker/login-action@v3
|
||
with:
|
||
username: ${{ secrets.DOCKER_USERNAME }}
|
||
password: ${{ secrets.DOCKER_PASSWORD }}
|
||
|
||
- name: 构建并推送Docker镜像
|
||
uses: docker/build-push-action@v5
|
||
with:
|
||
context: .
|
||
push: true
|
||
tags: |
|
||
${{ secrets.DOCKER_USERNAME }}/flask-prompt-master:latest
|
||
${{ secrets.DOCKER_USERNAME }}/flask-prompt-master:${{ github.sha }}
|
||
cache-from: type=gha
|
||
cache-to: type=gha,mode=max
|
||
|
||
# 部署到测试环境
|
||
deploy-test:
|
||
name: 部署到测试环境
|
||
runs-on: ubuntu-latest
|
||
needs: build
|
||
if: github.ref == 'refs/heads/develop'
|
||
environment: test
|
||
|
||
steps:
|
||
- name: 检出代码
|
||
uses: actions/checkout@v4
|
||
|
||
- name: 部署到测试服务器
|
||
run: |
|
||
echo "部署到测试环境..."
|
||
# 这里可以添加部署到测试服务器的脚本
|
||
# 例如:使用SSH连接到测试服务器并更新应用
|
||
|
||
- name: 运行部署后测试
|
||
run: |
|
||
echo "运行部署后测试..."
|
||
# 等待应用启动
|
||
sleep 30
|
||
|
||
# 测试应用是否正常运行
|
||
curl -f http://test-server:5000/health || exit 1
|
||
|
||
# 部署到生产环境
|
||
deploy-production:
|
||
name: 部署到生产环境
|
||
runs-on: ubuntu-latest
|
||
needs: build
|
||
if: github.ref == 'refs/heads/main'
|
||
environment: production
|
||
|
||
steps:
|
||
- name: 检出代码
|
||
uses: actions/checkout@v4
|
||
|
||
- name: 部署到生产服务器
|
||
run: |
|
||
echo "部署到生产环境..."
|
||
# 这里可以添加部署到生产服务器的脚本
|
||
# 例如:使用SSH连接到生产服务器并更新应用
|
||
|
||
- name: 运行部署后测试
|
||
run: |
|
||
echo "运行部署后测试..."
|
||
# 等待应用启动
|
||
sleep 30
|
||
|
||
# 测试应用是否正常运行
|
||
curl -f https://production-server/health || exit 1
|
||
|
||
- name: 发送部署通知
|
||
run: |
|
||
echo "部署完成,发送通知..."
|
||
# 这里可以添加发送通知的逻辑
|
||
# 例如:发送邮件、Slack消息等
|
||
|
||
# 监控系统部署
|
||
deploy-monitoring:
|
||
name: 部署监控系统
|
||
runs-on: ubuntu-latest
|
||
needs: deploy-production
|
||
if: github.ref == 'refs/heads/main'
|
||
|
||
steps:
|
||
- name: 检出代码
|
||
uses: actions/checkout@v4
|
||
|
||
- name: 部署监控系统
|
||
run: |
|
||
echo "部署监控系统..."
|
||
# 部署监控脚本到服务器
|
||
# 配置定时任务
|
||
# 启动监控服务
|
||
|
||
- name: 验证监控系统
|
||
run: |
|
||
echo "验证监控系统..."
|
||
# 检查监控系统是否正常运行
|
||
# 测试监控脚本功能
|