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

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

jobs:

  build_and_release_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: Update Flutter version
      run: flutter upgrade

    - name: Show Flutter version
      run: flutter --version

    - name: Get depenencies
      run: flutter pub get

    - name: Upgrade depenencies
      run: flutter pub upgrade

    - name: Show outdated depenencies
      run: flutter pub outdated

    - name: Start release build
      run: flutter build apk

    - 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.apk.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.apk.sha1
