name: Build Android APK

on:
  workflow_dispatch:
  push:
    branches: [ master ]
    paths-ignore:
      - '**.md'
      - '**.txt'
      - '.github/**'
      - '.idea/**'
      - '!.github/workflows/**'

jobs:
  build:
    name: Build APK
    runs-on: ubuntu-latest
    if: "! contains(github.event.head_commit.message, 'Merge remote-tracking branch')"

    steps:
      - name: Checkout code
        uses: actions/checkout@v3.5.3

      - name: Setup JDK 17
        uses: actions/setup-java@v3
        with:
          distribution: "temurin"
          java-version: 17
          cache: 'gradle'

      - name: Setup cmake
        run: |
          echo "y" | sudo ${ANDROID_HOME}/tools/bin/sdkmanager --install "cmake;3.22.1" --sdk_root=${ANDROID_SDK_ROOT} &> /dev/null
          echo "sdk.dir=${ANDROID_HOME}" > local.properties

      - name: Setup Gradle
        uses: gradle/gradle-build-action@v2.7.0

      - name: Build with Gradle
        run: |
          echo ${{ secrets.SIGN_KEYSTORE_BASE64 }} | base64 -d > keystore.jks
          chmod +x ./gradlew
          ./gradlew :app:assembleAppRelease --build-cache --parallel --daemon --warning-mode all
           echo "APK_FILE=$(find app/build/outputs/apk/app/release -name '*.apk')" >> $GITHUB_ENV
        env:
          KEYSTORE_PATH: "../keystore.jks"
          KEYSTORE_PASSWORD: ${{ secrets.SIGN_KEYSTORE_PASSWORD }}
          KEY_ALIAS: ${{ secrets.SIGN_ALIAS }}
          KEY_PASSWORD: ${{ secrets.SIGN_KEY_PASSWORD }}

      - name: Show Artifacts SHA256
        run: |
          echo "### Build Success :rocket:" >> $GITHUB_STEP_SUMMARY
          echo "|ABI|SHA256|" >> $GITHUB_STEP_SUMMARY
          echo "|:--------:|:----------|" >> $GITHUB_STEP_SUMMARY
          arm64=($(sha256sum ${{ env.APK_FILE }}))
          echo "|arm64|$arm64" >> $GITHUB_STEP_SUMMARY
      
      - name: Upload APK RELEASE
        uses: actions/upload-artifact@v3
        with:
          name: Shamrock-release-${{ github.event.head_commit.id }}-arm64
          path: ${{ env.APK_FILE }}
