Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
40 changes: 40 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# PythonDL环境变量配置

# MySQL配置
MYSQL_ROOT_PASSWORD=root123456
MYSQL_DATABASE=pythondl
MYSQL_USER=pythondl
MYSQL_PASSWORD=pythondl123

# Redis配置
REDIS_PASSWORD=

# Elasticsearch配置
ELASTICSEARCH_PASSWORD=

# Grafana配置
GRAFANA_USER=admin
GRAFANA_PASSWORD=admin

# 应用配置
APP_ENV=production
APP_DEBUG=false
SECRET_KEY=your-secret-key-change-this-in-production

# 数据库配置
DATABASE_URL=mysql+mysqlconnector://pythondl:pythondl123@mysql:3306/pythondl

# Redis配置
REDIS_URL=redis://redis:6379/0

# Elasticsearch配置
ELASTICSEARCH_URL=http://elasticsearch:9200

# 日志配置
LOG_LEVEL=INFO

# 缓存配置
CACHE_TTL=3600

# 文件上传配置
MAX_UPLOAD_SIZE=104857600
156 changes: 156 additions & 0 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
name: CI/CD Pipeline

on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]

env:
DOCKER_IMAGE: ${{ secrets.DOCKER_REGISTRY }}/pythondl:${{ github.sha }}
DOCKER_IMAGE_LATEST: ${{ secrets.DOCKER_REGISTRY }}/pythondl:latest

jobs:
# 代码检查
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Install dependencies
run: |
pip install ruff black mypy

- name: Run ruff
run: ruff check app/

- name: Run black
run: black --check app/

- name: Run mypy
run: mypy app/ --ignore-missing-imports

# 测试
test:
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: test123456
MYSQL_DATABASE: pythondl_test
MYSQL_USER: test
MYSQL_PASSWORD: test123
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

env:
DATABASE_URL: mysql+mysqlconnector://test:test123@localhost:3306/pythondl_test
REDIS_URL: redis://localhost:6379/0

steps:
- uses: actions/checkout@v3

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.11'

- name: Install dependencies
run: |
pip install uv
uv pip install --system -r pyproject.toml

- name: Run database migrations
run: alembic upgrade head

- name: Run tests
run: pytest tests/ -v --cov=app --cov-report=xml --cov-report=term

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
file: ./coverage.xml
flags: unittests
name: codecov-umbrella

# 构建Docker镜像
build:
runs-on: ubuntu-latest
needs: [lint, test]
if: github.event_name == 'push' && (github.ref == 'refs/heads/main' || github.ref == 'refs/heads/develop')
steps:
- uses: actions/checkout@v3

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Login to Docker Registry
uses: docker/login-action@v2
with:
registry: ${{ secrets.DOCKER_REGISTRY }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: |
${{ env.DOCKER_IMAGE }}
${{ env.DOCKER_IMAGE_LATEST }}
cache-from: type=gha
cache-to: type=gha,mode=max

# 部署到开发环境
deploy-dev:
runs-on: ubuntu-latest
needs: [build]
if: github.event_name == 'push' && github.ref == 'refs/heads/develop'
environment:
name: development
url: http://dev.example.com
steps:
- name: Deploy to development
uses: appleboy/ssh-action@v0.1.5
with:
host: ${{ secrets.DEV_HOST }}
username: ${{ secrets.DEV_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd ${{ secrets.DEV_PATH }}
docker-compose pull
docker-compose up -d

# 部署到生产环境
deploy-prod:
runs-on: ubuntu-latest
needs: [build]
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
environment:
name: production
url: https://example.com
steps:
- name: Deploy to production
uses: appleboy/ssh-action@v0.1.5
with:
host: ${{ secrets.PROD_HOST }}
username: ${{ secrets.PROD_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
script: |
cd ${{ secrets.PROD_PATH }}
docker-compose pull
docker-compose up -d
55 changes: 55 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,59 @@
dist
./.venv
./venv
./venv/*
.python-version
.idea

config

# 日志文件
logs/*.log
logs/*.log.*

# 缓存文件
runtimes/cache/*
runtimes/sessions/*
!runtimes/.gitkeep

# 临时文件
temps/*
!temps/.gitkeep

# 数据文件
data/*
!data/.gitkeep

# 导出文件
files/exports/*
files/images/*
files/videos/*
!files/.gitkeep

# Python 缓存
__pycache__/
*.py[cod]
*$py.class
*.so
.Python

# 环境变量
.env
.env.local
.env.*.local

# IDE
.vscode/
.idea/
*.swp
*.swo
*~

# 操作系统
.DS_Store
Thumbs.db

# 数据库
*.db
*.sqlite
*.sqlite3
8 changes: 0 additions & 8 deletions .idea/.gitignore

This file was deleted.

31 changes: 31 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM python:3.11-slim

WORKDIR /app

ENV PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
PIP_NO_CACHE_DIR=1 \
PIP_DISABLE_PIP_VERSION_CHECK=1

RUN apt-get update && apt-get install -y \
gcc \
g++ \
make \
libmariadb-dev \
pkg-config \
&& rm -rf /var/lib/apt/lists/*

COPY pyproject.toml ./
COPY uv.lock ./

RUN pip install --upgrade pip && \
pip install uv && \
uv pip install --system -r pyproject.toml

COPY . .

RUN mkdir -p logs data files runtimes temps

EXPOSE 8000

CMD ["gunicorn", "-c", "gunicorn_config.py", "app:app", "-w", "4", "-k", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:8000"]
Loading
Loading