name: Deploy To Server

on:
  # 暂停自动/手动部署触发；需要恢复时改回 push / workflow_dispatch。
  workflow_dispatch:
    inputs:
      disabled:
        description: "Deployment is temporarily disabled"
        required: false
        default: "true"

concurrency:
  group: deploy-${{ github.ref }}
  cancel-in-progress: true

jobs:
  deploy:
    if: ${{ false }}
    runs-on: ubuntu-latest
    steps:
      - name: Deploy over SSH
        env:
          DEPLOY_HOST: ${{ secrets.DEPLOY_HOST }}
          DEPLOY_PORT: ${{ secrets.DEPLOY_PORT }}
          DEPLOY_USER: ${{ secrets.DEPLOY_USER }}
          DEPLOY_SSH_PRIVATE_KEY: ${{ secrets.DEPLOY_SSH_PRIVATE_KEY }}
          DEPLOY_SSH_KNOWN_HOSTS: ${{ secrets.DEPLOY_SSH_KNOWN_HOSTS }}
          DEPLOY_WORKDIR: ${{ secrets.DEPLOY_WORKDIR }}
        run: |
          set -eu

          mkdir -p ~/.ssh
          chmod 700 ~/.ssh
          printf '%s\n' "$DEPLOY_SSH_PRIVATE_KEY" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519
          printf '%s\n' "$DEPLOY_SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
          chmod 644 ~/.ssh/known_hosts

          SSH_PORT="${DEPLOY_PORT:-22}"
          REMOTE_DIR="${DEPLOY_WORKDIR:-/opt/chat-one-web}"

          ssh -p "$SSH_PORT" "$DEPLOY_USER@$DEPLOY_HOST" <<EOF
          set -eu
          cd "$REMOTE_DIR"
          git pull --ff-only
          docker compose up -d --build
          docker image prune -f
          EOF
