name: Build
on:
    pull_request: {}
    push:
        branches: [develop, master]
    merge_group:
        types: [checks_requested]
concurrency:
    group: ${{ github.workflow }}-${{ github.head_ref || github.sha }}
    cancel-in-progress: ${{ github.event_name == 'pull_request' }}
# develop pushes and repository_dispatch handled in build_develop.yaml
env:
    # This must be set for fetchdep.sh to get the right branch
    PR_NUMBER: ${{ github.event.pull_request.number }}
    NX_DEFAULT_OUTPUT_STYLE: stream-without-prefixes
permissions: {} # No permissions required
jobs:
    build:
        name: "Build on ${{ matrix.image }}"
        # We build on all 3 platforms to ensure we don't have any OS-specific build incompatibilities
        strategy:
            fail-fast: false
            matrix:
                image:
                    - ubuntu-24.04
                    - windows-2022
                    - macos-14
                isDevelop:
                    - ${{ github.event_name == 'push' && github.ref_name == 'develop' }}
                isPullRequest:
                    - ${{ github.event_name == 'pull_request' }}
                # Skip the ubuntu-24.04 build for the develop branch as the dedicated CD build_develop workflow handles that
                # Skip the non-linux builds for pull requests as Windows is awfully slow, so run in merge queue only
                exclude:
                    - isDevelop: true
                      image: ubuntu-24.04
                    - isPullRequest: true
                      image: windows-2022
                    - isPullRequest: true
                      image: macos-14
        runs-on: ${{ matrix.image }}
        defaults:
            run:
                shell: bash
        steps:
            - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6
              with:
                  persist-credentials: false

            - uses: pnpm/action-setup@41ff72655975bd51cab0327fa583b6e92b6d3061 # v4
            - uses: actions/setup-node@6044e13b5dc448c55e2357c09f80417699197238 # v6
              with:
                  # Disable cache on Windows as it is slower than not caching
                  # https://github.com/actions/setup-node/issues/975
                  cache: ${{ runner.os != 'Windows' && 'pnpm' || '' }}
                  node-version: "lts/*"

            - name: Fetch layered build
              run: ./scripts/layered.sh

            - name: Copy config
              working-directory: apps/web
              run: cp element.io/develop/config.json config.json

            - name: Build
              working-directory: apps/web
              env:
                  CI_PACKAGE: true
              run: VERSION=$(scripts/get-version-from-git.sh) pnpm build

            - name: Upload Artifact
              uses: actions/upload-artifact@bbbca2ddaa5d8feaa63e36b76fdaad77386f024f # v7
              with:
                  name: webapp-${{ matrix.image }}
                  path: apps/web/webapp
                  retention-days: 1
