选号网构建默认使用npmmirror并增加npm重试

缓解国内服务器访问registry.npmjs.org出现ECONNRESET的问题。
This commit is contained in:
yml2213
2026-07-16 22:34:42 +08:00
parent 2c1cdb0339
commit 3dfa43d3d0
2 changed files with 23 additions and 12 deletions
+2
View File
@@ -9,6 +9,8 @@ CADDY_EMAIL=admin@example.com
# CADDY_SHOW_DOMAIN=show.example.com # CADDY_SHOW_DOMAIN=show.example.com
# 选号网源码目录(相对 hfb_sys 根或绝对路径),默认 ../hfb_show # 选号网源码目录(相对 hfb_sys 根或绝对路径),默认 ../hfb_show
# HFB_SHOW_DIR=../hfb_show # HFB_SHOW_DIR=../hfb_show
# 选号网 npm 源(国内服务器默认用 npmmirror,避免 registry.npmjs.org 超时)
# NPM_REGISTRY=https://registry.npmmirror.com
# MySQL 容器初始化变量,同时供后端 DSN 使用。 # MySQL 容器初始化变量,同时供后端 DSN 使用。
MYSQL_ROOT_PASSWORD=change-root-password MYSQL_ROOT_PASSWORD=change-root-password
+21 -12
View File
@@ -278,34 +278,43 @@ prepare_show_site() {
fi fi
log_warn "使用已有 show-dist--no-build" log_warn "使用已有 show-dist--no-build"
else else
# 服务器通常无本机 Node:优先本机 npm,否则用 Docker 内 node 镜像构建 # 服务器通常无本机 Node:优先本机 npm,否则用 Docker 内 node 镜像构建
log "构建选号网静态资源:${show_dir}" # 国内机访问 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 if command -v npm >/dev/null 2>&1; then
( (
cd "${show_dir}" cd "${show_dir}"
if [[ -f package-lock.json ]]; then ${npm_install} \
npm ci --registry="${npm_registry}" \
else --fetch-retries=5 \
npm install --fetch-retry-mintimeout=20000 \
fi --fetch-retry-maxtimeout=120000
npm run build npm run build
) )
else else
log_warn "本机无 npm,使用 Docker 镜像 node:24-alpine 构建选号网(无需安装 Node" log_warn "本机无 npm,使用 Docker 镜像 node:24-alpine 构建选号网(无需安装 Node"
need_cmd docker 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 行为一致 # 挂载源码目录;node_modules/dist 写在挂载卷上,与本机 npm 行为一致
docker run --rm \ docker run --rm \
-e npm_config_registry="${npm_registry}" \
-v "${show_dir}:/app" \ -v "${show_dir}:/app" \
-w /app \ -w /app \
node:24-alpine \ 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 fi
if [[ ! -d "${show_dir}/dist" ]]; then if [[ ! -d "${show_dir}/dist" ]]; then
log_error "选号网构建失败:未生成 dist/" log_error "选号网构建失败:未生成 dist/"
log_error "若仍网络失败,可在 backend/.env 设置 NPM_REGISTRY=https://registry.npmmirror.com 后重试"
exit 1 exit 1
fi fi
rm -rf "${show_dist}" rm -rf "${show_dist}"