This commit is contained in:
whm
2026-03-18 18:11:54 +08:00
parent c9d9224a68
commit 07f55e0139
3 changed files with 52 additions and 13 deletions

50
restart.sh Normal file → Executable file
View File

@@ -1,6 +1,6 @@
#!/usr/bin/env bash
# 直接重启缺什么自动安装curl、Docker、Docker Compose再 docker compose 重启,不拉代码
# 用法cd 项目根 && chmod +x restart.sh && ./restart.sh
# 仅不拉代码,其余与 pull-and-restart.sh 一致:构建到 deploy/ 并重启
# 用法cd 项目根 && ./restart.sh拉取后可直接执行无需 chmod
# 行尾LF
set -e
ROOT="${PROJECT_ROOT:-$(cd "$(dirname "$0")" && pwd)}"
@@ -166,7 +166,7 @@ compose_cmd() {
run_sudo env REGISTRY_MIRROR="${REGISTRY_MIRROR}" GOPROXY="${GOPROXY}" $COMPOSE_CMD "$@"
}
echo "重启 yh_web ($ROOT)..."
echo "重启 yh_web ($ROOT)(不拉代码,仅构建并启动)..."
# 环境配置:缺失时从 server/.env.example 复制
if [ ! -f server/.env ]; then
@@ -176,14 +176,48 @@ if [ ! -f server/.env ]; then
else
mkdir -p server
ND="${NGINX_DOMAIN:-yuheng.yuxindazhineng.com}"
printf 'MONGODB_URI=mongodb://mongo:27017\nMONGODB_DB=yxd-agent-testing\nPORT=9527\nGIN_MODE=release\nALLOWED_ORIGINS=https://%s\n' "$ND" > server/.env
printf 'MONGODB_URI=mongodb://mongo:27017\nMONGODB_DB=yxd-agent-testing\nPORT=8088\nGIN_MODE=release\nALLOWED_ORIGINS=https://%s\n' "$ND" > server/.env
echo "已创建默认 server/.env"
fi
fi
[ -f server/.env ] && sed -i 's/\r$//' server/.env
[ -f server/.env ] && sed -i 's/\r$//' server/.env 2>/dev/null || true
[ -f server/.env ] && set -a && source server/.env && set +a
export GOPROXY="${GOPROXY:-https://goproxy.cn,direct}"
export REGISTRY_MIRROR="${REGISTRY_MIRROR:-docker.m.daocloud.io/library/}"
# 与 pull-and-restart 一致:宿主机 9527 检查
if grep -q '9527' "$ROOT/docker-compose.yml" 2>/dev/null; then
echo "错误: docker-compose.yml 仍含 9527会与 sshd 冲突。请拉取最新代码后再执行。" >&2
exit 1
fi
# 构建到 deploy/(与 pull-and-restart.sh 相同,仅无 git 拉取)
mkdir -p "$ROOT/deploy/web/dist" "$ROOT/deploy/admin/dist" "$ROOT/deploy/api"
echo "构建 web 前端 -> deploy/web/dist ..."
run_sudo docker run --rm -v "$ROOT/web:/app" -v "$ROOT/deploy/web/dist:/out" -w /app \
"${REGISTRY_MIRROR}node:20-alpine" sh -c "rm -rf /out/* 2>/dev/null; (npm ci --legacy-peer-deps 2>/dev/null || npm install --legacy-peer-deps) && npm run build && cp -r dist/. /out/"
echo "构建 admin 前端 -> deploy/admin/dist ..."
run_sudo docker run --rm -v "$ROOT/admin:/app" -v "$ROOT/deploy/admin/dist:/out" -w /app \
"${REGISTRY_MIRROR}node:20-alpine" sh -c "rm -rf /out/* 2>/dev/null; (npm ci --legacy-peer-deps 2>/dev/null || npm install --legacy-peer-deps) && npm run build && cp -r dist/. /out/"
echo "构建 api 二进制 -> deploy/api/server ..."
run_sudo docker run --rm -v "$ROOT/server:/src" -v "$ROOT/deploy/api:/out" -w /src -e GOPROXY="${GOPROXY}" \
"${REGISTRY_MIRROR}golang:1.21-alpine" sh -c "go build -mod=vendor -o /out/server ."
run_sudo chmod -R a+rX "$ROOT/deploy/web/dist" "$ROOT/deploy/admin/dist" 2>/dev/null || true
if [ ! -f "$ROOT/deploy/web/dist/index.html" ] || [ ! -f "$ROOT/deploy/admin/dist/index.html" ]; then
echo "错误: 构建产物不完整(缺少 index.html请检查上方构建日志。" >&2
exit 1
fi
compose_cmd build api
MONGO_IMAGE="${REGISTRY_MIRROR}mongo:7"
if ! run_sudo docker image inspect "$MONGO_IMAGE" >/dev/null 2>&1; then
echo "拉取 mongo 镜像(仅首次或镜像缺失时)..."
run_sudo docker pull "$MONGO_IMAGE" || true
else
echo "mongo 镜像已存在,跳过拉取."
fi
NGINX_DOMAIN="${NGINX_DOMAIN:-yuheng.yuxindazhineng.com}"
NGINX_SSL_DIR="/etc/ssl/yh_web/$NGINX_DOMAIN"
NGINX_CONF_NAME="${NGINX_DOMAIN}.conf"
@@ -195,15 +229,15 @@ if [ -f "$ROOT/nginx/$NGINX_DOMAIN.pem" ] && [ -f "$ROOT/nginx/$NGINX_DOMAIN.key
run_sudo chmod 600 "$NGINX_SSL_DIR/privkey.pem"
elif [ -f "$ROOT/nginx/fullchain.pem" ] && [ -f "$ROOT/nginx/privkey.pem" ]; then
run_sudo cp -f "$ROOT/nginx/fullchain.pem" "$ROOT/nginx/privkey.pem" "$NGINX_SSL_DIR/"
run_sudo chmod 644 "$NGINX_SSL_DIR/fullchain.pem"
run_sudo chmod 600 "$NGINX_SSL_DIR/privkey.pem" 2>/dev/null || true
elif [ -f "$ROOT/nginx/$NGINX_DOMAIN/fullchain.pem" ] && [ -f "$ROOT/nginx/$NGINX_DOMAIN/privkey.pem" ]; then
run_sudo cp -f "$ROOT/nginx/$NGINX_DOMAIN/fullchain.pem" "$ROOT/nginx/$NGINX_DOMAIN/privkey.pem" "$NGINX_SSL_DIR/"
run_sudo chmod 644 "$NGINX_SSL_DIR/fullchain.pem"
run_sudo chmod 600 "$NGINX_SSL_DIR/privkey.pem" 2>/dev/null || true
fi
[ -f "$NGINX_SSL_DIR/fullchain.pem" ] && [ -f "$NGINX_SSL_DIR/privkey.pem" ] && echo "已同步证书到 $NGINX_SSL_DIR"
compose_cmd down 2>/dev/null || true
run_sudo docker pull "${REGISTRY_MIRROR}mongo:7" 2>/dev/null || true
compose_cmd up -d
compose_cmd up -d --force-recreate
if [ -f "$ROOT/nginx/$NGINX_CONF_NAME" ]; then
run_sudo cp -f "$ROOT/nginx/$NGINX_CONF_NAME" /etc/nginx/conf.d/ 2>/dev/null || true