diff --git a/backend/.env.prod.example b/backend/.env.prod.example index cfaa9a3..90fcb7d 100644 --- a/backend/.env.prod.example +++ b/backend/.env.prod.example @@ -9,6 +9,8 @@ CADDY_EMAIL=admin@example.com # CADDY_SHOW_DOMAIN=show.example.com # 选号网源码目录(相对 hfb_sys 根或绝对路径),默认 ../hfb_show # HFB_SHOW_DIR=../hfb_show +# 选号网 npm 源(国内服务器默认用 npmmirror,避免 registry.npmjs.org 超时) +# NPM_REGISTRY=https://registry.npmmirror.com # MySQL 容器初始化变量,同时供后端 DSN 使用。 MYSQL_ROOT_PASSWORD=change-root-password diff --git a/scripts/deploy-prod.sh b/scripts/deploy-prod.sh index ffe7eb3..f967971 100755 --- a/scripts/deploy-prod.sh +++ b/scripts/deploy-prod.sh @@ -278,34 +278,43 @@ prepare_show_site() { fi log_warn "使用已有 show-dist(--no-build)" else - # 服务器通常无本机 Node:优先本机 npm,否则用 Docker 内 node 镜像构建 - log "构建选号网静态资源:${show_dir}" + # 服务器通常无本机 Node:优先本机 npm,否则用 Docker 内 node 镜像构建。 + # 国内机访问 registry.npmjs.org 易 ECONNRESET,默认走 npmmirror,可用 NPM_REGISTRY 覆盖。 + local npm_registry npm_install + npm_registry="$(env_value NPM_REGISTRY || true)" + if [[ -z "${npm_registry}" ]]; then + npm_registry="${NPM_REGISTRY:-https://registry.npmmirror.com}" + fi + if [[ -f "${show_dir}/package-lock.json" ]]; then + npm_install="npm ci" + else + npm_install="npm install" + fi + log "构建选号网静态资源:${show_dir}(registry=${npm_registry})" if command -v npm >/dev/null 2>&1; then ( cd "${show_dir}" - if [[ -f package-lock.json ]]; then - npm ci - else - npm install - fi + ${npm_install} \ + --registry="${npm_registry}" \ + --fetch-retries=5 \ + --fetch-retry-mintimeout=20000 \ + --fetch-retry-maxtimeout=120000 npm run build ) else log_warn "本机无 npm,使用 Docker 镜像 node:24-alpine 构建选号网(无需安装 Node)" need_cmd docker - local npm_cmd="npm ci && npm run build" - if [[ ! -f "${show_dir}/package-lock.json" ]]; then - npm_cmd="npm install && npm run build" - fi # 挂载源码目录;node_modules/dist 写在挂载卷上,与本机 npm 行为一致 docker run --rm \ + -e npm_config_registry="${npm_registry}" \ -v "${show_dir}:/app" \ -w /app \ node:24-alpine \ - sh -c "${npm_cmd}" + sh -c "${npm_install} --registry=${npm_registry} --fetch-retries=5 --fetch-retry-mintimeout=20000 --fetch-retry-maxtimeout=120000 && npm run build" fi if [[ ! -d "${show_dir}/dist" ]]; then log_error "选号网构建失败:未生成 dist/" + log_error "若仍网络失败,可在 backend/.env 设置 NPM_REGISTRY=https://registry.npmmirror.com 后重试" exit 1 fi rm -rf "${show_dist}"