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@v4 with: name: frontend-dist-${{ github.sha }} path: frontend/out/ retention-days: 1 compression-level: 9 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: 🚀 部署到服务器 run: | # 设置 SSH 环境 export SSHPASS=${{ secrets.ALIYUN_PWD }} # 并行执行:测试连接 + 创建目录 sshpass -e ssh -o StrictHostKeyChecking=no -o ConnectTimeout=10 ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }} " mkdir -p /home/gitea/www/photography echo '✅ 连接和目录检查完成' " # 快速部署:跳过备份以提高速度 echo "🚀 快速部署新版本..." sshpass -e rsync -avz --delete --compress-level=9 --progress \ -e "ssh -o StrictHostKeyChecking=no" \ frontend/out/ ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }}:/home/gitea/www/photography/ # 并行设置权限和重载 sshpass -e ssh -o StrictHostKeyChecking=no ${{ secrets.ALIYUN_USER_NAME }}@${{ secrets.ALIYUN_IP }} " 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 {} \; && \ 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