name: deploy-on-main

on:
  push:
    branches: [ "main" ]

jobs:
  deploy:
    runs-on: [ self-hosted, linux_amd64 ]

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Node / PM2 / Prisma 확인
        run: |
          node -v
          npm -v
          pm2 -v || true
          npx prisma --version || true

      - name: 배포
        env:
          APP_DIR: /root/msgapp
        run: |
          set -e
          cd "$APP_DIR"
          # 최신 main 코드 반영
          git fetch origin main
          git reset --hard origin/main
          
          npm ci
          npm run dbforce
          npm run build
          pm2 reload ecosystem.config.js --env production || (pm2 start ecosystem.config.js --env production && pm2 save)
          pm2 list