name: XGBoost-JVM-Tests

on: [push, pull_request]

permissions:
  contents: read # to fetch code (actions/checkout)

concurrency:
  group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}
  cancel-in-progress: true

jobs:
  test-with-jvm:
    name: Test JVM on OS ${{ matrix.os }}
    timeout-minutes: 30
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [windows-latest, ubuntu-latest, macos-13]

    steps:
    - uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6
      with:
        submodules: 'true'

    - uses: actions/setup-java@99b8673ff64fbf99d8d325f52d9a5bdedb8483e9  # v4.2.1
      with:
        distribution: 'temurin'
        java-version: '8'

    - uses: conda-incubator/setup-miniconda@a4260408e20b96e80095f42ff7f1a15b27dd94ca  # v3.0.4
      with:
        miniforge-variant: Mambaforge
        miniforge-version: latest
        activate-environment: jvm_tests
        environment-file: tests/ci_build/conda_env/jvm_tests.yml
        use-mamba: true

    - name: Cache Maven packages
      uses: actions/cache@0c45773b623bea8c8e75f6c82b208c3cf94ea4f9 # v4.0.2
      with:
        path: ~/.m2
        key: ${{ runner.os }}-m2-${{ hashFiles('./jvm-packages/pom.xml') }}
        restore-keys: ${{ runner.os }}-m2-${{ hashFiles('./jvm-packages/pom.xml') }}

    - name: Test XGBoost4J (Core)
      run: |
        cd jvm-packages
        mvn test -B -pl :xgboost4j_2.12

    - name: Test XGBoost4J (Core, Spark, Examples)
      run: |
        rm -rfv build/
        cd jvm-packages
        mvn -B test
      if: matrix.os == 'ubuntu-latest'  # Distributed training doesn't work on Windows

    - name: Extract branch name
      shell: bash
      run: |
        echo "branch=${GITHUB_REF#refs/heads/}" >> "$GITHUB_OUTPUT"
      id: extract_branch
      if: |
        (github.ref == 'refs/heads/master' || contains(github.ref, 'refs/heads/release_')) &&
        (matrix.os == 'windows-latest' || matrix.os == 'macos-13')

    - name: Publish artifact xgboost4j.dll to S3
      run: |
        cd lib/
        Rename-Item -Path xgboost4j.dll -NewName xgboost4j_${{ github.sha }}.dll
        dir
        python -m awscli s3 cp xgboost4j_${{ github.sha }}.dll s3://xgboost-nightly-builds/${{ steps.extract_branch.outputs.branch }}/libxgboost4j/ --acl public-read --region us-west-2
      if: |
        (github.ref == 'refs/heads/master' || contains(github.ref, 'refs/heads/release_')) &&
        matrix.os == 'windows-latest'
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID_IAM_S3_UPLOADER }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY_IAM_S3_UPLOADER }}

    - name: Publish artifact libxgboost4j.dylib to S3
      shell: bash -l {0}
      run: |
        cd lib/
        mv -v libxgboost4j.dylib libxgboost4j_${{ github.sha }}.dylib
        ls
        python -m awscli s3 cp libxgboost4j_${{ github.sha }}.dylib s3://xgboost-nightly-builds/${{ steps.extract_branch.outputs.branch }}/libxgboost4j/ --acl public-read --region us-west-2
      if: |
        (github.ref == 'refs/heads/master' || contains(github.ref, 'refs/heads/release_')) &&
        matrix.os == 'macos-13'
      env:
        AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID_IAM_S3_UPLOADER }}
        AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY_IAM_S3_UPLOADER }}

    - name: Build and Test XGBoost4J with scala 2.13
      run: |
        rm -rfv build/
        cd jvm-packages
        mvn -B clean install test -Pdefault,scala-2.13
      if: matrix.os == 'ubuntu-latest'  # Distributed training doesn't work on Windows
