name: Restart server
on:
  push:
    branches:
      - release

jobs:
  restart:
    runs-on: [ ubuntu-22.04 ]
    steps:
      - uses: actions/checkout@v4

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

      - name: Restart server
        run: |
          ssh -i ~/.ssh/deploy_key \
            ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_HOST }} \
            "systemctl restart barotrauma 2>/dev/null || \
             (killall DedicatedServer 2>/dev/null; sleep 2; cd /opt/barotrauma && nohup ./DedicatedServer >/dev/null 2>&1 &)"
