fix: 修复 Docker 构建时找不到前端产物的问题

- 从 .dockerignore 移除 frontend/dist 和 backend/build,使外部构建产物可被 Docker 使用
- 在 GitHub Actions 中添加构建上下文准备步骤,验证产物存在
- 更新 Dockerfile 注释,说明构建产物在不同场景下的使用
This commit is contained in:
WrBug
2026-01-21 04:16:01 +08:00
parent 59297ec52f
commit 64a78406ed
3 changed files with 30 additions and 14 deletions
+2 -2
View File
@@ -14,11 +14,11 @@
.DS_Store
# 构建产物
backend/build/
# 注意:frontend/dist 和 backend/build/libs 在使用 BUILD_IN_DOCKER=false 时是必需的
# 所以不能忽略它们。在 BUILD_IN_DOCKER=true 时,它们会被 Docker 内部编译覆盖
backend/.gradle/
backend/out/
backend/bin/
frontend/dist/
frontend/node_modules/
frontend/.vite/
frontend/.cache/
+16
View File
@@ -195,6 +195,22 @@ jobs:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Prepare Docker build context
run: |
echo "📦 准备 Docker 构建上下文..."
# 确保构建产物存在且可访问
if [ ! -d "frontend/dist" ]; then
echo "❌ 错误:frontend/dist 不存在"
exit 1
fi
if [ ! -d "backend/build/libs" ] || [ -z "$(ls -A backend/build/libs/*.jar 2>/dev/null)" ]; then
echo "❌ 错误:backend/build/libs/*.jar 不存在"
exit 1
fi
echo "✅ 构建产物已准备好"
ls -lh frontend/dist/ | head -5
ls -lh backend/build/libs/*.jar
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
+12 -12
View File
@@ -19,8 +19,7 @@ ARG GITHUB_REPO_URL=https://github.com/WrBug/PolyHermes
ENV VERSION=${VERSION}
ENV GIT_TAG=${GIT_TAG}
ENV GITHUB_REPO_URL=${GITHUB_REPO_URL}
# 复制前端文件
# 复制前端文件(先复制 package.json 以利用 Docker 缓存)
COPY frontend/package*.json ./
# 条件:仅在 Docker 内部编译时安装依赖
@@ -28,24 +27,25 @@ RUN if [ "$BUILD_IN_DOCKER" = "true" ]; then \
npm ci; \
fi
# 复制所有前端源文件
COPY frontend/ ./
# 如果使用外部产物,先从构建上下文复制外部编译的 dist
# 注意:如果 BUILD_IN_DOCKER=true 且本地没有 dist,这个 COPY 会失败,但会在下面编译生成
COPY frontend/dist ./dist
# 条件:仅在 Docker 内部编译时执行构建(会覆盖外部产物)
# 条件:仅在 Docker 内部编译时执行构建
# 如果 BUILD_IN_DOCKER=false,需要从构建上下文复制外部编译的 dist
RUN if [ "$BUILD_IN_DOCKER" = "true" ]; then \
echo "🔨 Docker 内部编译前端..."; \
npm run build; \
else \
echo "⏭️ 使用外部产物"; \
if [ ! -d "dist" ] || [ -z "$(ls -A dist 2>/dev/null)" ]; then \
echo "❌ 错误:BUILD_IN_DOCKER=false 但找不到外部产物 frontend/dist"; \
exit 1; \
fi; \
echo "⏭️ 使用外部产物,将在下一步复制"; \
mkdir -p dist; \
fi
# 如果使用外部产物,从构建上下文复制外部编译的 dist
# 注意:这个 COPY 在 BUILD_IN_DOCKER=false 时必需
# 在 BUILD_IN_DOCKER=true 时,如果前端已编译,这个 COPY 会尝试覆盖,但结果相同
# 如果本地没有 distBUILD_IN_DOCKER=true 且未编译),这个 COPY 会失败,但上面的 RUN 已经编译了
COPY frontend/dist ./dist
# ==================== 阶段2:构建后端 ====================
FROM gradle:8.5-jdk17 AS backend-build
ARG BUILD_IN_DOCKER