name: Android Release
run-name: ${{ GITHUB.ACTOR }} is compiling an Android App 🚀

on:  
  workflow_dispatch:
  push:
    branches:
      - main
    paths:
      - "lib/**"

jobs:

  build_apk:
    runs-on: ubuntu-latest
    steps:

    - name: Checkout repository
      uses: actions/checkout@v3

    - name: Setup Java
      uses: actions/setup-java@v3
      with:
        distribution: "zulu"
        java-version: "12.x"
        cache: "gradle"

    - name: Setup Flutter
      uses: subosito/flutter-action@v2
      with:
        flutter-version: "3.0.0"
        channel: "stable"
        cache: true

    - name: Get depenencies
      run: flutter pub get

    - name: Start release build
      run: flutter build apk

  release_apk:
    needs: build_apk
    runs-on: ubuntu-latest
    steps:

      - name: Checkput repository
        uses: actions/checkout@v3

      - name: Upload APK as Artifact
        uses: actions/upload-artifact@v3
        with:
          name: Android APK
          path: |
            build/app/outputs/flutter-apk/app-release.apk
            build/app/outputs/flutter-apk/app-release.sha1

      - name: Upload APK as release
        uses: "marvinpinto/action-automatic-releases@latest"
        with:
          repo_token: "${{ secrets.GITHUB_TOKEN }}"
          automatic_release_tag: "latest"
          prerelease: true
          title: "Android APK"
          files: |
            build/app/outputs/flutter-apk/app-release.apk
            build/app/outputs/flutter-apk/app-release.sha1
