name: Backend Deploy (Go + Docker)
on:
  push:
    branches:
      - main
    paths:
      - 'server/**'

jobs:
  deploy:
    runs-on: arm
    container:
      image: catthehacker/ubuntu:act-latest
    steps:
      - name: 获取代码
        uses: actions/checkout@v4
      
      - name: 安装 Go
        uses: actions/setup-go@v5
        with:
          go-version: '1.22'
          
      - name: 编译后端可执行文件
        run: |
          cd server
          env GOOS=linux GOARCH=arm64 go build -o server_bin ./cmd/server
      
      - name: 准备部署文件
        run: |
          mkdir -p deploy
          cp server/server_bin deploy/
          cp server/Dockerfile deploy/
          cp server/docker-compose.yml deploy/

      - name: 部署文件到服务器
        uses: up9cloud/action-rsync@master
        env:
          USER: root
          HOST: 144.24.60.0
          KEY: ${{secrets.USAARMLOGIN_SSH_KEY}}
          ARGS: -avz --delete 
          SSH_ARGS: "-p 22 -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no"
          SOURCE: ./deploy/
          TARGET: /var/admin/InsightReply/server/
          VERBOSE: true

      - name: 重启服务 (Docker Compose)
        uses: appleboy/ssh-action@v1.0.3
        with:
          host: 144.24.60.0
          username: root
          key: ${{secrets.USAARMLOGIN_SSH_KEY}}
          script: |
            cd /var/admin/InsightReply/server
            # 停止并重新构建启动容器 (利用刚同步过来的新 server_bin 和 Dockerfile)
            docker-compose up -d --build
