name: Github Release Workflow

on:
  push:
    tags:
      - '[0-9]+.[0-9]+.[0-9]+'

jobs:
  setup:
    runs-on: ubuntu-latest
    steps:
      - name: Setup JDK 8
        uses: actions/setup-java@v3
        with:
          distribution: 'temurin'
          java-version: 8

  build:
    needs: setup
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      - name: Cache Gradle and wrapper
        uses: actions/cache@v3
        with:
          path: |
            ~/.gradle/caches
            ~/.gradle/wrapper
          key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}

      - name: Make gradlew executable
        run: chmod +x ./gradlew

      - name: Build APK
        id: build
        run: bash ./gradlew assembleTempoRelease

      - name: Sign APK
        id: sign_apk
        uses: r0adkll/sign-android-release@v1
        with:
          releaseDirectory: app/build/outputs/apk/tempo/release
          signingKeyBase64: ${{ secrets.KEYSTORE_BASE64 }}
          alias: ${{ secrets.KEY_ALIAS_GITHUB }}
          keyStorePassword: ${{ secrets.KEYSTORE_PASSWORD }}
          keyPassword: ${{ secrets.KEY_PASSWORD_GITHUB }}

      - name: Make artifact
        uses: actions/upload-artifact@v2
        with:
          name: app-release-signed
          path: ${{steps.sign_apk.outputs.signedReleaseFile}}

#      - name: Build AAB
#        run: bash ./gradlew bundleRelease

#      - name: Sign AAB
#        id: sign_aab
#        uses: r0adkll/sign-android-release@v1
#        with:
#          releaseDirectory: app/build/outputs/bundle/release
#          signingKeyBase64: ${{ secrets.KEYSTORE_BASE64 }}
#          alias: ${{ secrets.KEY_ALIAS_GITHUB }}
#          keyStorePassword: ${{ secrets.KEYSTORE_PASSWORD }}
#          keyPassword: ${{ secrets.KEY_PASSWORD_GITHUB }}

#      - name: Make artifact
#        uses: actions/upload-artifact@v2
#        with:
#          name: app-release-signed
#          path: ${{steps.sign_aab.outputs.signedReleaseFile}}

#      - name: Build Changelog
#        id: changelog
#        uses: ardalanamini/auto-changelog@v3
#        with:
#          mention-authors: false
#          mention-new-contributors: false
#          include-compare: false
#          semver: false

      - name: Create Release
        id: create_release
        uses: actions/create-release@v1
        with:
          tag_name: ${{ github.ref }}
          release_name: Release v${{ github.ref }}
          body: '> Changelog coming soon'
#          body: ${{ steps.changelog.outputs.changelog }}
        env:
          GITHUB_TOKEN: ${{ github.token }}

      - name: Upload APK
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ github.token }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }}
          asset_path: ${{steps.sign_apk.outputs.signedReleaseFile}}
          asset_name: app-tempo-release.apk
          asset_content_type: application/zip

#      - name: Upload AAB
#        uses: actions/upload-release-asset@v1
#        env:
#          GITHUB_TOKEN: ${{ github.token }}
#        with:
#          upload_url: ${{ steps.create_release.outputs.upload_url }}
#          asset_path: ${{steps.sign_aab.outputs.signedReleaseFile}}
#          asset_name: app-release.aab
#          asset_content_type: application/zip
