name: Build and publish an android app

on:
  # push:
  #   branches:
  #     - gitea

  schedule:
    # Two times per month
    - cron: '0 0 1,15 * *'

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

jobs:
  build-android-app:
    runs-on: ubuntu-latest
    if: github.repository == 'MultiMote/niimblue'

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - name: Setup GitHub CLI
        uses: dev-hanz-ops/install-gh-cli-action@v0.2.1
        with:
          gh-cli-version: 2.72.0

      - name: Setup Node
        uses: actions/setup-node@v4
        with:
          node-version: "20"
          cache: npm
          cache-dependency-path: ./package-lock.json

      - name: Setup JDK 21
        uses: actions/setup-java@v4
        with:
          java-version: '21'
          distribution: 'temurin'
          cache: gradle

      - name: Setup Android SDK
        uses: android-actions/setup-android@v3
        with:
          log-accepted-android-sdk-licenses: false

      - name: Add build tools to PATH for apksigner
        run: echo "$ANDROID_HOME/build-tools/34.0.0" >> $GITEA_PATH

      - name: Decode Keystore
        run: mkdir -p .keystore && echo $KEYSTORE_BASE_64 | base64 -d > .keystore/keystore.jks
        env:
          KEYSTORE_BASE_64: ${{ secrets.KEYSTORE_BASE_64 }}

      - name: Install dependencies
        run: npm ci

      - name: Install capacitor dependencies
        working-directory: ./capacitor
        run: npm ci

      - name: Gradle cache
        uses: actions/cache@v4
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
          restore-keys: |
            ${{ runner.os }}-gradle-

      - name: Build niimblue www
        working-directory: ./capacitor
        run: npm run build-www

      - name: Gradle cache
        uses: actions/cache@v4
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
          restore-keys: |
            ${{ runner.os }}-gradle-

      - name: Build and sign capacitor app
        working-directory: ./capacitor
        run: npm run build-android
        env:
          KEYSTORE_PATH: ${{ gitea.workspace }}/.keystore/keystore.jks
          KEYSTORE_ALIAS: ${{ secrets.KEYSTORE_ALIAS }}
          KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
          KEYSTORE_ALIAS_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
          APP_VERSION_CODE: ${{ gitea.run_number }}

      - name: Copy release apk
        run: cp ./capacitor/android/app/build/outputs/apk/release/app-release-signed.apk $RUNNER_TEMP/niimblues.apk

      - name: Update GitHub release
        run: gh release upload snapshot $RUNNER_TEMP/niimblues.apk -R MultiMote/niimblue --clobber
        env:
          GH_TOKEN: ${{ secrets.GH_UPLOAD_TOKEN }}


