🏡 index : quine-global/hyper.git

name: 'Build'
description: 'Builds, tests, and packages the app for release'
inputs:
  node-version:
    description: 'Node.js version to use'
    required: true
  matrix-name:
    description: 'Matrix Name'
    required: true
  matrix-os:
    description: 'Matrix OS'
    required: true
  upload-artifact:
    description: 'Whether to upload artifacts'
    required: false
    default: 'true'
runs:
  using: 'composite'
  steps:
    - name: Checkout
      uses: actions/checkout@v6.0.2

    - name: Use Node.js ${{ env.NODE_VERSION }}
      uses: actions/setup-node@v6.4.0
      with:
        node-version: ${{ env.NODE_VERSION }}

    - name: Enable pnpm
      uses: pnpm/action-setup@v4

    - name: Set up Python 3.10
      uses: actions/setup-python@v6.2.0
      with:
        python-version: '3.10'

    - name: Fix node-gyp and Python
      shell: bash
      run: |
        if [[ "$RUNNER_OS" == "macOS" ]]; then
          brew install python-setuptools python-packaging
        else
          python3 -m pip install $EXTRA_ARGS packaging setuptools
        fi

    - name: Get pnpm store directory path
      shell: bash
      id: pnpm-store-dir-path
      run: echo "dir=$(pnpm store path --silent)" >> $GITHUB_OUTPUT

    - uses: actions/cache/restore@v5.0.5
      with:
        path: ${{ steps.pnpm-store-dir-path.outputs.dir }}
        key: ${{ runner.os }}-pnpm-store-${{ hashFiles('pnpm-lock.yaml', 'app/pnpm-lock.yaml') }}
        restore-keys: |
          ${{ runner.os }}-pnpm-store-

    - name: Delete old electron headers
      # These can sometimes be present on a freshly-provisioned github runner, so remove them
      shell: bash
      run: |
        rm -rf ~/.electron-gyp || true

    - name: Install
      shell: bash
      run: pnpm install --frozen-lockfile
      env:
        npm_config_node_gyp: ${{ github.workspace }}${{ runner.os == 'Windows' && '\node_modules\node-gyp\bin\node-gyp.js' || '/node_modules/node-gyp/bin/node-gyp.js' }}

    - name: Install libarchive-tools
      shell: bash
      if: runner.os == 'Linux'
      run: |
        sudo apt update
        sudo apt install libarchive-tools

    - name: Install Flatpak build tools
      shell: bash
      if: runner.os == 'Linux'
      run: |
        sudo apt install -y flatpak flatpak-builder
        flatpak remote-add --user --if-not-exists flathub https://flathub.org/repo/flathub.flatpakrepo

    - name: Cache Flatpak runtimes
      if: runner.os == 'Linux'
      uses: actions/cache@v5.0.5
      with:
        path: ~/.local/share/flatpak
        key: flatpak-runtimes-${{ runner.os }}-25.08

    - name: Rebuild native modules for Electron
      shell: bash
      run: npx electron-rebuild

    - name: Lint and Run Unit Tests
      shell: bash
      run: pnpm run test

    - name: Getting Build Icon
      shell: bash
      if: github.ref == 'refs/heads/canary' || github.base_ref == 'canary'
      run: |
        cp build/canary.ico build/icon.ico
        cp build/canary.icns build/icon.icns

    - name: Build
      shell: bash
      run: |
        if [ -z "$CSC_LINK" ] ; then unset CSC_LINK ; fi
        if [ -z "$CSC_KEY_PASSWORD" ] ; then unset CSC_KEY_PASSWORD ; fi
        if [ -z "$WIN_CSC_LINK" ] ; then unset WIN_CSC_LINK ; fi
        if [ -z "$WIN_CSC_KEY_PASSWORD" ] ; then unset WIN_CSC_KEY_PASSWORD ; fi
        if [ -z "$APPLE_ID" ] ; then unset APPLE_ID ; fi
        if [ -z "$APPLE_APP_SPECIFIC_PASSWORD" ] ; then unset APPLE_APP_SPECIFIC_PASSWORD ; fi

        pnpm run dist
      env:
        GH_TOKEN: ${{ env.GH_TOKEN }}
        CSC_LINK: ${{ env.MAC_CERT_P12_BASE64 }}
        CSC_KEY_PASSWORD: ${{ env.MAC_CERT_P12_PASSWORD }}
        WIN_CSC_LINK: ${{ env.WIN_CERT_P12_BASE64 }}
        WIN_CSC_KEY_PASSWORD: ${{ env.WIN_CERT_P12_PASSWORD }}
        APPLE_ID: ${{ env.APPLE_ID }}
        APPLE_APP_SPECIFIC_PASSWORD: ${{ env.APPLE_PASSWORD }}

    - name: Archive Build Artifacts
      uses: actions/upload-artifact@v7.0.1
      with:
        name: hyper-${{ runner.os }}-${{ inputs.matrix-name }}
        path: |
          dist/*.dmg
          dist/*.snap
          dist/*.AppImage
          dist/*.deb
          dist/*.pacman
          dist/*.rpm
          dist/*.exe
          dist/*.flatpak

    - name: Run E2E Tests
      shell: bash
      if: runner.os != 'Linux'
      run: pnpm run test:e2e --verbose

    - name: Run E2E Tests on Linux
      if: runner.os == 'Linux'
      shell: bash
      run: xvfb-run -a pnpm run test:e2e
      env:
        SHELL: /bin/bash
        DEBUG: "pw:browser*"

    - name: Archive E2E test screenshot
      uses: actions/upload-artifact@v7.0.1
      with:
        name: e2e-${{ inputs.matrix-os }}-${{ strategy.job-index }}
        path: dist/tmp/*.png

    - name: Save the pr number in an artifact
      shell: bash
      if: github.event_name == 'pull_request'
      env:
        PR_NUM: ${{ github.event.number }}
      run: echo $PR_NUM > pr_num.txt

    - name: Upload the pr num
      uses: actions/upload-artifact@v7.0.1
      if: github.event_name == 'pull_request'
      with:
        name: pr_num
        path: ./pr_num.txt

    - uses: actions/cache/save@v5.0.5
      if: github.event_name != 'pull_request'
      with:
        path: ${{ steps.pnpm-store-dir-path.outputs.dir }}
        key: ${{ runner.os }}-pnpm-store-${{ hashFiles('pnpm-lock.yaml', 'app/pnpm-lock.yaml') }}