#!/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