name: Android

on:
  pull_request:
    branches:
      - 'master'
  push:
    branches:
      - 'master'
    tags:
      - '*'

jobs:
  build-and-release:
    name: Build and release
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
        with:
          token: "${{ secrets.GITHUB_TOKEN }}"
          fetch-depth: 0
      - name: Set up JDK 17
        uses: actions/setup-java@v4
        with:
          java-version: 17
          distribution: 'temurin'
      - name: Get latest version
        run: |
          echo $(git describe --tags --abbrev=0)
          echo "VERSION=$(git describe --tags --abbrev=0)" >> $GITHUB_ENV
      - name: Setup Gradle
        uses: gradle/actions/setup-gradle@v4
      - name: Build bundle
        run: ./gradlew :app:bundleDevDebug
      - name: Build APK
        run: ./gradlew :app:assembleDevDebug
      - name: Upload APK and bundle
        uses: actions/upload-artifact@v4
        with:
          name: app
          path: app/build/outputs/
      - name: Create release
        if: startsWith(github.ref, 'refs/tags/')
        uses: marvinpinto/action-automatic-releases@latest
        with:
          repo_token: "${{ secrets.GITHUB_TOKEN }}"
          prerelease: false
          automatic_release_tag: "${{ env.VERSION }}"
          title: "${{ env.VERSION }}"
          files: |
            app/build/outputs/apk/dev/debug/app-dev-debug.apk
            app/build/outputs/bundle/devDebug/app-dev-debug.aab
