name: Publish Android packages

on:
  schedule:
    - cron: '0 9 * * *'
  workflow_dispatch:
    inputs:
        mode:
          description: 'staging/snapshot, default is snapshot'
          required: true
          default: 'snapshot'

jobs:
  release-android:
    if: github.repository == 'deepjavalibrary/djl'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          distribution: 'corretto'
          java-version: 17
      # Enable gradle cache: https://github.com/actions/cache/blob/master/examples.md#java---gradle
      - uses: actions/cache@v4
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-${{ hashFiles('*/build.gradle.kts', 'engines/**/build.gradle.kts', 'extensions/**/build.gradle.kts') }}
          restore-keys: |
            ${{ runner.os }}-gradle-
      - name: publish android core, pytorch-native and tokenizer-native to sonatype
        if: ${{ github.event.inputs.mode == '' || github.event.inputs.mode == 'snapshot' }}
        working-directory: android
        run: |
          ./gradlew :pytorch-native:assemble
          ./gradlew :tokenizer-native:assemble
          ./gradlew publish -Psnapshot
        env:
          ORG_GRADLE_PROJECT_signingKey: ${{ secrets.ORG_GRADLE_PROJECT_signingKey }}
          ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.ORG_GRADLE_PROJECT_signingPassword }}
          ORG_GRADLE_PROJECT_ossrhUsername: ${{ secrets.ORG_GRADLE_PROJECT_ossrhUsername }}
          ORG_GRADLE_PROJECT_ossrhPassword: ${{ secrets.ORG_GRADLE_PROJECT_ossrhPassword }}
      - name: publish android core, pytorch-native and tokenizer-native to sonatype
        if: ${{ github.event.inputs.mode == 'staging' }}
        working-directory: android
        run: |
          ./gradlew :pytorch-native:assemble
          ./gradlew :tokenizer-native:assemble
          ./gradlew publish -Pstaging
        env:
          ORG_GRADLE_PROJECT_signingKey: ${{ secrets.ORG_GRADLE_PROJECT_signingKey }}
          ORG_GRADLE_PROJECT_signingPassword: ${{ secrets.ORG_GRADLE_PROJECT_signingPassword }}
          ORG_GRADLE_PROJECT_ossrhUsername: ${{ secrets.ORG_GRADLE_PROJECT_ossrhUsername }}
          ORG_GRADLE_PROJECT_ossrhPassword: ${{ secrets.ORG_GRADLE_PROJECT_ossrhPassword }}
