name: Deployment

on:
  push:
    branches: ["main"]
  workflow_dispatch:

jobs:
  deploy:
    runs-on: ubuntu-latest
    timeout-minutes: 10
    steps:
      - name: Setup SSH
        env:
          PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
        run: |
          echo "$PRIVATE_KEY" | base64 --decode > private_key.pem
          chmod 600 private_key.pem

      - name: Deploy to VPS
        run: |
          ssh -o StrictHostKeyChecking=no -i private_key.pem ${{ vars.SSH_REMOTE_USERNAME }}@${{ vars.SSH_REMOTE_HOST }} <<EOF
            set -e
            cd cm1005-gameproj
            git pull -r
          EOF

      - name: Cleanup SSH key
        if: always()
        run: rm -f private_key.pem
