name: Build Electron Desktop

on:
  workflow_dispatch:
  push:
    branches:
      - electron-notary-test
    paths:
      - apps/app/**
      - apps/desktop/**
      - packages/ui/**
      - constants.json
      - pnpm-lock.yaml
      - package.json
      - .github/workflows/build-electron-desktop.yml

permissions:
  contents: read

jobs:
  build-electron:
    strategy:
      fail-fast: false
      matrix:
        include:
          - os: macos-latest
            artifact: macos
          - os: ubuntu-latest
            artifact: linux
          - os: windows-latest
            artifact: windows
          - os: windows-11-arm
            artifact: windows-arm64
    runs-on: ${{ matrix.os }}

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      # pnpm must be installed BEFORE setup-node so setup-node can find the
      # pnpm binary on PATH. Matches the pattern in build-desktop.yml so
      # the two workflows share the same bootstrap story.
      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 11.4.0

      - name: Setup Node
        uses: actions/setup-node@v6
        with:
          node-version: 24

      - name: Setup Bun
        uses: oven-sh/setup-bun@v2
        with:
          # Bun >= 1.3.10 is required by the server build and package-time helpers.
          bun-version: 1.3.10

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Package Electron desktop (unpacked)
        run: pnpm --filter @openwork/desktop package:electron:dir

      - name: Upload Electron artifacts
        uses: actions/upload-artifact@v4
        with:
          name: openwork-electron-${{ matrix.artifact }}
          path: apps/desktop/dist-electron/**

  electron-macos-notarization-smoke:
    name: Electron macOS notarization smoke
    runs-on: macos-14

    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Setup pnpm
        uses: pnpm/action-setup@v4
        with:
          version: 11.4.0

      - name: Setup Node
        uses: actions/setup-node@v6
        with:
          node-version: 24

      - name: Setup Bun
        uses: oven-sh/setup-bun@v2
        with:
          bun-version: 1.3.9

      - name: Install dependencies
        run: pnpm install --frozen-lockfile

      - name: Write notary API key
        env:
          APPLE_NOTARY_API_KEY_P8_BASE64: ${{ secrets.APPLE_NOTARY_API_KEY_P8_BASE64 }}
        run: |
          set -euo pipefail

          NOTARY_KEY_PATH="$RUNNER_TEMP/AuthKey.p8"
          printf '%s' "$APPLE_NOTARY_API_KEY_P8_BASE64" | base64 --decode > "$NOTARY_KEY_PATH"
          chmod 600 "$NOTARY_KEY_PATH"

          echo "NOTARY_KEY_PATH=$NOTARY_KEY_PATH" >> "$GITHUB_ENV"

      - name: Package Electron desktop (signed + notarized, no publish)
        env:
          CSC_LINK: ${{ secrets.APPLE_CODESIGN_CERT_P12_BASE64 }}
          CSC_KEY_PASSWORD: ${{ secrets.APPLE_CODESIGN_CERT_PASSWORD }}
          MACOS_NOTARIZE: true
          APPLE_API_KEY: ${{ secrets.APPLE_NOTARY_API_KEY_ID }}
          APPLE_API_ISSUER: ${{ secrets.APPLE_NOTARY_API_ISSUER_ID }}
          APPLE_API_KEY_PATH: ${{ env.NOTARY_KEY_PATH }}
        run: |
          set -euo pipefail
          pnpm --filter @openwork/desktop run build:electron
          pnpm --dir apps/desktop exec electron-builder --config electron-builder.yml --mac dmg zip --publish never
