Compare commits

..

No commits in common. "5ae57745bec207fd19750d4e9a7390cb8f3f4645" and "dead13a052deb6f8dc87e4dbc9bc5e52614912c8" have entirely different histories.

3 changed files with 39 additions and 78 deletions

View File

@ -1,6 +1,9 @@
# 本地/简易:先 pnpm build,再 docker build # 本地/简易 Docker:仅静态 SPA。HTTPS 由外层网关(如 Traefik)终结。
# HTTPS 由外层网关(Traefik)终结 # 构建前先执行:pnpm build
FROM docker.m.daocloud.io/library/nginx:1.27-alpine FROM docker.m.daocloud.io/library/node:20-alpine
COPY dist/ /usr/share/nginx/html/ WORKDIR /app
COPY nginx-k3s.conf /etc/nginx/conf.d/default.conf RUN npm config set registry https://registry.npmmirror.com \
&& npm install -g serve@14.2.4
COPY dist/ ./dist/
EXPOSE 80 EXPOSE 80
CMD ["serve", "-s", "dist", "-l", "80"]

View File

@ -1,17 +1,43 @@
# k3s / Kaniko 备用:多阶段构建。生产 CI 优先宿主 pnpm + Dockerfile.k3s.runtime # k3s / Kaniko:静态 SPA(方案 A)。TLS 由 Traefik(websecure) + cert-manager 终结。
# TLS 由 Traefik(websecure) + cert-manager 终结;API 浏览器直连。 # API 由浏览器按站点域名直连:
# test.ideship.com → api.test.ideship.com;其余 → api.ideship.com
# FRONT_BUILD_SCRIPT:
# prod 默认 build(.env.production → api.ideship.com)
# test 传 build:test(.env.ideship-test → api.test.ideship.com)
# 即使误用 production 构建,运行时也会按 hostname 纠正到测试 API。
#
# 构建耗时提示(宿主机 ci-publish-front.sh):
# 务必给 Kaniko 开 --cache=true(及 cache-repo / cache-ttl),
# 否则每次都会全量 pnpm install + vite build(本地 vite alone 约 3~4 分钟)。
# ---------- 运行时 serve:与源码无关,Kaniko 有 cache 时几乎秒过 ----------
FROM docker.m.daocloud.io/library/node:20-alpine AS serve-runtime
RUN npm config set registry https://registry.npmmirror.com \
&& npm install -g serve@14.2.4
# ---------- 前端构建 ----------
FROM docker.m.daocloud.io/library/node:20-alpine AS build FROM docker.m.daocloud.io/library/node:20-alpine AS build
WORKDIR /app WORKDIR /app
ARG FRONT_BUILD_SCRIPT=build ARG FRONT_BUILD_SCRIPT=build
# corepack 启用内置 pnpm,避免每次 npm install -g
RUN corepack enable \ RUN corepack enable \
&& corepack prepare pnpm@9.15.9 --activate \ && corepack prepare pnpm@9.15.9 --activate \
&& pnpm config set registry https://registry.npmmirror.com && pnpm config set registry https://registry.npmmirror.com
COPY package.json pnpm-lock.yaml ./ COPY package.json pnpm-lock.yaml ./
# --ignore-scripts:跳过 gifsicle/optipng 等原生编译(需 autoreconf;vite 未启用 imagemin)
# 先只装依赖,lock 不变时可命中 Kaniko 层缓存(最大收益)
RUN pnpm install --frozen-lockfile --ignore-scripts RUN pnpm install --frozen-lockfile --ignore-scripts
COPY . . COPY . .
RUN pnpm run ${FRONT_BUILD_SCRIPT} RUN pnpm run ${FRONT_BUILD_SCRIPT}
FROM docker.m.daocloud.io/library/nginx:1.27-alpine # ---------- 最终镜像:只拷 serve + dist ----------
COPY --from=build /app/dist /usr/share/nginx/html/ FROM docker.m.daocloud.io/library/node:20-alpine
COPY nginx-k3s.conf /etc/nginx/conf.d/default.conf WORKDIR /app
COPY --from=serve-runtime /usr/local/lib/node_modules/serve /usr/local/lib/node_modules/serve
COPY --from=serve-runtime /usr/local/bin/serve /usr/local/bin/serve
COPY --from=build /app/dist ./dist
EXPOSE 80 EXPOSE 80
CMD ["serve", "-s", "dist", "-l", "80"]

View File

@ -1,68 +0,0 @@
# k3s 方案 A:仅静态 SPA。API 由浏览器按域名直连;TLS 由 Traefik 终结。
server {
listen 80;
server_name _;
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml image/svg+xml;
root /usr/share/nginx/html;
index index.html;
# 官网静态(若存在);否则回退 SPA
location = / {
try_files /site/index.html /index.html =404;
add_header Cache-Control "no-cache, no-store, must-revalidate";
add_header Pragma "no-cache";
add_header Expires "0";
}
location = /site {
return 301 /;
}
location = /site/ {
return 301 /;
}
location = /site/index.html {
return 301 /;
}
location ^~ /site/ {
try_files $uri =404;
expires 7d;
add_header Cache-Control "public";
access_log off;
}
location = /favicon.ico {
try_files /favicon.png /favicon.ico =404;
expires 7d;
add_header Cache-Control "public";
access_log off;
}
location = /index.html {
add_header Cache-Control "no-cache, no-store, must-revalidate";
add_header Pragma "no-cache";
add_header Expires "0";
}
# 微信域名校验文件
location ~* ^/MP_verify_[A-Za-z0-9]+\.txt$ {
try_files $uri =404;
add_header Cache-Control "no-cache";
}
location ~* \.(?:js|css|woff2?|ttf|eot|svg|png|jpe?g|gif|webp|ico|map)$ {
try_files $uri =404;
expires 7d;
add_header Cache-Control "public";
access_log off;
}
# SPA 路由回退
location / {
try_files $uri $uri/ /index.html;
}
}