From 61639c5a2d5395c9b7e09a6ae0786fa995f6896a Mon Sep 17 00:00:00 2001 From: xiaoxia Date: Fri, 17 Jul 2026 00:38:30 +0800 Subject: [PATCH] =?UTF-8?q?feat(ci):=20=E5=89=8D=E7=AB=AF=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E7=BC=93=E5=AD=98=E4=BC=98=E5=8C=96=20-=20=E5=8E=BB?= =?UTF-8?q?=E6=8E=89=E9=87=8D=E5=A4=8D=E6=9E=84=E5=BB=BA+BuildKit=20cache?= =?UTF-8?q?=20mount?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 删除host上重复的Build frontend assets步骤(Dockerfile中已构建) - web.Dockerfile加BuildKit cache mount: * /root/.npm: npm下载缓存 * node_modules: 依赖包缓存,避免重复安装 * .tscache: TypeScript增量编译缓存 - 预计Web构建耗时降低40%-60% --- .gitea/workflows/ci-build.yml | 8 -------- infra/docker/web.Dockerfile | 19 +++++++++++++++++-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/.gitea/workflows/ci-build.yml b/.gitea/workflows/ci-build.yml index 09e6cc1ec..69a45b7b5 100644 --- a/.gitea/workflows/ci-build.yml +++ b/.gitea/workflows/ci-build.yml @@ -119,10 +119,6 @@ jobs: - name: Setup cache strategy shell: sh run: "set -eu\n# develop/main 分支写回缓存,其他分支只读\nif [ \"${GITHUB_REF_NAME}\" = \"develop\" ] || [ \"${GITHUB_REF_NAME}\" = \"main\" ]; then\n echo \"CACHE_MODE=read-write\" >> $GITHUB_ENV\n echo \"Cache mode: read-write (will push cache)\"\nelse\n echo \"CACHE_MODE=read-only\" >> $GITHUB_ENV\n echo \"Cache mode: read-only\"\nfi\n" - - name: Build frontend assets (npm build) - if: matrix.service == 'web' - shell: sh - run: "set -eu\nNPM_CACHE_VOLUME=\"xiaoxia-npm-cache\"\nif ! docker volume inspect \"$NPM_CACHE_VOLUME\" >/dev/null 2>&1; then\n docker volume create \"$NPM_CACHE_VOLUME\" >/dev/null\n echo \"Created npm cache volume: $NPM_CACHE_VOLUME\"\nfi\n\ndocker run --rm -v \"$PWD:/workspace\" -v \"$NPM_CACHE_VOLUME:/workspace/apps/web/node_modules\" -w /workspace/apps/web docker.m.daocloud.io/library/node:20 sh -lc \"npm ci && npx tsc --incremental --tsBuildInfoFile node_modules/.tsbuildinfo && npx vite build\"\n\ntest -f apps/web/dist/index.html\necho \"Frontend build complete: $(ls apps/web/dist/ | head -5)\"\n" - name: Setup buildx builder (docker-container driver) shell: sh run: "set -eu\n# 确保使用 docker-container driver 以支持 cache export 功能\nif ! docker buildx inspect ci-builder-${GITHUB_RUN_ID}-${GITHUB_JOB} > /dev/null 2>&1; then\n docker buildx create --use --name ci-builder-${GITHUB_RUN_ID}-${GITHUB_JOB} --driver docker-container\n echo \"Created ci-builder (docker-container driver)\"\nelse\n docker buildx use ci-builder-${GITHUB_RUN_ID}-${GITHUB_JOB}\n echo \"Using existing ci-builder\"\nfi\ndocker buildx inspect --bootstrap\n" @@ -553,10 +549,6 @@ jobs: echo "Docker login successful" ' - - name: Build frontend assets (npm build) - if: matrix.service == 'web' - shell: sh - run: "set -eu\nNPM_CACHE_VOLUME=\"xiaoxia-npm-cache\"\nif ! docker volume inspect \"$NPM_CACHE_VOLUME\" >/dev/null 2>&1; then\n docker volume create \"$NPM_CACHE_VOLUME\" >/dev/null\nfi\n\ndocker run --rm -v \"$PWD:/workspace\" -v \"$NPM_CACHE_VOLUME:/workspace/apps/web/node_modules\" -w /workspace/apps/web docker.m.daocloud.io/library/node:20 sh -lc \"npm ci && npx tsc --incremental --tsBuildInfoFile node_modules/.tsbuildinfo && npx vite build\"\n\ntest -f apps/web/dist/index.html\necho \"Frontend build complete\"\n" - name: Setup buildx builder (docker-container driver) shell: sh run: "set -eu\n# 确保使用 docker-container driver 以支持 cache export 功能\nif ! docker buildx inspect ci-builder-${GITHUB_RUN_ID}-${GITHUB_JOB} > /dev/null 2>&1; then\n docker buildx create --use --name ci-builder-${GITHUB_RUN_ID}-${GITHUB_JOB} --driver docker-container\n echo \"Created ci-builder (docker-container driver)\"\nelse\n docker buildx use ci-builder-${GITHUB_RUN_ID}-${GITHUB_JOB}\n echo \"Using existing ci-builder\"\nfi\ndocker buildx inspect --bootstrap\n" diff --git a/infra/docker/web.Dockerfile b/infra/docker/web.Dockerfile index 074825fd7..f800ed684 100755 --- a/infra/docker/web.Dockerfile +++ b/infra/docker/web.Dockerfile @@ -3,12 +3,27 @@ FROM git.xiaoxiajianji.com/xiaoxia/base/node:20 AS builder WORKDIR /app ARG VITE_API_URL=https://saas-api.xiaoxiajianji.com ENV VITE_API_URL=$VITE_API_URL + +# 先拷依赖清单(缓存友好:依赖不变时直接命中缓存层) COPY apps/web/package.json apps/web/package-lock.json ./apps/web/ WORKDIR /app/apps/web -RUN npm config set registry https://registry.npmmirror.com \ + +# 安装依赖:用BuildKit cache mount缓存npm下载和node_modules +# sharing=locked 防止并发构建竞争写缓存 +RUN --mount=type=cache,target=/root/.npm,sharing=locked \ + --mount=type=cache,target=/app/apps/web/node_modules,sharing=locked \ + npm config set registry https://registry.npmmirror.com \ && npm ci + +# 再拷源码 COPY apps/web/ ./ -RUN npm run build + +# 构建:TS增量编译 + Vite构建,tsbuildinfo用cache mount持久化 +RUN --mount=type=cache,target=/app/apps/web/node_modules,sharing=locked \ + --mount=type=cache,target=/app/apps/web/.tscache,sharing=locked \ + mkdir -p .tscache \ + && npx tsc --incremental --tsBuildInfoFile .tscache/tsconfig.tsbuildinfo \ + && npx vite build # Production stage with nginx FROM git.xiaoxiajianji.com/xiaoxia/base/nginx:alpine AS runner -- 2.54.0