name: Deploy Static Sites

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        run: |
          git clone --depth=1 --branch=${{ github.ref_name }} https://gitea.airlabs.art/${{ github.repository }}.git .

      - name: Sync to server
        run: |
          # 写入 SSH 私钥
          mkdir -p ~/.ssh
          printf '%s\n' '${{ secrets.INTERNAL_SERVER_SSH_KEY }}' > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan -H 118.196.70.19 >> ~/.ssh/known_hosts 2>/dev/null

          # 先清空服务器目录，再上传项目文件
          ssh root@118.196.70.19 "rm -rf /data/static-sites/* && mkdir -p /data/static-sites"

          # 找出所有项目目录并 scp 上传
          for dir in */; do
            case "$dir" in
              .gitea/|.git/|k8s/) continue ;;
            esac
            echo "上传 $dir ..."
            scp -r "$dir" root@118.196.70.19:/data/static-sites/
          done

          echo "✓ 文件同步完成"

      - name: Verify
        run: |
          mkdir -p ~/.ssh
          printf '%s\n' '${{ secrets.INTERNAL_SERVER_SSH_KEY }}' > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan -H 118.196.70.19 >> ~/.ssh/known_hosts 2>/dev/null
          ssh root@118.196.70.19 "ls -laR /data/static-sites/"
