name: Publish @notesnook/web

on:
  workflow_dispatch:
    inputs:
      release-track:
        type: choice
        required: true
        default: stable
        description: "Select the release track"
        options:
          - stable
          - beta

jobs:
  build:
    name: Build
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup Node
        uses: ./.github/actions/setup-node-with-cache

      - name: Install packages
        run: |
          npm ci --ignore-scripts --prefer-offline --no-audit
          npm run bootstrap -- --scope=web

      - name: Setup environment
        run: |
          echo "CLOUDFLARE_ACCOUNT_ID=${{ secrets.CLOUDFLARE_ACCOUNT_ID }}" >> $GITHUB_ENV
          echo "CLOUDFLARE_API_TOKEN=${{ secrets.CLOUDFLARE_API_TOKEN }}" >> $GITHUB_ENV

      - name: Generate build (stable)
        if: ${{ inputs.release-track == 'stable' }}
        run: npm run build:web

      - name: Publish to Cloudflare Pages (stable)
        if: ${{ inputs.release-track == 'stable' }}
        working-directory: ./apps/web
        run: npx --yes wrangler pages deploy --project-name=notesnook-app ./build/

      - name: Generate build (beta)
        if: ${{ inputs.release-track == 'beta' }}
        run: npm run build:beta:web

      - name: Publish to Cloudflare Pages (beta)
        if: ${{ inputs.release-track == 'beta' }}
        working-directory: ./apps/web
        run: npx --yes wrangler pages deploy --branch=beta --project-name=notesnook-app-beta ./build/
