on:
  pull_request:
    branches:
      - main
    paths:
      - frontend/**


name: Build and release debug APK

defaults:
  run:
    working-directory: frontend/android


jobs:
  build:
    runs-on: macos
    env:
      # $BUNDLE_GEMFILE must be set at the job level, so it is set for all steps
      BUNDLE_GEMFILE: ${{ gitea.workspace }}/frontend/android/Gemfile

    steps:
    - uses: https://gitea.com/actions/checkout@v4

    - uses: https://github.com/actions/setup-java@v4
      with:
        java-version: '17'
        distribution: 'zulu'

    - name: Setup Android SDK
      uses: https://github.com/android-actions/setup-android@v3

    - name: Fix flutter SDK folder permission
      run: git config --global --add safe.directory "*"

    - uses: https://github.com/subosito/flutter-action@v2
      with:
        channel: stable
        flutter-version-file: ${{ gitea.workspace }}/frontend/pubspec.yaml
        architecture: x64
        cache: true

    - name: Install dependencies and clean up
      run: |
        flutter pub get
        flutter clean

    - name: Set up ruby env and install fastlane
      uses: https://github.com/ruby/setup-ruby@v1
      with:
        ruby-version: 3.3
        bundler-cache: true # runs 'bundle install' and caches installed gems automatically

    - name: Infer version number from git tag
      id: version
      env: 
        REF_NAME: ${{ gitea.ref_name }}
      run:
        # remove the 'v' prefix from the tag name
        echo "BUILD_NAME=${REF_NAME//v}" >> $GITHUB_ENV

    - name: Add required secret files
      run: |
        echo "${{ secrets.ANDROID_SECRET_PROPERTIES_BASE64 }}" | base64 -d > secrets.properties
        echo "${{ secrets.ANDROID_GOOGLE_PLAY_JSON_BASE64 }}" | base64 -d > google-key.json
        echo "${{ secrets.ANDROID_KEYSTORE_BASE64 }}" | base64 -d > release.keystore

    - name: Run fastlane lane
      run: bundle exec fastlane deploy_beta
      env:
        BUILD_NUMBER: ${{ gitea.run_number }}
        # BUILD_NAME is implicitly available
        ANDROID_GOOGLE_MAPS_API_KEY: ${{ secrets.ANDROID_GOOGLE_MAPS_API_KEY }}