From 52718ef60827e9c336df754f29afde432b2be468 Mon Sep 17 00:00:00 2001 From: shaw Date: Mon, 4 Aug 2025 10:32:22 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=9B=B4=E6=96=B0=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 更新 Docker 配置和构建脚本 - 优化项目文档和部署脚本 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- .dockerignore | 5 +- .github/workflows/auto-release-pipeline.yml | 61 +-------------------- Dockerfile | 23 +++++++- README.md | 20 ++++++- scripts/manage.sh | 8 +++ 5 files changed, 54 insertions(+), 63 deletions(-) diff --git a/.dockerignore b/.dockerignore index 252b0ab5..f10a08c1 100644 --- a/.dockerignore +++ b/.dockerignore @@ -55,12 +55,15 @@ coverage/ .nyc_output/ # Build files -dist/ +# dist/ # 前端构建阶段需要复制源文件,所以不能忽略 build/ *.pid *.seed *.pid.lock +# 但可以忽略本地已构建的 dist 目录 +web/admin-spa/dist/ + # CI/CD .travis.yml .gitlab-ci.yml diff --git a/.github/workflows/auto-release-pipeline.yml b/.github/workflows/auto-release-pipeline.yml index a4abbb26..12a59522 100644 --- a/.github/workflows/auto-release-pipeline.yml +++ b/.github/workflows/auto-release-pipeline.yml @@ -103,52 +103,7 @@ jobs: echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT echo "new_tag=v$NEW_VERSION" >> $GITHUB_OUTPUT - - name: Check if frontend build is needed - id: check_frontend - if: steps.check.outputs.needs_bump == 'true' - run: | - # 检查 web/admin-spa 目录是否有变更 - FRONTEND_CHANGES=false - CHANGED_FILES=$(git diff --name-only HEAD~1..HEAD 2>/dev/null || git diff --name-only $(git rev-list --max-parents=0 HEAD)..HEAD) - - while IFS= read -r file; do - if [[ "$file" =~ ^web/admin-spa/ ]] && - [[ ! "$file" =~ ^web/admin-spa/dist/ ]] && - [[ ! "$file" =~ \.(md|txt)$ ]] && - [[ "$file" != "web/admin-spa/.gitignore" ]]; then - echo "Found frontend change in: $file" - FRONTEND_CHANGES=true - break - fi - done <<< "$CHANGED_FILES" - - echo "needs_build=$FRONTEND_CHANGES" >> $GITHUB_OUTPUT - - - name: Setup Node.js - if: steps.check.outputs.needs_bump == 'true' && steps.check_frontend.outputs.needs_build == 'true' - uses: actions/setup-node@v4 - with: - node-version: '18' - cache: 'npm' - cache-dependency-path: web/admin-spa/package-lock.json - - - name: Build admin-spa - if: steps.check.outputs.needs_bump == 'true' && steps.check_frontend.outputs.needs_build == 'true' - run: | - echo "Building admin-spa frontend..." - cd web/admin-spa - npm ci - npm run build - cd ../.. - - # 确认 dist 目录已创建 - if [ -d "web/admin-spa/dist" ]; then - echo "✅ Frontend build successful" - ls -la web/admin-spa/dist/ - else - echo "❌ Frontend build failed - dist directory not found" - exit 1 - fi + # 前端构建已移至 Docker 构建流程中 - name: Update VERSION file if: steps.check.outputs.needs_bump == 'true' @@ -159,19 +114,9 @@ jobs: git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" - # 检查是否需要添加 dist 目录 - if [ "${{ steps.check_frontend.outputs.needs_build }}" = "true" ] && [ -d "web/admin-spa/dist" ]; then - git add -f web/admin-spa/dist/ - echo "Added frontend dist directory to commit" - fi - - # 提交VERSION文件和可能的dist目录 - 添加 [skip ci] 以避免再次触发 + # 提交VERSION文件 - 添加 [skip ci] 以避免再次触发 git add VERSION - if [ "${{ steps.check_frontend.outputs.needs_build }}" = "true" ]; then - git commit -m "chore: sync VERSION file with release ${{ steps.next_version.outputs.new_tag }} and rebuild frontend [skip ci]" - else - git commit -m "chore: sync VERSION file with release ${{ steps.next_version.outputs.new_tag }} [skip ci]" - fi + git commit -m "chore: sync VERSION file with release ${{ steps.next_version.outputs.new_tag }} [skip ci]" - name: Install git-cliff if: steps.check.outputs.needs_bump == 'true' diff --git a/Dockerfile b/Dockerfile index 339da465..c6306ca3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,22 @@ -# 🐳 使用官方 Node.js 18 Alpine 镜像 +# 🎯 前端构建阶段 +FROM node:18-alpine AS frontend-builder + +# 📁 设置工作目录 +WORKDIR /app/web/admin-spa + +# 📦 复制前端依赖文件 +COPY web/admin-spa/package*.json ./ + +# 🔽 安装前端依赖 +RUN npm ci + +# 📋 复制前端源代码 +COPY web/admin-spa/ ./ + +# 🏗️ 构建前端 +RUN npm run build + +# 🐳 主应用阶段 FROM node:18-alpine # 📋 设置标签 @@ -26,6 +44,9 @@ RUN npm ci --only=production && \ # 📋 复制应用代码 COPY . . +# 📦 从构建阶段复制前端产物 +COPY --from=frontend-builder /app/web/admin-spa/dist /app/web/admin-spa/dist + # 🔧 复制并设置启动脚本权限 COPY docker-entrypoint.sh /usr/local/bin/ RUN chmod +x /usr/local/bin/docker-entrypoint.sh diff --git a/README.md b/README.md index ce7d7126..a376441a 100644 --- a/README.md +++ b/README.md @@ -269,7 +269,17 @@ module.exports = { } ``` -### 第四步:启动服务 +### 第四步:安装前端依赖并构建 + +```bash +# 安装前端依赖 +npm run install:web + +# 构建前端(生成 dist 目录) +npm run build:web +``` + +### 第五步:启动服务 ```bash # 初始化 @@ -526,10 +536,14 @@ git add package-lock.json # 3. 安装新的依赖(如果有) npm install -# 4. 重启服务 +# 4. 安装并构建前端 +npm run install:web +npm run build:web + +# 5. 重启服务 npm run service:restart:daemon -# 5. 检查服务状态 +# 6. 检查服务状态 npm run service:status ``` diff --git a/scripts/manage.sh b/scripts/manage.sh index 01a1550e..5a526887 100644 --- a/scripts/manage.sh +++ b/scripts/manage.sh @@ -458,6 +458,10 @@ EOF print_info "安装Web界面依赖..." npm run install:web + # 构建前端 + print_info "构建前端界面..." + npm run build:web + # 创建systemd服务文件(Linux) if [[ "$OS" == "debian" || "$OS" == "redhat" || "$OS" == "arch" ]]; then create_systemd_service @@ -547,6 +551,10 @@ update_service() { npm install npm run install:web + # 构建前端 + print_info "构建前端界面..." + npm run build:web + # 启动服务 start_service