name: CD | Cyberia mmo server | remote ssh
on:
  workflow_dispatch:
    inputs:
      job:
        description: 'Job to run (deploy)'
        required: true
        type: choice
        options:
          - deploy
      source-channel:
        description: 'Source channel: public, or private for the private mirrors. A private release is mirrored to public once it serves'
        required: false
        default: private
        type: choice
        options:
          - private
          - public
permissions:
  contents: write
  packages: write
  id-token: write

jobs:
  deploy:
    if: >-
      (github.repository == 'underpostnet/cyberia-server' ||
       github.repository == 'underpost/cyberia-server')
      && github.event.inputs.job == 'deploy'
    name: Remote SSH deployment
    runs-on: ubuntu-latest
    environment: production
    concurrency:
      group: dd-cyberia-cyberia-server
      cancel-in-progress: false
    steps:
      - name: Run remote deploy via SSH
        uses: appleboy/ssh-action@v1.2.5
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          key: ${{ secrets.SSH_PRIV_KEY }}
          port: ${{ secrets.SSH_PORT }}
          command_timeout: 60m
          script: |
            set -e
            set -o pipefail
            # Carried across the SSH hop so the far side reports in GitHub Actions form:
            # deploy/lib/github-actions-logging.sh folds its monitor frames into collapsed groups, and the
            # state domain's telemetry export emits ::notice::/::error:: annotations this job
            # surfaces. GITHUB_ACTIONS itself is a runner-local variable and does not travel.
            export RUN_QUIET_CI=github
            export CYBERIA_SOURCE_CHANNEL=${{ github.event.inputs.source-channel || 'private' }}
            bash /home/dd/engine/deploy/cyberia-server/deploy.sh
