name: Deploy to VPS

on:
  push:
    branches:
      - master
  workflow_dispatch:

permissions:
  contents: read
  packages: write

env:
  IMAGE_NAME: ghcr.io/${{ github.repository_owner }}/bete

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          submodules: recursive

      - name: Normalize image name
        run: echo "IMAGE_NAME=${IMAGE_NAME,,}" >> "$GITHUB_ENV"

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Log in to GHCR
        uses: docker/login-action@v3
        with:
          registry: ghcr.io
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: |
            ${{ env.IMAGE_NAME }}:latest
            ${{ env.IMAGE_NAME }}:${{ github.sha }}

      - name: Deploy to VPS
        uses: appleboy/ssh-action@v1.2.5
        env:
          IMAGE_NAME: ${{ env.IMAGE_NAME }}
          GHCR_USERNAME: ${{ github.actor }}
          GHCR_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          ENV_FILE: ${{ secrets.ENV_FILE }}
        with:
          host: ${{ secrets.VPS_HOST }}
          username: ${{ secrets.VPS_USERNAME }}
          key: ${{ secrets.VPS_SSH_KEY }}
          envs: IMAGE_NAME,GHCR_USERNAME,GHCR_TOKEN,ENV_FILE
          script: |
            set -eu

            APP_DIR=/opt/imphenbot
            mkdir -p "$APP_DIR"
            cd "$APP_DIR"

            printf '%s\nIMAGE_NAME=%s:latest\n' "$ENV_FILE" "$IMAGE_NAME" > .env

            cat > docker-compose.yml <<'EOF'
            services:
              app:
                image: ${IMAGE_NAME}
                container_name: imphenbot-app
                restart: unless-stopped
                env_file:
                  - .env
                volumes:
                  - ./recordings:/app/recordings
                  - ./.muxer-queue.db:/app/.muxer-queue.db
                  - ./.muxer-queue.db-shm:/app/.muxer-queue.db-shm
                  - ./.muxer-queue.db-wal:/app/.muxer-queue.db-wal
                labels:
                  - "traefik.enable=true"
                  - "traefik.http.routers.imphenbot.rule=Host(`imphnen.asepharyana.tech`)"
                  - "traefik.http.routers.imphenbot.entrypoints=websecure"
                  - "traefik.http.routers.imphenbot.tls=true"
                  - "traefik.http.services.imphenbot.loadbalancer.server.port=3000"
                networks:
                  - app-shared-net

            networks:
              app-shared-net:
                name: app-shared-net
                external: true
            EOF

            touch .muxer-queue.db .muxer-queue.db-shm .muxer-queue.db-wal
            mkdir -p recordings

            echo "$GHCR_TOKEN" | docker login ghcr.io -u "$GHCR_USERNAME" --password-stdin

            docker compose pull
            docker compose up -d --force-recreate --remove-orphans
            docker image prune -f
