docs(deploy): 修复文档流程,添加克隆代码步骤 #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ============================================================ | |
| # GitHub Actions - 前端部署 (Frontend) | |
| # ============================================================ | |
| name: Deploy Frontend | |
| on: | |
| push: | |
| branches: [main, develop] | |
| paths: | |
| - 'frontend/**' | |
| - 'packages/shared-types/**' | |
| - '.github/workflows/deploy-frontend.yml' | |
| workflow_dispatch: | |
| env: | |
| NODE_VERSION: '22' | |
| DEPLOY_DIR: /var/www/qzt/frontend | |
| jobs: | |
| deploy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup Node.js | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: ${{ env.NODE_VERSION }} | |
| - name: Install pnpm | |
| uses: pnpm/action-setup@v4 | |
| with: | |
| version: 9 | |
| - name: Get pnpm store directory | |
| id: pnpm-cache | |
| shell: bash | |
| run: | | |
| echo "STORE_PATH=$(pnpm store path)" >> $GITHUB_OUTPUT | |
| - name: Setup pnpm cache | |
| uses: actions/cache@v4 | |
| with: | |
| path: ${{ steps.pnpm-cache.outputs.STORE_PATH }} | |
| key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }} | |
| restore-keys: | | |
| ${{ runner.os }}-pnpm-store- | |
| - name: Install dependencies | |
| run: pnpm install --frozen-lockfile | |
| - name: Generate API client | |
| run: | | |
| cd frontend | |
| pnpm run generate:api | |
| - name: Build Frontend | |
| run: | | |
| cd frontend | |
| pnpm run build | |
| env: | |
| VITE_API_BASE_URL: https://admin.devlovecode.com/api | |
| - name: Create deployment package | |
| run: | | |
| cd frontend/dist | |
| tar -czf ../../frontend-dist.tar.gz . | |
| - name: Setup SSH | |
| run: | | |
| mkdir -p ~/.ssh | |
| echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_rsa | |
| chmod 600 ~/.ssh/id_rsa | |
| ssh-keyscan -H ${{ secrets.SERVER_HOST }} >> ~/.ssh/known_hosts | |
| - name: Deploy to server | |
| run: | | |
| ssh -o StrictHostKeyChecking=no root@${{ secrets.SERVER_HOST }} << 'ENDSSH' | |
| set -e | |
| DEPLOY_DIR="${{ env.DEPLOY_DIR }}" | |
| BACKUP_DIR="/var/www/qzt/backups" | |
| # 创建目录 | |
| mkdir -p $DEPLOY_DIR $BACKUP_DIR | |
| # 备份 | |
| if [ -f "$DEPLOY_DIR/index.html" ]; then | |
| tar -czf $BACKUP_DIR/frontend-backup-$(date +%Y%m%d_%H%M%S).tar.gz -C $DEPLOY_DIR . 2>/dev/null || true | |
| echo "备份已创建" | |
| fi | |
| ENDSSH | |
| # 上传构建文件 | |
| scp -o StrictHostKeyChecking=no frontend-dist.tar.gz \ | |
| root@${{ secrets.SERVER_HOST }}:/tmp/ | |
| ssh -o StrictHostKeyChecking=no root@${{ secrets.SERVER_HOST }} << 'ENDSSH' | |
| set -e | |
| DEPLOY_DIR="${{ env.DEPLOY_DIR }}" | |
| # 清空并解压新版本 | |
| rm -rf $DEPLOY_DIR/* | |
| tar -xzf /tmp/frontend-dist.tar.gz -C $DEPLOY_DIR/ | |
| rm /tmp/frontend-dist.tar.gz | |
| # 设置权限 | |
| chown -R nginx:nginx $DEPLOY_DIR | |
| chmod -R 755 $DEPLOY_DIR | |
| # 重载 nginx | |
| nginx -t && nginx -s reload | |
| echo "前端部署成功!" | |
| ENDSSH | |
| - name: Health check | |
| run: | | |
| sleep 5 | |
| curl -f https://admin.devlovecode.com/health || exit 1 | |
| echo "健康检查通过!" |