ci: speed up docker build with BuildKit cache mount and layer caching

This commit is contained in:
mediabot-pt
2026-06-29 12:37:48 +08:00
parent 76f975915c
commit d5fc73cad7
2 changed files with 25 additions and 7 deletions

View File

@@ -1,14 +1,18 @@
# syntax=docker/dockerfile:1
# PAR Server - 前后端一体化 Docker 镜像
# 参考 mediabot 的多阶段构建方式
# 启用 BuildKit 以支持 cache mount
# 第一阶段:构建前端
FROM 192.168.31.253/library/node:22-alpine AS web-builder
WORKDIR /app
COPY web/package.json web/package-lock.json ./
RUN npm ci --ignore-scripts
RUN --mount=type=cache,target=/root/.npm \
npm ci --ignore-scripts
COPY web/ .
RUN npm run build
RUN --mount=type=cache,target=/root/.npm \
npm run build
# 第二阶段:构建后端
# mediabot 使用 maven-java21 镜像(已含 JDK + Maven)
@@ -22,8 +26,9 @@ COPY par-common/pom.xml par-common/
COPY par-core/pom.xml par-core/
COPY par-api/pom.xml par-api/
# 下载依赖(缓存层)
RUN mvn dependency:go-offline -B
# 下载依赖(使用 cache mount 缓存 Maven 仓库)
RUN --mount=type=cache,target=/root/.m2,sharing=locked \
mvn dependency:go-offline -B
# 复制源码并构建
COPY par-common/src par-common/src
@@ -33,7 +38,9 @@ COPY par-api/src par-api/src
# 将前端构建产物复制到后端静态资源目录
COPY --from=web-builder /app/dist par-api/src/main/resources/static
RUN mvn clean package -DskipTests -B && \
# 打包(使用 cache mount 缓存 Maven 仓库)
RUN --mount=type=cache,target=/root/.m2,sharing=locked \
mvn clean package -DskipTests -B && \
mkdir -p target && \
cp par-api/target/*.jar target/app.jar