name: Build and publish Java packages
on:
  release:
    types: [released]
  pull_request:
    paths:
      - .github/workflows/java-publish.yml

jobs:
  macos-arm64:
    name: Build on MacOS Arm64
    runs-on: macos-14
    timeout-minutes: 45
    defaults:
      run:
        working-directory: ./java/core/lancedb-jni
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - uses: Swatinem/rust-cache@v2
      - name: Install dependencies
        run: |
          brew install protobuf
      - name: Build release
        run: |
          cargo build --release
      - uses: actions/upload-artifact@v4
        with:
          name: liblancedb_jni_darwin_aarch64.zip
          path: target/release/liblancedb_jni.dylib
          retention-days: 1
          if-no-files-found: error
  linux-arm64:
    name: Build on Linux Arm64
    runs-on: warp-ubuntu-2204-arm64-8x
    timeout-minutes: 45
    defaults:
      run:
        working-directory: ./java/core/lancedb-jni
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - uses: Swatinem/rust-cache@v2
      - uses: actions-rust-lang/setup-rust-toolchain@v1
        with:
          toolchain: "1.81.0"
          cache-workspaces: "./java/core/lancedb-jni"
          # Disable full debug symbol generation to speed up CI build and keep memory down
          # "1" means line tables only, which is useful for panic tracebacks.
          rustflags: "-C debuginfo=1"
      - name: Install dependencies
        run: |
          sudo apt -y -qq update
          sudo apt install -y protobuf-compiler libssl-dev pkg-config
      - name: Build release
        run: |
          cargo build --release
      - uses: actions/upload-artifact@v4
        with:
          name: liblancedb_jni_linux_aarch64.zip
          path: target/release/liblancedb_jni.so
          retention-days: 1
          if-no-files-found: error
  linux-x86:
    runs-on: warp-ubuntu-2204-x64-8x
    timeout-minutes: 30
    needs: [macos-arm64, linux-arm64]
    defaults:
      run:
        working-directory: ./java
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - uses: Swatinem/rust-cache@v2
      - name: Set up Java 8
        uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 8
          cache: "maven"
          server-id: ossrh
          server-username: SONATYPE_USER
          server-password: SONATYPE_TOKEN
          gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY }}
          gpg-passphrase: ${{ secrets.GPG_PASSPHRASE }}
      - name: Install dependencies
        run: |
          sudo apt -y -qq update
          sudo apt install -y protobuf-compiler libssl-dev pkg-config
      - name: Download artifact
        uses: actions/download-artifact@v4
      - name: Copy native libs
        run: |
          mkdir -p ./core/target/classes/nativelib/darwin-aarch64 ./core/target/classes/nativelib/linux-aarch64
          cp ../liblancedb_jni_darwin_aarch64.zip/liblancedb_jni.dylib ./core/target/classes/nativelib/darwin-aarch64/liblancedb_jni.dylib
          cp ../liblancedb_jni_linux_aarch64.zip/liblancedb_jni.so ./core/target/classes/nativelib/linux-aarch64/liblancedb_jni.so
      - name: Dry run
        if: github.event_name == 'pull_request'
        run: |
          mvn --batch-mode -DskipTests -Drust.release.build=true package
      - name: Set github
        run: |
          git config --global user.email "LanceDB Github Runner"
          git config --global user.name "dev+gha@lancedb.com"
      - name: Publish with Java 8
        if: github.event_name == 'release'
        run: |
          echo "use-agent" >> ~/.gnupg/gpg.conf
          echo "pinentry-mode loopback" >> ~/.gnupg/gpg.conf
          export GPG_TTY=$(tty)
          mvn --batch-mode -DskipTests -Drust.release.build=true -DpushChanges=false -Dgpg.passphrase=${{ secrets.GPG_PASSPHRASE }} deploy -P deploy-to-ossrh
        env:
          SONATYPE_USER: ${{ secrets.SONATYPE_USER }}
          SONATYPE_TOKEN: ${{ secrets.SONATYPE_TOKEN }}
