This commit is contained in:
71
scripts/deploy.sh
Normal file
71
scripts/deploy.sh
Normal file
@@ -0,0 +1,71 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
用法:
|
||||
./scripts/deploy.sh <backend_dir>
|
||||
|
||||
示例:
|
||||
./scripts/deploy.sh /work/server-node
|
||||
|
||||
说明:
|
||||
1. 进入指定后端目录
|
||||
2. 构建后端
|
||||
3. 重启已有的 genarrative-server
|
||||
4. 如果 PM2 进程不存在,则使用 ecosystem.config.cjs 创建
|
||||
|
||||
注意:
|
||||
- 不会执行 git pull
|
||||
- 不会同步文件
|
||||
- 不会构建前端
|
||||
EOF
|
||||
}
|
||||
|
||||
require_command() {
|
||||
local command_name="$1"
|
||||
|
||||
if ! command -v "$command_name" >/dev/null 2>&1; then
|
||||
echo "[deploy] 缺少命令: $command_name" >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
BACKEND_DIR="${1:-}"
|
||||
|
||||
if [[ -z "${BACKEND_DIR}" || "${BACKEND_DIR}" == "-h" || "${BACKEND_DIR}" == "--help" ]]; then
|
||||
usage
|
||||
if [[ -z "${BACKEND_DIR}" ]]; then
|
||||
exit 1
|
||||
fi
|
||||
exit 0
|
||||
fi
|
||||
|
||||
require_command npm
|
||||
require_command pm2
|
||||
|
||||
if [[ ! -d "${BACKEND_DIR}" ]]; then
|
||||
echo "[deploy] 后端目录不存在: ${BACKEND_DIR}" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [[ ! -f "${BACKEND_DIR}/ecosystem.config.cjs" ]]; then
|
||||
echo "[deploy] 缺少 PM2 配置文件: ${BACKEND_DIR}/ecosystem.config.cjs" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "[deploy] 后端目录: ${BACKEND_DIR}"
|
||||
|
||||
cd "${BACKEND_DIR}"
|
||||
|
||||
# 重新构建后端产物。
|
||||
echo "[deploy] 构建后端"
|
||||
npm run build
|
||||
|
||||
# 优先重启;如果进程还不存在,就直接创建。
|
||||
echo "[deploy] 重启或创建 PM2 服务"
|
||||
pm2 restart genarrative-server --update-env \
|
||||
|| pm2 start ecosystem.config.cjs
|
||||
|
||||
echo "[deploy] 完成"
|
||||
76
scripts/update.sh
Normal file
76
scripts/update.sh
Normal file
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
用法:
|
||||
./scripts/update.sh
|
||||
|
||||
说明:
|
||||
1. 对当前仓库执行 git pull
|
||||
2. 只构建前端
|
||||
3. 固定同步前端 dist 到 /work/dist
|
||||
4. 固定同步 server-node 到 /work/server-node
|
||||
|
||||
注意:
|
||||
- server-node 同步时会排除 dist 和 node_modules
|
||||
- 不会构建后端
|
||||
- 不会执行 npm ci
|
||||
- 不会重启 PM2
|
||||
EOF
|
||||
}
|
||||
|
||||
require_command() {
|
||||
local command_name="$1"
|
||||
|
||||
if ! command -v "$command_name" >/dev/null 2>&1; then
|
||||
echo "[update] 缺少命令: $command_name" >&2
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
|
||||
usage
|
||||
exit 0
|
||||
fi
|
||||
|
||||
require_command git
|
||||
require_command npm
|
||||
require_command rsync
|
||||
|
||||
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
|
||||
REPO_ROOT="$(cd -- "${SCRIPT_DIR}/.." && pwd)"
|
||||
CLIENT_TARGET_DIR="/work/dist"
|
||||
SERVER_TARGET_DIR="/work/server-node"
|
||||
|
||||
echo "[update] 仓库目录: ${REPO_ROOT}"
|
||||
echo "[update] 前端目标目录: ${CLIENT_TARGET_DIR}"
|
||||
echo "[update] 后端目标目录: ${SERVER_TARGET_DIR}"
|
||||
|
||||
cd "${REPO_ROOT}"
|
||||
|
||||
# 先拉取当前分支的最新代码。
|
||||
echo "[update] 拉取当前分支最新代码"
|
||||
git pull
|
||||
|
||||
# 只构建前端,不处理后端构建。
|
||||
echo "[update] 构建前端"
|
||||
npm run build
|
||||
|
||||
# 固定创建 /work 下的目标目录。
|
||||
echo "[update] 创建目标目录"
|
||||
mkdir -p "${CLIENT_TARGET_DIR}" "${SERVER_TARGET_DIR}"
|
||||
|
||||
# 同步前端构建产物。
|
||||
echo "[update] 同步前端 dist -> ${CLIENT_TARGET_DIR}"
|
||||
rsync -a --delete "${REPO_ROOT}/dist/" "${CLIENT_TARGET_DIR}/"
|
||||
|
||||
# 同步 server-node 源码和配置,但保留目标目录自己的 dist 和 node_modules。
|
||||
echo "[update] 同步 server-node -> ${SERVER_TARGET_DIR}"
|
||||
rsync -a --delete \
|
||||
--exclude 'dist/' \
|
||||
--exclude 'node_modules/' \
|
||||
"${REPO_ROOT}/server-node/" "${SERVER_TARGET_DIR}/"
|
||||
|
||||
echo "[update] 完成"
|
||||
18
server-node/ecosystem.config.cjs
Normal file
18
server-node/ecosystem.config.cjs
Normal file
@@ -0,0 +1,18 @@
|
||||
module.exports = {
|
||||
apps: [
|
||||
{
|
||||
name: 'genarrative-server',
|
||||
script: 'dist/server.js',
|
||||
cwd: __dirname,
|
||||
instances: 1,
|
||||
exec_mode: 'fork',
|
||||
watch: false,
|
||||
env: {
|
||||
NODE_ENV: 'production',
|
||||
},
|
||||
error_file: 'logs/error.log',
|
||||
out_file: 'logs/out.log',
|
||||
time: true,
|
||||
},
|
||||
],
|
||||
};
|
||||
Reference in New Issue
Block a user