name: Create release
on:
  push:
    tags:
      - v*

concurrency:
  group: release-${{ github.ref }}
  cancel-in-progress: true

env:
  prerelease: ${{ contains(github.ref_name, '-alpha') || contains(github.ref_name, '-beta') || contains(github.ref_name, '-rc') }}

jobs:
  build:
    name: Create release
    permissions:
      contents: write
      discussions: write
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-java@v4
        with:
          distribution: temurin
          java-version: 21
      - uses: gradle/actions/setup-gradle@v4

      - name: Prepare keystore
        run: echo ${{ secrets.android_keystore_base64 }} | base64 -d >$GITHUB_WORKSPACE/keystore.jks

      - name: Build signed package
        # Use build cache to speed up building of build variants, but clean caches from previous tests before
        run: ./gradlew --build-cache --configuration-cache --no-daemon app:clean app:assembleRelease
        env:
          ANDROID_KEYSTORE: ${{ github.workspace }}/keystore.jks
          ANDROID_KEYSTORE_PASSWORD: ${{ secrets.android_keystore_password }}
          ANDROID_KEY_ALIAS: ${{ secrets.android_key_alias }}
          ANDROID_KEY_PASSWORD: ${{ secrets.android_key_password }}

      - name: Create Github release
        uses: softprops/action-gh-release@v2
        with:
          prerelease: ${{ env.prerelease }}
          files: app/build/outputs/apk/ose/release/*.apk
          fail_on_unmatched_files: true
          generate_release_notes: true
