name: Nightly Release
on:
  # This can be used to automatically publish nightlies at UTC nighttime
  schedule:
    - cron: "0 2 * * *" # run at 2 AM UTC
  # This can be used to allow manually triggering nightlies from the web interface
  workflow_dispatch:
  push:
    branches:
      - renovate/electron-forge*
  pull_request:
    paths:
      - .github/actions/build-electron/*
      - .github/workflows/nightly.yml
      - forge.config.ts

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

env:
  GITHUB_RELEASE_ID: 179589950

permissions:
  contents: write

jobs:
  nightly-electron:
    if: github.repository == ${{ vars.REPO_MAIN }}
    name: Deploy nightly
    strategy:
      fail-fast: false
      matrix:
        arch: [x64, arm64]
        os:
          - name: macos
            image: macos-latest
            shell: bash
            forge_platform: darwin
          - name: linux
            image: ubuntu-22.04
            shell: bash
            forge_platform: linux
          - name: windows
            image: win-signing
            shell: cmd
            forge_platform: win32
        # Exclude ARM64 Linux from default matrix to use native runner
        exclude:
          - arch: arm64
            os:
              name: linux
        # Add ARM64 Linux with native ubuntu-24.04-arm runner for better-sqlite3 compatibility
        include:
          - arch: arm64
            os:
              name: linux
              image: ubuntu-24.04-arm
              shell: bash
              forge_platform: linux
    runs-on: ${{ matrix.os.image }}
    steps:
      - uses: actions/checkout@v6
      - uses: pnpm/action-setup@v4
      - name: Set up node & dependencies
        uses: actions/setup-node@v6
        with:
          node-version: 24
          cache: 'pnpm'
      - name: Install dependencies
        run: pnpm install --frozen-lockfile
      - name: Update nightly version
        run: pnpm run chore:ci-update-nightly-version
      - name: Run the build
        uses: ./.github/actions/build-electron
        with:
          os: ${{ matrix.os.name }}
          arch: ${{ matrix.arch }}
          shell: ${{ matrix.os.shell }}
          forge_platform: ${{ matrix.os.forge_platform }}
        env:
          APPLE_APP_CERTIFICATE_BASE64: ${{ secrets.APPLE_APP_CERTIFICATE_BASE64 }}
          APPLE_APP_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_APP_CERTIFICATE_PASSWORD }}
          APPLE_INSTALLER_CERTIFICATE_BASE64: ${{ secrets.APPLE_INSTALLER_CERTIFICATE_BASE64 }}
          APPLE_INSTALLER_CERTIFICATE_PASSWORD: ${{ secrets.APPLE_INSTALLER_CERTIFICATE_PASSWORD }}
          APPLE_TEAM_ID: ${{ secrets.APPLE_TEAM_ID }}
          APPLE_ID: ${{ secrets.APPLE_ID }}
          APPLE_ID_PASSWORD: ${{ secrets.APPLE_ID_PASSWORD }}
          WINDOWS_SIGN_EXECUTABLE: ${{ vars.WINDOWS_SIGN_EXECUTABLE }}
          GPG_SIGNING_KEY: ${{ secrets.GPG_SIGN_KEY }}

      - name: Publish release
        uses: softprops/action-gh-release@v2.5.0
        if: ${{ github.event_name != 'pull_request' }}
        with:
          make_latest: false
          prerelease: true
          draft: false
          fail_on_unmatched_files: true
          files: apps/desktop/upload/*.*
          tag_name: nightly
          name: Nightly Build

      - name: Publish artifacts
        uses: actions/upload-artifact@v5
        if: ${{ github.event_name == 'pull_request' }}
        with:
          name: TriliumNotes ${{ matrix.os.name }} ${{ matrix.arch }}
          path: apps/desktop/upload

  nightly-server:
    if: github.repository == ${{ vars.REPO_MAIN }}
    name: Deploy server nightly
    strategy:
      fail-fast: false
      matrix:
        arch: [x64, arm64]
        include:
          - arch: x64
            runs-on: ubuntu-22.04
          - arch: arm64
            runs-on: ubuntu-24.04-arm
    runs-on: ${{ matrix.runs-on }}
    steps:
      - uses: actions/checkout@v6

      - name: Run the build
        uses: ./.github/actions/build-server
        with:
          os: linux
          arch: ${{ matrix.arch }}

      - name: Publish release
        uses: softprops/action-gh-release@v2.5.0
        if: ${{ github.event_name != 'pull_request' }}
        with:
          make_latest: false
          prerelease: true
          draft: false
          fail_on_unmatched_files: true
          files: upload/*.*
          tag_name: nightly
          name: Nightly Build
