name: CI

on: [push, pull_request]

jobs:
  linux:
    runs-on: ubuntu-22.04

    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
    - uses: actions/setup-java@v3
      with:
        java-version: '17'
        distribution: 'liberica'
        cache: 'gradle'
    - name: Build & Test
      run: xvfb-run -a ./gradlew --info test linux
    - name: Extract CHANGELOG
      run: sed '/^$/q' CHANGELOG > build/libs/README

    - uses: actions/upload-artifact@v4
      with:
        name: Linux binaries
        path: build/libs

    - name: Release (if tag)
      uses: softprops/action-gh-release@v1
      if: startsWith(github.ref, 'refs/tags/')
      with:
        draft: true
        body_path: build/libs/README
        files: build/libs/*

  mac:
    runs-on: macos-15

    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
    - uses: actions/setup-java@v3
      with:
        java-version: '17'
        distribution: 'liberica'
        architecture: aarch64
        cache: 'gradle'
    - uses: actions/setup-java@v3
      with:
        java-version: '17'
        distribution: 'liberica'
        architecture: x64

    - run: ./gradlew test macX86 --info
    - run: JLINK_OPTS="--module-path $JAVA_HOME_17_AARCH64/jmods" ./gradlew macArm64 --info

      env:
        APPLE_USER: ${{ secrets.APPLE_USER }}
        APPLE_PASSWORD: ${{ secrets.APPLE_PASSWORD }}

    - uses: actions/upload-artifact@v4
      with:
        name: macOS binaries
        path: build/libs

    - name: Add to release (if tag)
      uses: softprops/action-gh-release@v1
      if: startsWith(github.ref, 'refs/tags/')
      with:
        draft: true
        files: build/libs/*.zip

  windows:
    runs-on: windows-2022

    steps:
    - uses: actions/checkout@v3
      with:
        fetch-depth: 0
    - uses: actions/setup-java@v3
      with:
        java-version: '17'
        distribution: 'liberica'
        architecture: x64
        cache: 'gradle'
    - run: ./gradlew test win-installer --info

    - uses: actions/upload-artifact@v4
      with:
        name: Windows binaries
        path: build/libs

    - name: Add to release (if tag)
      uses: softprops/action-gh-release@v1
      if: startsWith(github.ref, 'refs/tags/')
      with:
        draft: true
        files: build/libs/*.exe
