name: 部署前端网站 on: push: branches: [ main ] paths: - 'frontend/**' - '.gitea/workflows/deploy-frontend.yml' workflow_dispatch: env: BUN_VERSION: 'latest' CACHE_KEY: ${{ runner.os }}-bun-${{ hashFiles('**/bun.lock') }} jobs: test-and-build: name: 🧪 测试和构建 runs-on: ubuntu-latest steps: - name: 📥 检出代码 uses: actions/checkout@v4 - name: 🦀 设置 Bun 环境 uses: oven-sh/setup-bun@v1 with: bun-version: ${{ env.BUN_VERSION }} - name: 💾 缓存 Bun 依赖 uses: actions/cache@v4 with: path: | ~/.bun/install/cache frontend/node_modules key: ${{ env.CACHE_KEY }}-${{ hashFiles('frontend/bun.lock') }} restore-keys: | ${{ env.CACHE_KEY }}- - name: 📦 安装依赖 working-directory: ./frontend run: bun install --frozen-lockfile - name: 🏗️ 并行构建和检查 working-directory: ./frontend run: | # 并行执行代码检查和构建 bun run lint & bun run type-check & wait env: NEXT_PUBLIC_API_URL: https://api.photography.iriver.top NEXT_PUBLIC_SITE_URL: https://photography.iriver.top NEXT_PUBLIC_SITE_NAME: 摄影作品集 - name: 🏗️ 构建生产版本 working-directory: ./frontend env: NEXT_PUBLIC_API_URL: https://api.photography.iriver.top NEXT_PUBLIC_SITE_URL: https://photography.iriver.top NEXT_PUBLIC_SITE_NAME: 摄影作品集 run: bun run build - name: 📦 打包构建产物 uses: actions/upload-artifact@v3 with: name: frontend-dist-${{ github.sha }} path: frontend/out/ retention-days: 1 deploy: name: 🚀 部署到生产环境 runs-on: ubuntu-latest needs: test-and-build if: github.ref == 'refs/heads/main' steps: - name: 📥 检出代码 uses: actions/checkout@v4 - name: 🦀 设置 Bun 环境 uses: oven-sh/setup-bun@v1 with: bun-version: ${{ env.BUN_VERSION }} - name: 💾 缓存 Bun 依赖 uses: actions/cache@v4 with: path: | ~/.bun/install/cache frontend/node_modules key: ${{ env.CACHE_KEY }}-${{ hashFiles('frontend/bun.lock') }} - name: 📦 安装依赖 working-directory: ./frontend run: bun install --frozen-lockfile - name: 🏗️ 构建生产版本 working-directory: ./frontend env: NEXT_PUBLIC_API_URL: https://api.photography.iriver.top NEXT_PUBLIC_SITE_URL: https://photography.iriver.top NEXT_PUBLIC_SITE_NAME: 摄影作品集 run: bun run build - name: 📤 上传文件到服务器 uses: appleboy/scp-action@v0.1.6 with: host: ${{ secrets.ALIYUN_IP }} username: ${{ secrets.ALIYUN_USER_NAME }} key: ${{ secrets.ALIYUN_SSH_KEY }} port: 22 source: "frontend/out/*" target: "/home/gitea/www/photography/" strip_components: 2 rm: true - name: 🔧 设置文件权限 uses: appleboy/ssh-action@v1.0.0 with: host: ${{ secrets.ALIYUN_IP }} username: ${{ secrets.ALIYUN_USER_NAME }} key: ${{ secrets.ALIYUN_SSH_KEY }} port: 22 script: | echo "🔧 设置文件权限..." # 设置所有者 chown -R gitea:gitea /home/gitea/www/photography # 设置权限 chmod -R 755 /home/gitea/www/photography find /home/gitea/www/photography -type f \( -name '*.html' -o -name '*.js' -o -name '*.css' -o -name '*.json' \) -exec chmod 644 {} \; # 重新加载 Caddy sudo systemctl reload caddy echo "✅ 前端部署完成!" - name: 🔍 健康检查 run: | echo "🔍 执行健康检查..." sleep 5 # 快速健康检查 if curl -f -s -o /dev/null --max-time 10 https://photography.iriver.top; then echo "✅ 前端网站访问正常" else echo "⚠️ 前端网站访问异常,请手动检查" fi