diff --git a/.dockerignore b/.dockerignore index 332da2e..b1417ea 100644 --- a/.dockerignore +++ b/.dockerignore @@ -9,7 +9,8 @@ .idea node_modules -dist +# 勿排除 dist:宿主机构建 Dockerfile / Dockerfile.k3s.runtime 需要 COPY dist/ +# Kaniko 多阶段会在镜像内重新 pnpm build,带上旧 dist 无妨 coverage *.log npm-debug.log* diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 0906f6e..900802f 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -1,8 +1,6 @@ # 复制到 itfx-front 仓库 .gitea/workflows/deploy.yml # push master → prod;push test → test -# 构建始终用 prod kubeconfig(Kaniko);仅 deploy 切集群 -# 前端镜像:test 分支须传 FRONT_BUILD_SCRIPT=build:test,否则会打进正式 API 域名 -# 构建变慢时:确认宿主机 ci-publish-front.sh 已给 Kaniko --cache=true(lock 不变时应跳过 pnpm install) +# 构建:runner 宿主 pnpm + docker(ci-publish-front.sh);仅 deploy 切集群 name: Build and Deploy Front on: diff --git a/Dockerfile b/Dockerfile index 0112129..f6cc94f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,6 @@ -# 本地/简易 Docker:仅静态 SPA。HTTPS 由外层网关(如 Traefik)终结。 -# 构建前先执行:pnpm build -FROM docker.m.daocloud.io/library/node:20-alpine -WORKDIR /app -RUN npm config set registry https://registry.npmmirror.com \ - && npm install -g serve@14.2.4 -COPY dist/ ./dist/ +# 本地/简易:先 pnpm build,再 docker build +# HTTPS 由外层网关(Traefik)终结 +FROM docker.m.daocloud.io/library/nginx:1.27-alpine +COPY dist/ /usr/share/nginx/html/ +COPY nginx-k3s.conf /etc/nginx/conf.d/default.conf EXPOSE 80 -CMD ["serve", "-s", "dist", "-l", "80"] diff --git a/Dockerfile.k3s b/Dockerfile.k3s index 97e3896..4b09171 100644 --- a/Dockerfile.k3s +++ b/Dockerfile.k3s @@ -1,43 +1,17 @@ -# k3s / Kaniko:静态 SPA(方案 A)。TLS 由 Traefik(websecure) + cert-manager 终结。 -# 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 - -# ---------- 前端构建 ---------- +# k3s / Kaniko 备用:多阶段构建。生产 CI 优先宿主 pnpm + Dockerfile.k3s.runtime +# TLS 由 Traefik(websecure) + cert-manager 终结;API 浏览器直连。 FROM docker.m.daocloud.io/library/node:20-alpine AS build WORKDIR /app ARG FRONT_BUILD_SCRIPT=build - -# corepack 启用内置 pnpm,避免每次 npm install -g RUN corepack enable \ && corepack prepare pnpm@9.15.9 --activate \ && pnpm config set registry https://registry.npmmirror.com - COPY package.json pnpm-lock.yaml ./ -# --ignore-scripts:跳过 gifsicle/optipng 等原生编译(需 autoreconf;vite 未启用 imagemin) -# 先只装依赖,lock 不变时可命中 Kaniko 层缓存(最大收益) RUN pnpm install --frozen-lockfile --ignore-scripts - COPY . . RUN pnpm run ${FRONT_BUILD_SCRIPT} -# ---------- 最终镜像:只拷 serve + dist ---------- -FROM docker.m.daocloud.io/library/node:20-alpine -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 +FROM docker.m.daocloud.io/library/nginx:1.27-alpine +COPY --from=build /app/dist /usr/share/nginx/html/ +COPY nginx-k3s.conf /etc/nginx/conf.d/default.conf EXPOSE 80 -CMD ["serve", "-s", "dist", "-l", "80"] diff --git a/Dockerfile.k3s.runtime b/Dockerfile.k3s.runtime new file mode 100644 index 0000000..20b48af --- /dev/null +++ b/Dockerfile.k3s.runtime @@ -0,0 +1,5 @@ +# 宿主机构建 dist 后只打 nginx 静态层(CI:ci-publish-front.sh) +FROM docker.m.daocloud.io/library/nginx:1.27-alpine +COPY dist/ /usr/share/nginx/html/ +COPY nginx-k3s.conf /etc/nginx/conf.d/default.conf +EXPOSE 80 diff --git a/nginx-k3s.conf b/nginx-k3s.conf new file mode 100644 index 0000000..64837cf --- /dev/null +++ b/nginx-k3s.conf @@ -0,0 +1,68 @@ +# 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; + } +}