name: Deploy to Staging Netlify

on:
  push:
    # TODO: Migrate to staging branch only
    branches: [main]

jobs:
  build:
    name: Push build files to Netlify
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repository
        uses: actions/checkout@v3

      - name: Setup and run pnpm install
        uses: pnpm/action-setup@v2.2.4
        env:
          VITE_BACKEND_GQL_URL: ${{ secrets.STAGING_BACKEND_GQL_URL }}
        with:
          version: 7
          run_install: true

      - name: Build Site
        env:
          VITE_GA_ID: ${{ secrets.STAGING_GA_ID }}
          VITE_GTM_ID: ${{ secrets.STAGING_GTM_ID }}
          VITE_API_KEY: ${{ secrets.STAGING_FB_API_KEY }}
          VITE_AUTH_DOMAIN: ${{ secrets.STAGING_FB_AUTH_DOMAIN }}
          VITE_DATABASE_URL: ${{ secrets.STAGING_FB_DATABASE_URL }}
          VITE_PROJECT_ID: ${{ secrets.STAGING_FB_PROJECT_ID }}
          VITE_STORAGE_BUCKET: ${{ secrets.STAGING_FB_STORAGE_BUCKET }}
          VITE_MESSAGING_SENDER_ID: ${{ secrets.STAGING_FB_MESSAGING_SENDER_ID }}
          VITE_APP_ID: ${{ secrets.STAGING_FB_APP_ID }}
          VITE_BASE_URL: ${{ secrets.STAGING_BASE_URL }}
          VITE_BACKEND_GQL_URL: ${{ secrets.STAGING_BACKEND_GQL_URL }}
          VITE_BACKEND_WS_URL: ${{ secrets.STAGING_BACKEND_WS_URL }}
          VITE_SENTRY_DSN: ${{ secrets.SENTRY_DSN }}
          VITE_SENTRY_RELEASE_TAG: ${{ github.sha }}
          VITE_SENTRY_ENVIRONMENT: staging
        run: pnpm run generate

      # Deploy the staging site with netlify-cli
      - name: Deploy to Netlify (staging)
        run: npx netlify-cli deploy --dir=packages/hoppscotch-web/dist --prod
        env:
          NETLIFY_SITE_ID: ${{ secrets.NETLIFY_STAGING_SITE_ID }}
          NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}

      - name: Create Sentry Release
        uses: getsentry/action-release@v1
        env:
          SENTRY_AUTH_TOKEN: ${{ secrets.SENTRY_AUTH_TOKEN }}
          SENTRY_ORG: ${{ secrets.SENTRY_ORG }}
          SENTRY_PROJECT: ${{ secrets.SENTRY_PROJECT }}
        with:
          environment: staging
          ignore_missing: true
          ignore_empty: true
          version: ${{ github.sha }}
