duoqi-api/scripts/deploy.sh
Wang Zhuoxuan 2a02c19dcb
Some checks failed
CI/CD Pipeline / Code Quality (push) Failing after 15s
CI/CD Pipeline / Unit Tests (push) Has been skipped
CI/CD Pipeline / Build Test Image (push) Has been skipped
CI/CD Pipeline / Build Production Image (push) Has been skipped
CI/CD Pipeline / Deploy to Test (push) Has been skipped
CI/CD Pipeline / Deploy to Production (push) Has been skipped
refactor: 统一使用 Docker Compose V2 命令语法
将所有 docker-compose(V1)替换为 docker compose(V2):
- CI/CD 流水线、部署脚本、文档中的命令调用
- 安装包名 docker-compose → docker-compose-plugin
2026-04-17 00:31:49 +08:00

106 lines
2.7 KiB
Bash
Raw Permalink 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