duoqi-api/scripts/deploy.sh
Wang Zhuoxuan e893755340 feat: 添加 CI/CD 部署方案(Gitea + Docker + 双分支工作流)
- Dockerfile: 多阶段构建,oven/bun 基础镜像,非 root 用户
- docker-compose.yml: 本地开发环境(API + MySQL + Drizzle Studio)
- docker-compose.prod.yml: 服务器部署(prod + test,Docker profiles)
- .gitea/workflows/deploy.yml: 双分支 CI/CD(develop→测试, main→生产)
- docs/ci-deployment-guide.md: 完整部署指南(Alibaba Cloud Linux 3)
- scripts/deploy.sh: 手动部署运维脚本
- .env.prod.example: 生产环境变量模板
- .dockerignore: 排除非构建文件
- .gitignore: 排除 .claude/ 目录
2026-04-16 12:44:14 +08:00

106 lines
2.7 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Duoqi API 部署脚本
# 用法bash scripts/deploy.sh [命令] [环境]
#
# 命令:
# prod - 构建并部署生产环境
# test - 构建并部署测试环境
# test-stop - 停止测试环境(释放内存)
# status - 查看所有容器状态
# rollback - 回滚生产环境到上一版本
# logs - 查看生产日志
set -e
DEPLOY_DIR="/opt/duoqi-api"
COMMAND="${1:-status}"
ENV="${2:-prod}"
cd "$DEPLOY_DIR" 2>/dev/null || {
echo "Error: Deploy directory $DEPLOY_DIR not found"
echo "Please run server setup first."
exit 1
}
case "$COMMAND" in
prod)
echo "=== Building and deploying production ==="
docker-compose build api-prod
docker-compose up -d api-prod
echo "Waiting for health check..."
sleep 15
if curl -sf http://localhost:3000/health > /dev/null; then
echo "✓ Production deployment successful!"
else
echo "✗ Production health check failed!"
exit 1
fi
;;
test)
echo "=== Building and deploying test ==="
docker-compose build api-test
docker-compose --profile test up -d api-test
echo "Waiting for health check..."
sleep 10
if curl -sf http://localhost:3001/health > /dev/null; then
echo "✓ Test deployment successful!"
else
echo "✗ Test health check failed!"
exit 1
fi
;;
test-stop)
echo "=== Stopping test environment ==="
docker-compose --profile test stop api-test
echo "✓ Test environment stopped"
free -h | head -2
;;
status)
echo "=== Container Status ==="
docker-compose ps
echo ""
echo "=== Memory Usage ==="
free -h | head -2
echo ""
echo "=== Disk Usage ==="
df -h / | tail -1
;;
rollback)
echo "=== Rolling back production ==="
if docker images | grep -q "duoqi-api:rollback"; then
docker tag duoqi-api:rollback duoqi-api:prod
docker-compose up -d --no-build api-prod
echo "✓ Rollback completed!"
else
echo "✗ No rollback image found!"
exit 1
fi
;;
logs)
ENV="${2:-prod}"
if [ "$ENV" = "test" ]; then
docker-compose --profile test logs -f --tail=100 api-test
else
docker-compose logs -f --tail=100 api-prod
fi
;;
*)
echo "Usage: bash scripts/deploy.sh [command]"
echo ""
echo "Commands:"
echo " prod Build and deploy production"
echo " test Build and deploy test environment"
echo " test-stop Stop test environment (free memory)"
echo " status Show container status"
echo " rollback Rollback production"
echo " logs [env] View logs (prod/test, default: prod)"
exit 1
;;
esac