name: Build Renderer

concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

on:
  push:
    branches:
      - release/**

jobs:
  build:
    runs-on: ubuntu-latest

    permissions:
      contents: read

    env:
      NODE_OPTIONS: --max-old-space-size=4096
      BRANCH_NAME: ${{ github.ref_name }}

    steps:
      - name: Check out Git repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Set up Node.js
        uses: actions/setup-node@v4
        with:
          node-version: 22.21.0
          cache: "yarn"

      - name: Enable Corepack (Yarn)
        run: corepack enable

      - name: Install dependencies
        run: yarn install --frozen-lockfile --ignore-scripts

      - name: Build Renderer
        run: yarn build
        env:
          RENDERER_VITE_EXTERNAL_RESOURCES_URL: ${{ vars.EXTERNAL_RESOURCES_URL }}
          RENDERER_VITE_SENTRY_DSN: ${{ vars.SENTRY_DSN }}

      - name: Deploy to Cloudflare Pages
        env:
          CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
          CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
        run: |
          npx --yes wrangler@3 pages deploy out/renderer \
            --project-name="hydra" \
            --branch "$BRANCH_NAME" \
            --commit-dirty
