fix(ci): Web 镜像 buildx 幽灵缓存防护——Git Tree Hash Cache Bust
CI/CD Pipeline / Dedup Check - skip PR tests when covered by push pipeline (pull_request) Successful in 1s
CI/CD Pipeline / Check if frontend-only change (pull_request) Successful in 1s
CI/CD Pipeline / Check push changed paths (pull_request) Has been skipped
AI Code Review / AI Code Review (pull_request) Successful in 3m26s
PR Automation / Auto Merge on CI Green + Approved (pull_request) Successful in 2m51s
Preview Deploy / Deploy Preview Environment (pull_request) Successful in 3m51s
PR Automation / Auto Approve on CI Green (pull_request) Successful in 4m44s
CI/CD Pipeline / Frontend Lint (pull_request) Has been skipped
CI/CD Pipeline / Frontend Unit Tests (pull_request) Has been skipped
CI/CD Pipeline / PR Build Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Build Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging API Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Worker Image (pull_request) Has been skipped
CI/CD Pipeline / Retag skipped Staging Web Image (pull_request) Has been skipped
CI/CD Pipeline / Validate - Python (mypy + alembic) (pull_request) Successful in 8m45s
CI/CD Pipeline / PR Build API Image (pull_request) Successful in 3m10s
CI/CD Pipeline / PR Build Worker Image (pull_request) Successful in 3m15s
CI/CD Pipeline / Validate - Style (pull_request) Has been cancelled
CI/CD Pipeline / Validate - Security (pull_request) Has been cancelled
CI/CD Pipeline / Unit Tests (pull_request) Has been cancelled
CI/CD Pipeline / Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Staging (Watchtower auto-deploy) (pull_request) Has been cancelled
CI/CD Pipeline / Staging E2E Tests (pull_request) Has been cancelled
CI/CD Pipeline / Staging API Integration Tests (pull_request) Has been cancelled
CI/CD Pipeline / Build Production API Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Web Image (pull_request) Has been cancelled
CI/CD Pipeline / Build Production Worker Image (pull_request) Has been cancelled
CI/CD Pipeline / Deploy Production (pull_request) Has been cancelled
CI/CD Pipeline / Production Browser E2E (pull_request) Has been cancelled
CI/CD Pipeline / ACR Image Cleanup (pull_request) Has been cancelled
CI/CD Pipeline / Canary Release to Production (pull_request) Has been cancelled
CI/CD Pipeline / CI Gate (pull_request) Has been cancelled

问题:ci-builder-persist 持久化 builder 的 buildkit 层缓存偶尔出现'幽灵命中':
COPY apps/web/ ./ 步骤认为自己没变(实际文件已改),导致 vite build 不执行,
打出来的 Web 镜像前端文件是旧的。

修复原理:
把 apps/web/ 目录的 git tree hash 作为 build arg 传入 Dockerfile。
buildx 把 build arg 值作为缓存键的一部分,hash 变了 → RUN 步骤缓存失效 →
vite build 必须重新执行。依赖层(npm ci)不受影响,仍然正常缓存。

改动:
1. web.Dockerfile:新增 ARG SOURCE_HASH,在构建步骤写入 .cache_bust 文件
2. docker_build_push.sh:Web 镜像构建时计算 git tree hash 并传入
3. docker_build_only.sh:同上

验证:
- 合并后触发 push 到 develop
- 查看 Build Staging Web Image 日志,确认输出 Web cache bust: SOURCE_HASH=xxxxx
- 修改 apps/web/ 下文件再次 push,确认 SOURCE_HASH 值变化且 vite build 执行
This commit is contained in:
xiaoxia
2026-08-31 21:09:01 +08:00
parent deb127ae08
commit f34b076dcd
3 changed files with 27 additions and 0 deletions
+3
View File
@@ -1,5 +1,6 @@
# Build stage
FROM git.xiaoxiajianji.com/xiaoxia/base/node:20 AS builder
ARG SOURCE_HASH=""
WORKDIR /app
ARG VITE_API_URL=https://saas-api.xiaoxiajianji.com
ENV VITE_API_URL=$VITE_API_URL
@@ -18,8 +19,10 @@ COPY apps/web/ ./
# 构建:TS增量编译 + Vite构建,tsbuildinfo用cache mount持久化
# node_modules直接使用镜像中已安装的(layer缓存保证完整性)
# SOURCE_HASH 变化时强制重新执行(防止 buildkit 幽灵缓存命中)
RUN --mount=type=cache,target=/app/apps/web/.tscache,sharing=locked \
mkdir -p .tscache \
&& echo "SOURCE_HASH=${SOURCE_HASH}" > .cache_bust \
&& ./node_modules/.bin/tsc --incremental --tsBuildInfoFile .tscache/tsconfig.tsbuildinfo \
&& ./node_modules/.bin/vite build
+12
View File
@@ -19,6 +19,18 @@ for arg in "$@"; do
BUILD_ARGS="$BUILD_ARGS --build-arg $arg"
done
# Web 镜像 cache bust:计算 apps/web/ 的 git tree hash
# 当源码变化时 hash 变化,buildx 的 ARG 缓存键失效 → vite build 必定重新执行
if [ "${DOCKERFILE##*/}" = "web.Dockerfile" ]; then
SOURCE_HASH=$(git rev-parse HEAD:apps/web 2>/dev/null || echo "")
if [ -n "$SOURCE_HASH" ]; then
echo "Web cache bust: SOURCE_HASH=${SOURCE_HASH}"
BUILD_ARGS="$BUILD_ARGS --build-arg SOURCE_HASH=${SOURCE_HASH}"
else
echo "⚠️ 无法计算 apps/web tree hash,跳过 cache bust"
fi
fi
BUILDER_NAME="ci-builder-persist"
if ! docker buildx inspect "$BUILDER_NAME" > /dev/null 2>&1; then
echo "持久 builder 不存在,创建中..."
+12
View File
@@ -31,6 +31,18 @@ for arg in "$@"; do
BUILD_ARGS="$BUILD_ARGS --build-arg $arg"
done
# Web 镜像 cache bust:计算 apps/web/ 的 git tree hash
# 当源码变化时 hash 变化,buildx 的 ARG 缓存键失效 → vite build 必定重新执行
if [ "${DOCKERFILE##*/}" = "web.Dockerfile" ]; then
SOURCE_HASH=$(git rev-parse HEAD:apps/web 2>/dev/null || echo "")
if [ -n "$SOURCE_HASH" ]; then
echo "Web cache bust: SOURCE_HASH=${SOURCE_HASH}"
BUILD_ARGS="$BUILD_ARGS --build-arg SOURCE_HASH=${SOURCE_HASH}"
else
echo "⚠️ 无法计算 apps/web tree hash,跳过 cache bust"
fi
fi
# 确保持久 builder 存在并使用(幂等)
if ! docker buildx inspect "$BUILDER_NAME" > /dev/null 2>&1; then
echo "持久 builder 不存在,创建中..."