name: Deploy to Frontend Servers

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: self-hosted

    steps:
      - name: Free disk space
        run: |
          set +e
          echo "=== Disk before cleanup ==="
          df -h
          # Stale act runner workspaces from previous jobs (older than 60 min).
          if [ -d "$HOME/.cache/act" ]; then
            du -sh "$HOME/.cache/act" 2>/dev/null
            find "$HOME/.cache/act" -mindepth 1 -maxdepth 1 -type d -mmin +60 -exec rm -rf {} + 2>/dev/null
          fi
          # Stale runner workspaces under common locations.
          for dir in "$HOME/actions-runner/_work" "$HOME/.cache/setup-node" "$HOME/.npm/_cacache"; do
            if [ -d "$dir" ]; then
              find "$dir" -mindepth 1 -maxdepth 2 -mmin +1440 -exec rm -rf {} + 2>/dev/null
            fi
          done
          # Docker leftovers if docker is available.
          if command -v docker >/dev/null 2>&1; then
            docker image prune -af --filter "until=24h" 2>/dev/null
            docker container prune -f --filter "until=24h" 2>/dev/null
            docker builder prune -af --filter "until=24h" 2>/dev/null
          fi
          # Stale /tmp files older than 2h, keep currently-running runner files.
          find /tmp -mindepth 1 -maxdepth 1 -mmin +120 \
            -not -name 'runner*' -not -name 'act*' \
            -exec rm -rf {} + 2>/dev/null
          echo "=== Disk after cleanup ==="
          df -h
          exit 0

      - name: Checkout code
        uses: actions/checkout@v4

      - name: Setup Node.js
        uses: actions/setup-node@v4
        with:
          node-version: "22"
          cache: npm

      - name: Install dependencies
        run: npm ci

      - name: Type check
        run: npx tsc --noEmit

      - name: Format check
        run: npm run format:check

      - name: Test
        run: npm test

      - name: Build
        run: npm run build
        env:
          VITE_API_URL: ""
          VITE_API_PREFIX: "/apnew"
          VITE_DISABLE_ADMIN: "true"

      - name: Setup SSH key
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/deploy_key
          chmod 600 ~/.ssh/deploy_key
          ssh-keyscan -H ${{ secrets.FRONTEND_1_HOST }} >> ~/.ssh/known_hosts 2>/dev/null
          ssh-keyscan -H ${{ secrets.FRONTEND_2_HOST }} >> ~/.ssh/known_hosts 2>/dev/null

      - name: Deploy to both servers
        run: |
          set -euo pipefail
          deploy_to() {
            local HOST=$1
            echo ">>> 部署到 $HOST"
            rsync -avz --delete \
              -e "ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no" \
              dist/ \
              ec2-user@${HOST}:/var/www/ark-library/
            echo ">>> $HOST 部署完成"
          }
          deploy_to "${{ secrets.FRONTEND_1_HOST }}" &
          PID1=$!
          deploy_to "${{ secrets.FRONTEND_2_HOST }}" &
          PID2=$!
          FAIL=0
          wait $PID1 || { echo "ERROR: frontend-1 部署失败"; FAIL=1; }
          wait $PID2 || { echo "ERROR: frontend-2 部署失败"; FAIL=1; }
          [ $FAIL -eq 0 ] || exit 1
          echo "=== 两台都部署完成 ==="

      - name: Verify both servers match local build
        run: |
          set -euo pipefail
          LOCAL=$(sha256sum dist/index.html | awk '{print $1}')
          SUM1=$(ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no \
            ec2-user@${{ secrets.FRONTEND_1_HOST }} \
            "sha256sum /var/www/ark-library/index.html | awk '{print \$1}'")
          SUM2=$(ssh -i ~/.ssh/deploy_key -o StrictHostKeyChecking=no \
            ec2-user@${{ secrets.FRONTEND_2_HOST }} \
            "sha256sum /var/www/ark-library/index.html | awk '{print \$1}'")
          echo "local:      $LOCAL"
          echo "frontend-1: $SUM1"
          echo "frontend-2: $SUM2"
          if [ "$SUM1" != "$LOCAL" ]; then
            echo "ERROR: frontend-1 不是本次构建的版本"
            exit 1
          fi
          if [ "$SUM2" != "$LOCAL" ]; then
            echo "ERROR: frontend-2 不是本次构建的版本"
            exit 1
          fi
          echo "✓ 两台都已经更新到本次构建的版本。"

      - name: Cleanup SSH key
        if: always()
        run: rm -f ~/.ssh/deploy_key
