chore(deploy): add same-host staging stack next to production

dev deploys to /opt/dukang-staging (819x, *-test domains); main deploys to production. Staging uses full Mock with shared WeChat app ids.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-07-31 11:37:18 +08:00
parent 8d6c0f9ac0
commit 6a23e79f4c
20 changed files with 793 additions and 119 deletions
+46
View File
@@ -0,0 +1,46 @@
#!/usr/bin/env bash
# 在生产机上初始化 Staging 目录(同机双栈,一次性)
# 用法(SSH 到服务器后):
# APP_ROOT=/opt/dukang bash /opt/dukang/deploy/bootstrap-staging.sh
# 或本机:
# ssh root@host 'bash -s' < deploy/bootstrap-staging.sh
set -euo pipefail
PROD_ROOT="${PROD_ROOT:-/opt/dukang}"
STAGING_ROOT="${STAGING_ROOT:-/opt/dukang-staging}"
GIT_REMOTE_URL="${GIT_REMOTE_URL:-}"
GIT_BRANCH="${GIT_BRANCH:-dev}"
if [[ -d "$STAGING_ROOT/.git" ]]; then
echo "==> 已存在 $STAGING_ROOT,跳过 clone"
else
if [[ -z "$GIT_REMOTE_URL" ]]; then
if [[ -d "$PROD_ROOT/.git" ]]; then
GIT_REMOTE_URL="$(git -C "$PROD_ROOT" remote get-url origin)"
else
echo "错误: 请设置 GIT_REMOTE_URL,或确保 $PROD_ROOT 为 git 仓库" >&2
exit 1
fi
fi
echo "==> clone $GIT_REMOTE_URL$STAGING_ROOT (branch $GIT_BRANCH)"
git clone --branch "$GIT_BRANCH" "$GIT_REMOTE_URL" "$STAGING_ROOT"
fi
mkdir -p "$STAGING_ROOT/server/dukang-api"
if [[ ! -f "$STAGING_ROOT/server/dukang-api/.env.staging" ]]; then
if [[ -f "$STAGING_ROOT/server/dukang-api/.env.staging.example" ]]; then
cp "$STAGING_ROOT/server/dukang-api/.env.staging.example" \
"$STAGING_ROOT/server/dukang-api/.env.staging"
echo "==> 已生成 .env.staging(请编辑 DATABASE_URL / Redis / 微信后 sync 或本机改)"
else
echo "WARN: 无 .env.staging.example,请手动创建 .env.staging"
fi
fi
echo "==> 下一步:"
echo " 1. 创建 RDS 库 dukang_staging,填好 $STAGING_ROOT/server/dukang-api/.env.staging"
echo " 2. DNS: api-test/user-test/shop-test/partner-test/admin-test.dukanghaoke.com → 本机"
echo " 3. 启用 nginx: ln -sf $STAGING_ROOT/deploy/nginx-dukang-staging.conf /etc/nginx/sites-enabled/"
echo " nginx -t && systemctl reload nginx(证书需含 *-test SAN"
echo " 4. 本机发版: bash deploy/deploy-staging.sh -- --seed --accept-data-loss"
echo "==> bootstrap 完成"
+6
View File
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
# 发生产环境:分支默认 main → /opt/dukang80908094
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
echo "==> 即将发往 PRODUCTIONmain → /opt/dukang"
exec bash "$SCRIPT_DIR/deploy.sh" --env production "$@"
+5
View File
@@ -0,0 +1,5 @@
#!/usr/bin/env bash
# 发测试环境:分支默认 dev → /opt/dukang-staging81908194
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
exec bash "$SCRIPT_DIR/deploy.sh" --env staging "$@"
+7 -1
View File
@@ -4,8 +4,14 @@ DEPLOY_USER=root
DEPLOY_PORT=22
# DEPLOY_SSH_KEY=~/.ssh/id_rsa
# 同机双栈目录
PROD_APP_ROOT=/opt/dukang
STAGING_APP_ROOT=/opt/dukang-staging
# 兼容旧字段(未指定 --env 时可能用到)
APP_ROOT=/opt/dukang
# 生产仓库(SSH
GIT_REPO_URL=git@git.yqidian.com:jacy/dukang.git
GIT_REMOTE=origin
GIT_BRANCH=dev
# 默认分支已由 --env 决定:staging→devproduction→main
# GIT_BRANCH=dev
+65 -20
View File
@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# 本地执行:SSH 到服务器拉代码并发布
# 本地执行:SSH 到服务器拉代码并发布(支持 staging / production 同机双栈)
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
@@ -9,33 +9,35 @@ DEPLOY_HOST=""
DEPLOY_USER="root"
DEPLOY_PORT="22"
DEPLOY_SSH_KEY=""
APP_ROOT="/opt/dukang"
APP_ROOT=""
GIT_REMOTE="origin"
GIT_BRANCH="dev"
GIT_BRANCH=""
DUKANG_DEPLOY_ENV=""
CHECK_ONLY=false
RELEASE_ARGS=()
usage() {
cat <<'EOF'
用法: deploy/deploy.sh [选项] [-- 远程 release 参数]
用法: deploy/deploy.sh --env staging|production [选项] [-- 远程 release 参数]
环境配置: deploy/deploy.env(可复制 deploy.env.example
选项:
--env ENV staging(测试)| production(生产)【必填,或用 deploy-staging/prod.sh】
--host HOST 服务器地址(覆盖 deploy.env
--user USER SSH 用户,默认 root
--port PORT SSH 端口,默认 22
--key PATH SSH 私钥路径
--branch BRANCH 发布分支,默认 dev
--branch BRANCH 发布分支staging 默认 devproduction 默认 main
--check 仅 SSH 连接并查看 PM2 / 端口状态
-h, --help 显示帮助
示例:
./deploy/deploy.sh
./deploy/deploy.sh --branch main
./deploy/deploy.sh -- --skip-db
./deploy/deploy.sh -- --seed --accept-data-loss
./deploy/deploy-staging.sh -- --skip-db
./deploy/deploy-prod.sh -- --skip-db
./deploy/deploy.sh --env staging --branch dev -- --skip-db
./deploy/deploy.sh --env production --branch main -- --skip-db
EOF
}
@@ -48,6 +50,7 @@ load_env() {
while [[ $# -gt 0 ]]; do
case "$1" in
--env) DUKANG_DEPLOY_ENV="$2"; shift 2 ;;
--host) DEPLOY_HOST="$2"; shift 2 ;;
--user) DEPLOY_USER="$2"; shift 2 ;;
--port) DEPLOY_PORT="$2"; shift 2 ;;
@@ -66,6 +69,46 @@ done
load_env
# 兼容旧调用:未传 --env 时,若 GIT_BRANCH=main 视为生产,否则 staging
if [[ -z "$DUKANG_DEPLOY_ENV" ]]; then
if [[ "${GIT_BRANCH:-}" == "main" || "${GIT_BRANCH:-}" == "master" ]]; then
DUKANG_DEPLOY_ENV=production
else
DUKANG_DEPLOY_ENV=staging
echo "WARN: 未指定 --env,默认按 staging 发测试环境。生产请用 --env production 或 deploy-prod.sh" >&2
fi
fi
case "$DUKANG_DEPLOY_ENV" in
staging|stage|test) DUKANG_DEPLOY_ENV=staging ;;
production|prod) DUKANG_DEPLOY_ENV=production ;;
*)
echo "错误: --env 须为 staging 或 production" >&2
exit 1
;;
esac
if [[ "$DUKANG_DEPLOY_ENV" == "staging" ]]; then
APP_ROOT="${STAGING_APP_ROOT:-${APP_ROOT:-/opt/dukang-staging}}"
# 若 deploy.env 里 APP_ROOT 仍是生产路径,staging 强制用 STAGING_APP_ROOT 或默认
if [[ -n "${STAGING_APP_ROOT:-}" ]]; then
APP_ROOT="$STAGING_APP_ROOT"
elif [[ "$APP_ROOT" == "/opt/dukang" ]]; then
APP_ROOT="/opt/dukang-staging"
fi
GIT_BRANCH="${GIT_BRANCH:-dev}"
PORT_GREP='819[0-4]'
else
APP_ROOT="${PROD_APP_ROOT:-${APP_ROOT:-/opt/dukang}}"
if [[ -n "${PROD_APP_ROOT:-}" ]]; then
APP_ROOT="$PROD_APP_ROOT"
elif [[ "$APP_ROOT" == "/opt/dukang-staging" ]]; then
APP_ROOT="/opt/dukang"
fi
GIT_BRANCH="${GIT_BRANCH:-main}"
PORT_GREP='809[0-4]'
fi
if [[ -z "$DEPLOY_HOST" ]]; then
echo "错误: 未配置 DEPLOY_HOST。请创建 deploy/deploy.env 或使用 --host" >&2
exit 1
@@ -82,8 +125,8 @@ run_remote() {
}
if [[ "$CHECK_ONLY" == true ]]; then
echo "==> 检查 $TARGET"
run_remote "pm2 list; ss -tlnp | grep -E '809[0-3]' || true"
echo "==> 检查 $TARGET ($DUKANG_DEPLOY_ENV)"
run_remote "pm2 list; ss -tlnp | grep -E '$PORT_GREP' || true"
exit 0
fi
@@ -92,7 +135,7 @@ if [[ ${#RELEASE_ARGS[@]} -gt 0 ]]; then
REMOTE_RELEASE_ARGS="${RELEASE_ARGS[*]}"
fi
echo "==> 发布到 $TARGET ($GIT_BRANCH)"
echo "==> 发布到 $TARGET env=$DUKANG_DEPLOY_ENV branch=$GIT_BRANCH root=$APP_ROOT"
run_remote bash -s <<EOF
set -euo pipefail
@@ -100,26 +143,28 @@ APP_ROOT="$APP_ROOT"
GIT_REMOTE="$GIT_REMOTE"
GIT_BRANCH="$GIT_BRANCH"
RELEASE_ARGS="$REMOTE_RELEASE_ARGS"
DUKANG_DEPLOY_ENV="$DUKANG_DEPLOY_ENV"
export DEPLOY_TRIGGER=manual
export APP_ROOT
export DUKANG_DEPLOY_ENV
cd "\$APP_ROOT"
if [[ ! -d .git ]]; then
echo "错误: \$APP_ROOT 不是 git 仓库" >&2
echo "错误: \$APP_ROOT 不是 git 仓库。请先 clone 到该目录(staging 建议 /opt/dukang-staging" >&2
exit 1
fi
echo "==> 拉取代码"
git fetch "\$GIT_REMOTE"
git checkout "\$GIT_BRANCH"
git pull --ff-only "\$GIT_REMOTE" "\$GIT_BRANCH"
if [[ -n "\$RELEASE_ARGS" ]]; then
echo "==> 拉取代码"
git fetch "\$GIT_REMOTE"
git checkout "\$GIT_BRANCH"
git pull --ff-only "\$GIT_REMOTE" "\$GIT_BRANCH"
bash deploy/remote-release.sh \$RELEASE_ARGS
else
echo "==> 拉取代码并发布(auto-release"
bash deploy/auto-release.sh
bash deploy/remote-release.sh --skip-db
fi
EOF
echo "==> 本地发布命令已完成"
echo "==> 本地发布命令已完成 ($DUKANG_DEPLOY_ENV)"
+3 -1
View File
@@ -1,5 +1,5 @@
/** PM2 ecosystem — 杜康好客生产部署 */
const APP_ROOT = '/opt/dukang';
const APP_ROOT = process.env.APP_ROOT || '/opt/dukang';
module.exports = {
apps: [
@@ -11,6 +11,7 @@ module.exports = {
exec_mode: 'fork',
env: {
NODE_ENV: 'production',
APP_ENV: 'production',
PORT: 8090,
},
},
@@ -62,6 +63,7 @@ module.exports = {
exec_mode: 'fork',
env: {
NODE_ENV: 'production',
APP_ENV: 'production',
},
},
],
+55
View File
@@ -0,0 +1,55 @@
/** PM2 ecosystem — 杜康好客 Staging(同机双栈,端口 81908194 */
const APP_ROOT = process.env.APP_ROOT || '/opt/dukang-staging';
module.exports = {
apps: [
{
name: 'dukang-stg-api',
cwd: `${APP_ROOT}/server/dukang-api`,
script: 'dist/main.js',
instances: 1,
exec_mode: 'fork',
env: {
NODE_ENV: 'production',
APP_ENV: 'staging',
PORT: 8190,
},
},
{
name: 'dukang-stg-h5-user',
cwd: `${APP_ROOT}/apps/mini-user`,
script: 'npx',
args: 'serve dist -l 8191 -c serve.json',
interpreter: 'none',
instances: 1,
exec_mode: 'fork',
},
{
name: 'dukang-stg-h5-shop',
cwd: `${APP_ROOT}/apps/h5-shop`,
script: 'npx',
args: 'serve -s dist -l 8192',
interpreter: 'none',
instances: 1,
exec_mode: 'fork',
},
{
name: 'dukang-stg-h5-partner',
cwd: `${APP_ROOT}/apps/h5-partner`,
script: 'npx',
args: 'serve -s dist -l 8193',
interpreter: 'none',
instances: 1,
exec_mode: 'fork',
},
{
name: 'dukang-stg-admin-web',
cwd: `${APP_ROOT}/apps/admin-web`,
script: 'npx',
args: 'serve -s dist -l 8194',
interpreter: 'none',
instances: 1,
exec_mode: 'fork',
},
],
};
+187
View File
@@ -0,0 +1,187 @@
# 杜康好客 — Staging(测试)*-test.dukanghaoke.com → 81908194
# 证书:可与生产共用 user.dukanghaoke.com 证书(SAN 含 *-test),或单独申请
# 启用:ln -sf /opt/dukang-staging/deploy/nginx-dukang-staging.conf /etc/nginx/sites-enabled/
# nginx -t && systemctl reload nginx
# HTTP → HTTPS
server {
listen 80;
server_name user-test.dukanghaoke.com shop-test.dukanghaoke.com partner-test.dukanghaoke.com admin-test.dukanghaoke.com api-test.dukanghaoke.com;
location ^~ /.well-known/acme-challenge/ {
root /var/www/certbot;
default_type "text/plain";
}
location / {
return 301 https://$host$request_uri;
}
}
# api-test.dukanghaoke.com → 8190
server {
listen 443 ssl;
http2 on;
server_name api-test.dukanghaoke.com;
ssl_certificate /etc/letsencrypt/live/user.dukanghaoke.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/user.dukanghaoke.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
client_max_body_size 50m;
location / {
proxy_pass http://127.0.0.1:8190;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Connection "";
}
}
# user-test.dukanghaoke.com → 8191
server {
listen 443 ssl;
http2 on;
server_name user-test.dukanghaoke.com;
ssl_certificate /etc/letsencrypt/live/user.dukanghaoke.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/user.dukanghaoke.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
client_max_body_size 50m;
location /api/ {
proxy_pass http://127.0.0.1:8190;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Connection "";
}
location / {
proxy_pass http://127.0.0.1:8191;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
}
# shop-test.dukanghaoke.com → 8192
server {
listen 443 ssl;
http2 on;
server_name shop-test.dukanghaoke.com;
ssl_certificate /etc/letsencrypt/live/user.dukanghaoke.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/user.dukanghaoke.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
client_max_body_size 50m;
location /api/ {
proxy_pass http://127.0.0.1:8190;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Connection "";
}
location / {
proxy_pass http://127.0.0.1:8192;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
}
# partner-test.dukanghaoke.com → 8193
server {
listen 443 ssl;
http2 on;
server_name partner-test.dukanghaoke.com;
ssl_certificate /etc/letsencrypt/live/user.dukanghaoke.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/user.dukanghaoke.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
client_max_body_size 50m;
location /api/ {
proxy_pass http://127.0.0.1:8190;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Connection "";
}
location / {
proxy_pass http://127.0.0.1:8193;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
}
# admin-test.dukanghaoke.com → 8194
server {
listen 443 ssl;
http2 on;
server_name admin-test.dukanghaoke.com;
ssl_certificate /etc/letsencrypt/live/user.dukanghaoke.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/user.dukanghaoke.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
client_max_body_size 50m;
location /api/ {
proxy_pass http://127.0.0.1:8190;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Connection "";
}
location / {
proxy_pass http://127.0.0.1:8194;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_cache_bypass $http_upgrade;
}
}
+66 -21
View File
@@ -1,9 +1,50 @@
#!/usr/bin/env bash
# 在服务器上执行:拉取后的常规发版(不覆盖 .env.production、默认不 seed
# 在服务器上执行:拉取后的常规发版(不覆盖 env 文件、默认不 seed
# 通过 DUKANG_DEPLOY_ENV=staging|production 区分同机双栈
set -euo pipefail
APP_ROOT="${APP_ROOT:-/opt/dukang}"
DUKANG_DEPLOY_ENV="${DUKANG_DEPLOY_ENV:-production}"
case "$DUKANG_DEPLOY_ENV" in
staging|stage|test) DUKANG_DEPLOY_ENV=staging ;;
production|prod) DUKANG_DEPLOY_ENV=production ;;
*)
echo "错误: DUKANG_DEPLOY_ENV 须为 staging 或 production,当前=$DUKANG_DEPLOY_ENV" >&2
exit 1
;;
esac
if [[ "$DUKANG_DEPLOY_ENV" == "staging" ]]; then
APP_ROOT="${APP_ROOT:-/opt/dukang-staging}"
ENV_FILE_NAME=".env.staging"
ECOSYSTEM_FILE="ecosystem.staging.config.cjs"
VITE_API_DEFAULT="https://api-test.dukanghaoke.com"
API_PORT=8190
USER_PORT=8191
PORT_GREP='819[0-4]'
PM2_API=dukang-stg-api
PM2_USER=dukang-stg-h5-user
PM2_SHOP=dukang-stg-h5-shop
PM2_PARTNER=dukang-stg-h5-partner
PM2_ADMIN=dukang-stg-admin-web
else
APP_ROOT="${APP_ROOT:-/opt/dukang}"
ENV_FILE_NAME=".env.production"
ECOSYSTEM_FILE="ecosystem.config.cjs"
VITE_API_DEFAULT="https://api.dukanghaoke.com"
API_PORT=8090
USER_PORT=8091
PORT_GREP='809[0-4]'
PM2_API=dukang-api
PM2_USER=dukang-h5-user
PM2_SHOP=dukang-h5-shop
PM2_PARTNER=dukang-h5-partner
PM2_ADMIN=dukang-admin-web
fi
DEPLOY_DIR="$APP_ROOT/deploy"
export APP_ROOT
export APP_ENV="$DUKANG_DEPLOY_ENV"
export DUKANG_ENV_FILE="$ENV_FILE_NAME"
SKIP_BUILD=false
SKIP_DB=false
@@ -11,9 +52,11 @@ RUN_SEED=false
DB_PUSH_ACCEPT_DATA_LOSS=false
usage() {
cat <<'EOF'
cat <<EOF
用法: remote-release.sh [选项]
环境: DUKANG_DEPLOY_ENV=${DUKANG_DEPLOY_ENV} APP_ROOT=${APP_ROOT} env=${ENV_FILE_NAME}
--skip-build 跳过 pnpm build(仅重启 PM2
--skip-db 跳过 Prisma generate / db push
--seed 执行 prisma:seed(默认不执行)
@@ -36,6 +79,8 @@ done
cd "$APP_ROOT"
echo "==> 发版环境: $DUKANG_DEPLOY_ENV ($APP_ROOT)"
echo "==> 1. 启用 pnpm"
corepack enable 2>/dev/null || true
corepack prepare pnpm@11.2.2 --activate 2>/dev/null || true
@@ -47,15 +92,16 @@ export NODE_OPTIONS="${NODE_OPTIONS:---max-old-space-size=8192}"
export TARO_H5_PUBLIC_PATH="${TARO_H5_PUBLIC_PATH:-/user/}"
export TARO_H5_ROUTER_BASENAME="${TARO_H5_ROUTER_BASENAME:-/user}"
# C 端 H5 编译期注入的 API origin(勿落到 localhost
export VITE_API_TARGET="${VITE_API_TARGET:-https://api.dukanghaoke.com}"
export VITE_API_TARGET="${VITE_API_TARGET:-$VITE_API_DEFAULT}"
export VITE_APP_ENV="$DUKANG_DEPLOY_ENV"
pnpm approve-builds --all 2>/dev/null || true
pnpm install --frozen-lockfile 2>/dev/null || pnpm install
if [[ "$SKIP_DB" == false ]]; then
echo "==> 3. 数据库 schema 同步(构建前,读取 .env.production"
echo "==> 3. 数据库 schema 同步(构建前,读取 $ENV_FILE_NAME"
cd "$APP_ROOT/server/dukang-api"
if [[ ! -f .env.production ]]; then
echo "错误: 未找到 server/dukang-api/.env.production" >&2
if [[ ! -f "$ENV_FILE_NAME" ]]; then
echo "错误: 未找到 server/dukang-api/$ENV_FILE_NAME" >&2
exit 1
fi
node scripts/with-api-env.cjs pnpm prisma:generate
@@ -94,31 +140,30 @@ else
fi
fi
echo "==> 5. 重启 PM2"
if pm2 describe dukang-api &>/dev/null; then
# delete+start 以确保 cwd 从 h5-user 切到 mini-user
pm2 delete dukang-h5-user 2>/dev/null || true
pm2 start "$DEPLOY_DIR/ecosystem.config.cjs" --only dukang-h5-user
pm2 restart dukang-api dukang-h5-shop dukang-h5-partner dukang-admin-web 2>/dev/null \
|| pm2 restart dukang-api dukang-h5-shop dukang-h5-partner
echo "==> 5. 重启 PM2 ($ECOSYSTEM_FILE)"
if pm2 describe "$PM2_API" &>/dev/null; then
pm2 delete "$PM2_USER" 2>/dev/null || true
pm2 start "$DEPLOY_DIR/$ECOSYSTEM_FILE" --only "$PM2_USER"
pm2 restart "$PM2_API" "$PM2_SHOP" "$PM2_PARTNER" "$PM2_ADMIN" 2>/dev/null \
|| pm2 restart "$PM2_API" "$PM2_SHOP" "$PM2_PARTNER"
else
pm2 start "$DEPLOY_DIR/ecosystem.config.cjs"
pm2 start "$DEPLOY_DIR/$ECOSYSTEM_FILE"
fi
pm2 save
echo "==> 6. 健康检查"
sleep 2
pm2 list
ss -tlnp | grep -E '809[0-4]' || true
curl -sf -o /dev/null -w "mini-user(h5): %{http_code}\n" http://127.0.0.1:8091/ || echo "mini-user(h5): FAIL"
curl -sf -o /dev/null -w "api: %{http_code}\n" http://127.0.0.1:8090/api/v1/health 2>/dev/null \
|| curl -sf -o /dev/null -w "api: %{http_code}\n" http://127.0.0.1:8090/ \
ss -tlnp | grep -E "$PORT_GREP" || true
curl -sf -o /dev/null -w "mini-user(h5): %{http_code}\n" "http://127.0.0.1:${USER_PORT}/" || echo "mini-user(h5): FAIL"
curl -sf -o /dev/null -w "api: %{http_code}\n" "http://127.0.0.1:${API_PORT}/api/v1/health" 2>/dev/null \
|| curl -sf -o /dev/null -w "api: %{http_code}\n" "http://127.0.0.1:${API_PORT}/" \
|| echo "api: FAIL"
echo "==> 7. 记录系统版本"
cd "$APP_ROOT/server/dukang-api"
APP_ROOT="$APP_ROOT" DEPLOY_TRIGGER="${DEPLOY_TRIGGER:-manual}" \
APP_ROOT="$APP_ROOT" DEPLOY_TRIGGER="${DEPLOY_TRIGGER:-manual}" DUKANG_ENV_FILE="$ENV_FILE_NAME" \
node scripts/with-api-env.cjs node scripts/record-system-version.cjs \
|| echo "WARN: system_version 写入失败"
echo "==> 发版完成"
echo "==> 发版完成 ($DUKANG_DEPLOY_ENV)"
+32 -10
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
# 同步 API 生产环境变量到服务器 .env.production
# 用法: deploy/sync-api-env.sh [production]
# 同步 API 环境变量到服务器
# 用法: deploy/sync-api-env.sh staging|production
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
@@ -12,16 +12,31 @@ DEPLOY_USER="root"
DEPLOY_PORT="22"
DEPLOY_SSH_KEY=""
APP_ROOT="/opt/dukang"
STAGING_APP_ROOT="/opt/dukang-staging"
PROD_APP_ROOT="/opt/dukang"
TARGET="${1:-production}"
TARGET="${1:-}"
case "$TARGET" in
production|prod) SRC_ENV="$REPO_ROOT/server/dukang-api/.env.production" ;;
staging|stage|test)
TARGET=staging
SRC_ENV="$REPO_ROOT/server/dukang-api/.env.staging"
REMOTE_NAME=".env.staging"
PM2_NAME=dukang-stg-api
HEALTH_PORT=8190
;;
production|prod)
TARGET=production
SRC_ENV="$REPO_ROOT/server/dukang-api/.env.production"
REMOTE_NAME=".env.production"
PM2_NAME=dukang-api
HEALTH_PORT=8090
;;
development|dev)
echo "错误: development 仅用于本地开发,请使用 .env / .env.development,不同步到服务器" >&2
echo "错误: development 仅用于本地开发,请使用 .env,不同步到服务器" >&2
exit 1
;;
*)
echo "用法: $0 [production]" >&2
echo "用法: $0 staging|production" >&2
exit 1
;;
esac
@@ -31,8 +46,14 @@ if [[ -f "$ENV_FILE" ]]; then
source "$ENV_FILE"
fi
if [[ "$TARGET" == "staging" ]]; then
APP_ROOT="${STAGING_APP_ROOT:-/opt/dukang-staging}"
else
APP_ROOT="${PROD_APP_ROOT:-/opt/dukang}"
fi
if [[ ! -f "$SRC_ENV" ]]; then
echo "错误: 未找到 $SRC_ENV(可从 .env.production.example 复制)" >&2
echo "错误: 未找到 $SRC_ENV(可从 ${SRC_ENV}.example 复制)" >&2
exit 1
fi
@@ -46,7 +67,8 @@ SCP_OPTS=(-o "StrictHostKeyChecking=accept-new" -P "$DEPLOY_PORT")
[[ -n "$DEPLOY_SSH_KEY" ]] && SSH_OPTS+=(-i "$DEPLOY_SSH_KEY") && SCP_OPTS+=(-i "$DEPLOY_SSH_KEY")
REMOTE="${DEPLOY_USER}@${DEPLOY_HOST}"
echo "==> 同步 production 环境到 $REMOTE:$APP_ROOT/server/dukang-api/.env.production"
scp "${SCP_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 "==> 同步 $TARGET 环境到 $REMOTE:$APP_ROOT/server/dukang-api/$REMOTE_NAME"
scp "${SCP_OPTS[@]}" "$SRC_ENV" "$REMOTE:$APP_ROOT/server/dukang-api/$REMOTE_NAME"
ssh "${SSH_OPTS[@]}" "$REMOTE" \
"pm2 restart $PM2_NAME && sleep 2 && curl -sf -o /dev/null -w 'api-health:%{http_code}\n' http://127.0.0.1:${HEALTH_PORT}/api/v1/health"
echo "==> 完成"