Files
dukang/deploy/sync-api-env.sh
T
jacy 6dc051f26d chore(api): use .env.production on server, .env for local dev only
Load production env without .env override; sync-api-env targets .env.production; PM2 NODE_ENV=production; Prisma uses with-api-env.cjs on deploy.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-07 23:33:34 +08:00

52 lines
1.5 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.
#!/usr/bin/env bash
# 同步 API 生产环境变量到服务器 .env.production
# 用法: deploy/sync-api-env.sh [production]
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
ENV_FILE="$REPO_ROOT/deploy/deploy.env"
DEPLOY_HOST=""
DEPLOY_USER="root"
DEPLOY_PORT="22"
DEPLOY_SSH_KEY=""
APP_ROOT="/opt/dukang-haoke"
TARGET="${1:-production}"
case "$TARGET" in
production|prod) SRC_ENV="$REPO_ROOT/server/dukang-api/.env.production" ;;
development|dev)
echo "错误: development 仅用于本地开发,请使用 .env / .env.development,不同步到服务器" >&2
exit 1
;;
*)
echo "用法: $0 [production]" >&2
exit 1
;;
esac
if [[ -f "$ENV_FILE" ]]; then
# shellcheck disable=SC1090
source "$ENV_FILE"
fi
if [[ ! -f "$SRC_ENV" ]]; then
echo "错误: 未找到 $SRC_ENV(可从 .env.production.example 复制)" >&2
exit 1
fi
if [[ -z "$DEPLOY_HOST" ]]; then
echo "错误: 未配置 DEPLOY_HOSTdeploy/deploy.env" >&2
exit 1
fi
SSH_OPTS=(-o "StrictHostKeyChecking=accept-new" -p "$DEPLOY_PORT")
[[ -n "$DEPLOY_SSH_KEY" ]] && SSH_OPTS+=(-i "$DEPLOY_SSH_KEY")
REMOTE="${DEPLOY_USER}@${DEPLOY_HOST}"
echo "==> 同步 production 环境到 $REMOTE:$APP_ROOT/server/dukang-api/.env.production"
scp "${SSH_OPTS[@]}" "$SRC_ENV" "$REMOTE:$APP_ROOT/server/dukang-api/.env.production"
ssh "${SSH_OPTS[@]}" "$REMOTE" "pm2 restart dukang-api && sleep 2 && curl -sf -o /dev/null -w 'api-health:%{http_code}\n' http://127.0.0.1:8090/api/v1/health"
echo "==> 完成"