diff --git a/.env b/.env new file mode 100644 index 0000000..0ed6008 --- /dev/null +++ b/.env @@ -0,0 +1,28 @@ +ENV=dev + +# 项目基础信息 +PROJECT_NAME=OfferPie AI +SERVER_PORT=8000 + +# 数据库 (MySQL) +DB_HOST=192.168.31.105 +DB_PORT=3306 +DB_USER=root +DB_PASSWORD=123456 +DB_NAME=offerpie +DB_POOL_SIZE=10 +DB_MAX_OVERFLOW=20 + +# Redis +REDIS_HOST=192.168.31.105 +REDIS_PORT=6379 +REDIS_PASSWORD=123456 +REDIS_DB=0 + +# JWT +JWT_SECRET=Aa123123 +TOKEN_EXPIRE_SECONDS=5184000 + +# 日志 +LOGGING_LEVEL=DEBUG +LOG_FILE_NAME=app.log diff --git a/.env.prod b/.env.prod index f1f1bb5..c146300 100644 --- a/.env.prod +++ b/.env.prod @@ -1,2 +1,28 @@ ENV=pro +# 项目基础信息 +PROJECT_NAME=OfferPie AI +SERVER_PORT=8000 + +# 数据库 (MySQL) +DB_HOST=8.138.5.14 +DB_PORT=30006 +DB_USER=root +DB_PASSWORD=^CgDatabase2020 +DB_NAME=offerpie +DB_POOL_SIZE=10 +DB_MAX_OVERFLOW=20 + +# Redis +REDIS_HOST=8.138.5.14 +REDIS_PORT=30089 +REDIS_PASSWORD=#8kPCdAsser +REDIS_DB=0 + +# JWT +JWT_SECRET=Aa123123 +TOKEN_EXPIRE_SECONDS=5184000 + +# 日志 +LOGGING_LEVEL=INFO +LOG_FILE_NAME=app.log diff --git a/.env.test b/.env.test index 31683c7..26491c9 100644 --- a/.env.test +++ b/.env.test @@ -1 +1,28 @@ -ENV=test \ No newline at end of file +ENV=test + +# 项目基础信息 +PROJECT_NAME=OfferPie AI +SERVER_PORT=8000 + +# 数据库 (MySQL) +DB_HOST=192.168.31.105 +DB_PORT=3306 +DB_USER=root +DB_PASSWORD=123456 +DB_NAME=offerpie +DB_POOL_SIZE=10 +DB_MAX_OVERFLOW=20 + +# Redis +REDIS_HOST=192.168.31.105 +REDIS_PORT=6379 +REDIS_PASSWORD=123456 +REDIS_DB=0 + +# JWT +JWT_SECRET=Aa123123 +TOKEN_EXPIRE_SECONDS=5184000 + +# 日志 +LOGGING_LEVEL=DEBUG +LOG_FILE_NAME=app.log diff --git a/.gitignore b/.gitignore index f11a724..a722676 100644 --- a/.gitignore +++ b/.gitignore @@ -20,7 +20,6 @@ wheels/ .installed.cfg *.egg MANIFEST -.env venv/ ENV/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3009cbf --- /dev/null +++ b/Dockerfile @@ -0,0 +1,45 @@ +# syntax=docker/dockerfile:1 + +# 使用 Python 3.12 slim 镜像 +FROM python:3.12-slim + +ENV TZ=Asia/Shanghai +ENV ENV=pro +ENV PYTHONDONTWRITEBYTECODE=1 +ENV PYTHONUNBUFFERED=1 + +# 时区 + 系统依赖 +RUN ln -sf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \ + && rm -rf /etc/apt/sources.list.d/* \ + && echo "deb https://mirrors.aliyun.com/debian/ bookworm main non-free contrib" > /etc/apt/sources.list \ + && echo "deb https://mirrors.aliyun.com/debian-security/ bookworm-security main non-free contrib" >> /etc/apt/sources.list \ + && echo "deb https://mirrors.aliyun.com/debian/ bookworm-updates main non-free contrib" >> /etc/apt/sources.list \ + && apt-get clean \ + && apt-get update \ + && apt-get install -y --no-install-recommends curl \ + && rm -rf /var/lib/apt/lists/* + +RUN mkdir -p /app/app/logs + +WORKDIR /app + +# 先拷贝依赖声明,利用 Docker 层缓存 +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt \ + -i https://mirrors.aliyun.com/pypi/simple/ \ + --trusted-host mirrors.aliyun.com + +# 拷贝应用代码和环境配置 +COPY app/ ./app/ +COPY .env.prod ./.env.prod + +EXPOSE 8000 + +HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \ + CMD curl -sf http://localhost:8000/health/ || exit 1 + +# Gunicorn + Uvicorn Worker +CMD ["gunicorn", "-w", "2", "-k", "uvicorn.workers.UvicornWorker", \ + "app.main:app", "-b", "0.0.0.0:8000", \ + "--timeout", "120", "--graceful-timeout", "30", \ + "--access-logfile", "-"] diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..c61008a --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,204 @@ +/** + * OfferPie Python AI 蓝绿部署流水线 + * + * 工作目录说明: + * - Jenkins 会自动为每个项目创建独立的工作空间:/var/jenkins_home/workspace/<项目名>/ + * - docker-compose.yml 和 Dockerfile 都在项目根目录,直接执行即可 + * - nginx.conf 在项目根目录,首次部署时 docker cp 进 nginx 容器 + */ +pipeline { + agent any + + parameters { + choice(name: 'BRANCH', choices: ['master', 'pre', 'dev', 'test'], description: '选择要部署的分支') + } + + environment { + CONTAINER_PREFIX = 'offerpie-ai' // 容器名前缀,拼接 -blue / -green / -nginx + HEALTH_URL = 'http://localhost:8000/health/' // 应用健康检查地址 + } + + stages { + stage('开始提示') { + steps { + echo "OfferPie Python AI 开始构建" + } + } + + stage('拉取代码') { + steps { + echo "拉取 ${params.BRANCH} 分支代码" + git branch: "${params.BRANCH}", + credentialsId: 'ef5fffc1-9b35-403d-9ca6-e1b73eb0e45a', + url: 'https://codeup.aliyun.com/5f0ed3b9769820a3e817dee2/offerpie/offerpie_python_ai.git' + } + } + + /** + * 检测部署目标 + * - 检查 blue 和 green 容器的运行状态 + * - blue 在运行 → 部署 green;green 在运行 → 部署 blue + * - 都不在或都在 → 默认部署 blue + */ + stage('检测部署目标') { + steps { + echo "检查当前容器状态" + script { + def blueRunning = sh(script: "docker ps -q -f name=${CONTAINER_PREFIX}-blue", returnStdout: true).trim() + def greenRunning = sh(script: "docker ps -q -f name=${CONTAINER_PREFIX}-green", returnStdout: true).trim() + + env.DEPLOY_TARGET = '' + + if (blueRunning && !greenRunning) { + env.DEPLOY_TARGET = 'green' + } + if (greenRunning && !blueRunning) { + env.DEPLOY_TARGET = 'blue' + } + if (!env.DEPLOY_TARGET) { + echo "当前环境未部署服务或状态异常,默认部署 blue" + env.DEPLOY_TARGET = 'blue' + } + + env.OTHER_TARGET = (env.DEPLOY_TARGET == 'blue') ? 'green' : 'blue' + echo "当前激活: ${env.OTHER_TARGET},即将部署: ${env.DEPLOY_TARGET}" + } + } + } + + /** + * 构建新版本 + * - 先清理目标颜色的旧容器(如果残留) + * - docker-compose build 编译新镜像 + */ + stage('构建新版本') { + steps { + script { + def existingContainer = sh(script: "docker ps -aq -f name=${CONTAINER_PREFIX}-${env.DEPLOY_TARGET}", returnStdout: true).trim() + if (existingContainer) { + echo "清理已存在的容器: ${CONTAINER_PREFIX}-${env.DEPLOY_TARGET}" + sh "docker rm -f ${CONTAINER_PREFIX}-${env.DEPLOY_TARGET}" + } + sh "docker-compose build ${env.DEPLOY_TARGET}" + } + } + } + + /** + * 检查 Nginx + * - 检查 nginx 容器是否存在 + * - 不存在则启动并复制配置文件(首次部署) + * - 存在则确保容器运行中 + */ + stage('检查Nginx') { + steps { + script { + def nginxExists = sh(script: "docker ps -aq -f name=${CONTAINER_PREFIX}-nginx", returnStdout: true).trim() + if (!nginxExists) { + echo "首次部署,初始化 Nginx 容器" + sh "docker-compose up -d nginx" + sh "sleep 3" + // 复制配置文件到容器内 + sh "docker cp nginx.conf ${CONTAINER_PREFIX}-nginx:/etc/nginx/nginx.conf" + sh "docker exec ${CONTAINER_PREFIX}-nginx nginx -s reload" + } else { + def nginxRunning = sh(script: "docker ps -q -f name=${CONTAINER_PREFIX}-nginx", returnStdout: true).trim() + if (!nginxRunning) { + echo "Nginx 容器已停止,重新启动" + sh "docker start ${CONTAINER_PREFIX}-nginx" + sh "sleep 2" + } + echo "Nginx 容器已存在且运行中" + } + } + } + } + + stage('启动新版本') { + steps { + script { + sh "docker-compose up -d ${env.DEPLOY_TARGET}" + // 等待 FastAPI 应用启动 + sh 'sleep 15' + } + } + } + + /** + * 健康检查 + * - 进入容器执行 curl 检测应用是否正常响应 + * - curl -f 参数:HTTP 错误码(404、500等)时返回失败 + * - 最多重试 3 次,每次间隔 5 秒 + * - 全部失败则终止部署流程 + */ + stage('健康检查') { + steps { + script { + def maxRetries = 3 + def retryCount = 0 + def healthy = false + + while (retryCount < maxRetries && !healthy) { + try { + sh "docker exec ${CONTAINER_PREFIX}-${env.DEPLOY_TARGET} curl -f ${HEALTH_URL}" + healthy = true + } catch (Exception e) { + retryCount++ + if (retryCount < maxRetries) { + echo "健康检查失败,5秒后重试 (${retryCount}/${maxRetries})" + sleep 5 + } + } + } + + if (!healthy) { + error "健康检查失败,部署中止" + } + echo "✅ ${CONTAINER_PREFIX}-${env.DEPLOY_TARGET} 健康检查通过" + } + } + } + + /** + * 切换流量 + * - 修改 nginx 配置中的 proxy_pass 指向新版本容器 + * - nginx 不挂载宿主机文件,直接 sed -i 修改容器内配置 + * - reload 使新配置生效,实现零停机切换 + */ + stage('切换流量') { + steps { + script { + sh "docker exec ${CONTAINER_PREFIX}-nginx sed -i 's/proxy_pass http:\\/\\/\\(blue\\|green\\):8000;/proxy_pass http:\\/\\/${env.DEPLOY_TARGET}:8000;/' /etc/nginx/nginx.conf" + sh "docker exec ${CONTAINER_PREFIX}-nginx nginx -s reload" + echo "✅ 流量已切换到 ${env.DEPLOY_TARGET}" + } + } + } + + /** + * 停止并删除旧版本 + * - 不能用 docker-compose down,会删除所有服务(包括 nginx) + * - 用 docker rm -f 只删除指定的旧颜色容器 + */ + stage('删除旧版本') { + steps { + script { + def otherExists = sh(script: "docker ps -aq -f name=${CONTAINER_PREFIX}-${env.OTHER_TARGET}", returnStdout: true).trim() + if (otherExists) { + sh "docker rm -f ${CONTAINER_PREFIX}-${env.OTHER_TARGET}" + echo "旧版本 ${env.OTHER_TARGET} 已删除" + } + } + } + } + } + + post { + success { + echo "✅ 蓝绿部署成功!当前运行: ${env.DEPLOY_TARGET}" + } + failure { + echo '❌ 部署失败,请检查日志' + } + } +} diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..502997c --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,68 @@ +services: + nginx: + image: nginx:alpine + container_name: offerpie-ai-nginx + restart: unless-stopped + ports: + - "10502:80" + depends_on: + - blue + - green + healthcheck: + test: ["CMD", "wget", "--spider", "-q", "http://localhost/health"] + interval: 30s + timeout: 10s + retries: 3 + deploy: + resources: + limits: + memory: 256M + cpus: '1' + + blue: + build: . + container_name: offerpie-ai-blue + restart: unless-stopped + expose: + - "8000" + environment: + - APP_VERSION=blue + - ENV=pro + - TZ=Asia/Shanghai + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:8000/health/"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 20s + volumes: + - /logs/offerpie-ai:/app/app/logs + deploy: + resources: + limits: + memory: 2G + cpus: '2' + + green: + build: . + container_name: offerpie-ai-green + restart: unless-stopped + expose: + - "8000" + environment: + - APP_VERSION=green + - ENV=pro + - TZ=Asia/Shanghai + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:8000/health/"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 20s + volumes: + - /logs/offerpie-ai:/app/app/logs + deploy: + resources: + limits: + memory: 2G + cpus: '2' diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..fd83780 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,60 @@ +worker_processes auto; +error_log /var/log/nginx/error.log warn; +pid /var/run/nginx.pid; + +events { + worker_connections 1024; + use epoll; + multi_accept on; +} + +http { + include /etc/nginx/mime.types; + default_type application/octet-stream; + + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' + '$status $body_bytes_sent "$http_referer" ' + '"$http_user_agent" "$http_x_forwarded_for"'; + access_log /var/log/nginx/access.log main; + + sendfile on; + tcp_nopush on; + tcp_nodelay on; + keepalive_timeout 65; + + client_max_body_size 20m; + + server { + listen 80; + server_name _; + + # Nginx 自身健康检查 + location /health { + access_log off; + return 200 'ok'; + add_header Content-Type text/plain; + } + + # 默认代理到 blue,部署时通过 sed 切换 + location / { + proxy_pass http://blue:8000; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + + # WebSocket 支持(AI 流式输出) + proxy_http_version 1.1; + proxy_set_header Upgrade $http_upgrade; + proxy_set_header Connection "upgrade"; + + # 流式输出,关闭缓冲 + proxy_buffering off; + proxy_cache off; + + proxy_connect_timeout 300s; + proxy_send_timeout 300s; + proxy_read_timeout 300s; + } + } +}