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:
    # These must be set for fetchdep.sh to get the right branch
    REPOSITORY: ${{ github.repository }}
    PR_NUMBER: ${{ github.event.pull_request.number }}
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@11bd71901bbe5b1630ceea73d27597364c9af683 # v4

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

            # Workaround for yarn install timeouts, especially on Windows
            - run: yarn config set network-timeout 300000

            - name: Fetch layered build
              id: layered_build
              env:
                  # tell layered.sh to check out the right sha of the JS-SDK & EW, if they were given one
                  JS_SDK_GITHUB_BASE_REF: ${{ inputs.matrix-js-sdk-sha }}
              run: |
                  scripts/layered.sh
                  JSSDK_SHA=$(git -C matrix-js-sdk rev-parse --short=12 HEAD)
                  VECTOR_SHA=$(git rev-parse --short=12 HEAD)
                  echo "VERSION=$VECTOR_SHA--js-$JSSDK_SHA" >> $GITHUB_OUTPUT

            - name: Copy config
              run: cp element.io/develop/config.json config.json

            - name: Build
              env:
                  CI_PACKAGE: true
                  VERSION: "${{ steps.layered_build.outputs.VERSION }}"
              run: |
                  yarn build

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