# ============================================================================= # PERLER LAB — GitHub Actions CI/CD 流水线 # 触发条件:推送到 main 分支 或 手动触发 # 流程: # 1. test — 安装依赖 + 运行测试(前后端并行) # 2. build — 构建 Docker 镜像(验证 Dockerfile 正确) # 3. deploy — SSH 到生产服务器,git pull + docker compose up -d # ============================================================================= name: Deploy on: push: branches: [main] workflow_dispatch: env: # 镜像标签——使用 commit SHA 保证唯一性,部署时通过 docker compose up -d --build 重建 # 若需推送镜像到 registry,在此取消注释并配置后续步骤 # REGISTRY: ghcr.io # IMAGE_TAG: ${{ github.sha }} jobs: # ── 测试 ── test: runs-on: ubuntu-latest strategy: matrix: project: - name: perler-lab # perler-lab 无 package-lock.json,使用 npm install install-cmd: npm install - name: perler-lab-server # perler-lab-server 有 package-lock.json,使用 npm ci 保证可复现 install-cmd: npm ci steps: - uses: actions/checkout@v4 - uses: actions/setup-node@v4 with: node-version: 20 cache: npm cache-dependency-path: ${{ matrix.project.name }}/package-lock.json - name: Install & Test (${{ matrix.project.name }}) working-directory: ${{ matrix.project.name }} run: | ${{ matrix.project.install-cmd }} --no-audit --no-fund npm test # 额外:前端执行类型检查 - name: Typecheck (${{ matrix.project.name }}) if: matrix.project.name == 'perler-lab' working-directory: ${{ matrix.project.name }} run: npm run typecheck # ── 构建 Docker 镜像(验证 Dockerfile 正确性) ── build: needs: test runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Build frontend image run: docker build -t perler-lab-frontend:ci perler-lab - name: Build backend image run: docker build -t perler-lab-server:ci perler-lab-server # ── 部署到生产服务器 ── deploy: needs: build runs-on: ubuntu-latest # 需要 GitHub Secrets 配置以下密钥,否则跳过(上一步 build 仍可通过) if: ${{ secrets.DEPLOY_HOST != '' }} steps: - name: Deploy via SSH uses: appleboy/ssh-action@v1.1.0 with: host: ${{ secrets.DEPLOY_HOST }} username: ${{ secrets.DEPLOY_USER }} key: ${{ secrets.DEPLOY_SSH_KEY }} # 可选:用端口转发跳过跳板机 # port: ${{ secrets.DEPLOY_PORT || '22' }} script_stop: true script: | set -e echo "===== 进入部署目录 =====" cd ${{ secrets.DEPLOY_PATH || '/opt/perler-lab' }} echo "===== 拉取最新代码 =====" git pull origin main echo "===== 构建并启动服务 =====" docker compose -f perler-lab-server/docker-compose.yml up -d --build echo "===== 健康检查:等待后端就绪 =====" for i in $(seq 1 12); do if curl -sf http://localhost:8080/healthz > /dev/null 2>&1; then echo "✅ 前端健康检查通过" break fi if curl -sf http://localhost:8788/health > /dev/null 2>&1; then echo "✅ 后端健康检查通过" break fi echo "等待服务就绪($i/12)..." sleep 5 done echo "===== 清理旧镜像 =====" docker image prune -f echo "===== 部署完成 ====="