name: Build Nightly APK

on:
  schedule:
    - cron: '0 4 * * *'
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: set up JDK 17
        uses: actions/setup-java@v3
        with:
          java-version: '17'
          distribution: 'temurin'
          cache: gradle
      - name: Setup Gradle Cache
        uses: actions/cache@v3
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-${{ hashFiles('**/*.gradle*') }}-${{ hashFiles('**/gradle/wrapper/gradle-wrapper.properties') }}-${{ hashFiles('**/buildSrc/**/*.kt') }}
      - name: Grant execute permission for gradlew
        run: chmod +x gradlew
      - name: Decode Keystore
        env:
          ENCODED_STRING: ${{ secrets.KEYSTORE }}
        run: |
          TMP_KEYSTORE_FILE_PATH="${RUNNER_TEMP}"/keystore
          mkdir "${TMP_KEYSTORE_FILE_PATH}"
          echo $ENCODED_STRING | base64 -di > "${TMP_KEYSTORE_FILE_PATH}"/keystore.jks
      - name: Override version code based on date
        run: echo "VERSION_CODE_OVERRIDE=$(date +%Y%m%d00)" >> "$GITHUB_ENV"
      - name: Build and sign APK
        env:
          KEYSTORE_PASSWORD: ${{ secrets.KEYSTORE_PASSWORD }}
          SIGNING_KEY_ALIAS: ${{ secrets.SIGNING_KEY_ALIAS }}
          SIGNING_KEY_PASSWORD: ${{ secrets.SIGNING_KEY_PASSWORD }}
        run: ./gradlew assembleDefaultNightly
      - name: Upload APK
        uses: actions/upload-artifact@v1
        with:
          name: de.mm20.launcher2.nightly.apk
          path: app/app/build/outputs/apk/default/nightly/app-default-nightly.apk

