name: Deploy to Staging
on:
  push:
    branches:
      - staging
  workflow_dispatch:

jobs:
  deploy:
    name: Deploy to Staging
    runs-on: ubuntu-latest
    steps:
      - name: Pull latest code
        uses: appleboy/ssh-action@v1
        with:
          host: ${{ vars.STAGING_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          password: ${{ secrets.SSH_PASSWORD }}
          port: 22
          script: |
            cd ~/docker/var-monorepo
            git checkout staging
            git pull
      - name: Deploy migration to Database
        uses: appleboy/ssh-action@v1
        with:
          host: ${{ vars.STAGING_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          password: ${{ secrets.SSH_PASSWORD }}
          port: 22
          script: |
            export NVM_DIR="$HOME/.nvm"
            source "$NVM_DIR/nvm.sh"
            cd ~/docker/var-monorepo/packages/database
            pnpm exec prisma migrate deploy
      - name: Build and start containers
        uses: appleboy/ssh-action@v1
        with:
          host: ${{ vars.STAGING_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          password: ${{ secrets.SSH_PASSWORD }}
          port: 22
          command_timeout: 60m
          script: |
            export NVM_DIR="$HOME/.nvm"
            source "$NVM_DIR/nvm.sh"
            cd ~/docker/var-monorepo
            pnpm staging-start
