Files
dukang/deploy/sync-api-env.sh
T
2026-07-03 00:58:56 +08:00

49 lines
1.4 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 环境变量到服务器
# 用法: deploy/sync-api-env.sh [development|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:-development}"
case "$TARGET" in
development|dev) SRC_ENV="$REPO_ROOT/server/dukang-api/.env.development" ;;
production|prod) SRC_ENV="$REPO_ROOT/server/dukang-api/.env.production" ;;
*)
echo "用法: $0 [development|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" >&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 "==> 同步 $TARGET 环境到 $REMOTE:$APP_ROOT/server/dukang-api/.env"
scp "${SSH_OPTS[@]}" "$SRC_ENV" "$REMOTE:$APP_ROOT/server/dukang-api/.env"
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 "==> 完成"