name: Deploy stAndrews

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Deploy via SSH
        env:
          SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_SSH_KEY }}
          SERVER_IP: ${{ secrets.DEPLOY_SERVER_IP }}
          SERVER_USER: ${{ secrets.DEPLOY_SERVER_USER }}
        run: |
          mkdir -p ~/.ssh
          echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan -H $SERVER_IP >> ~/.ssh/known_hosts
          ssh ${SERVER_USER}@${SERVER_IP} "
            cd /data/projects/r/stAndrews &&
            git pull &&
            docker compose build &&
            docker compose up -d &&
            docker exec analytics-gateway caddy reload --config /etc/caddy/Caddyfile --adapter caddyfile
          "
