name: Android Client

on:
  workflow_dispatch:
  schedule:
    - cron: '20 5 * * 1,3,5' # 5:20 AM UTC time on every Monday, Wednesday and Friday
  pull_request:
    paths:
      - .github/workflows/client-android.yml
      - secrets/**
      - android/**
      - fastlane/**
      - Gemfile.lock
      - yarn.lock
  push:
    branches: [main, sdk-*]
    paths:
      - .github/workflows/client-android.yml
      - secrets/**
      - android/**
      - fastlane/**
      - Gemfile.lock
      - yarn.lock

concurrency:
  group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    # NOTE(kudo): [macos-temp] Temporarily use macos runner because the limited resource on linux runners does not allow to build Expo Go
    # runs-on: ubuntu-22.04
    runs-on: macos-13
    env:
      ORG_GRADLE_PROJECT_reactNativeArchitectures: x86_64
      # [macos-temp] Increase JVM memory because macOS runners have more memory available
      # GRADLE_OPTS: -Dorg.gradle.jvmargs="-Xmx3072m -XX:MaxMetaspaceSize=1024m"
      GRADLE_OPTS: -Dorg.gradle.jvmargs="-Xmx4096m -XX:MaxMetaspaceSize=4096m"
    steps:
      - name: 👀 Checkout
        uses: actions/checkout@v3
        with:
          submodules: true
      # [macos-temp]
      # - name: 🧹 Cleanup GitHub Linux runner disk space
      #   uses: ./.github/actions/cleanup-linux-disk-space
      - name: 🔨 Use JDK 11
        uses: actions/setup-java@v3
        with:
          distribution: 'temurin'
          java-version: '11'
      - name: 💎 Setup Ruby and install gems
        uses: ruby/setup-ruby@v1
        with:
          bundler-cache: true
          ruby-version: 3.2.2
      - name: ♻️ Restore caches
        uses: ./.github/actions/expo-caches
        id: expo-caches
        with:
          yarn-workspace: 'true'
          yarn-tools: 'true'
          gradle: 'true'
          hermes-engine-aar: 'true'
          react-native-gradle-downloads: 'true'
      - name: ➕ Add `bin` to GITHUB_PATH
        run: echo "$(pwd)/bin" >> $GITHUB_PATH
      - name: 🧶 Yarn install
        if: steps.expo-caches.outputs.yarn-workspace-hit != 'true'
        run: yarn install --frozen-lockfile
      - name: 🔓 Decrypt secrets if possible
        uses: ./.github/actions/expo-git-decrypt
        with:
          key: ${{ secrets.GIT_CRYPT_KEY_BASE64 }}
      - name: 🔎 Check which flavor to build
        id: flavor
        uses: dorny/paths-filter@v2
        with:
          # this action fails when base is not set on schedule event
          base: ${{ github.ref }}
          filters: |
            versioned:
              - android/versioned-abis/**
              - android/versioned-react-native/**
              - android/expoview/src/versioned/**
              - android/expoview/src/main/java/versioned/**
              - android/**/*.gradle
      - name: 🏭 Build APK
        env:
          IS_VERSIONED_FLAVOR: ${{ github.event_name == 'schedule' || steps.flavor.outputs.versioned == 'true' }}
          BUILD_TYPE: Debug
        run: |
          [[ "$IS_VERSIONED_FLAVOR" == "true" ]] && FLAVOR="Versioned" || FLAVOR="Unversioned"
          echo "Building: flavor[${FLAVOR}] type[${BUILD_TYPE}]"
          fastlane android build build_type:$BUILD_TYPE flavor:$FLAVOR sign:false
      - name: 📤 Upload APK artifact
        uses: actions/upload-artifact@v3
        with:
          name: android-apk
          path: android/app/build/outputs/apk
      - name: 💾 Store daemon logs for debugging crashes
        if: failure()
        uses: actions/upload-artifact@v3
        with:
          name: gradle-daemon-logs
          path: ~/.gradle/daemon
      - name: 🔔 Notify on Slack
        uses: 8398a7/action-slack@v3
        if: failure() && (github.event.ref == 'refs/heads/main' || startsWith(github.event.ref, 'refs/heads/sdk-'))
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SLACK_WEBHOOK_URL: ${{ secrets.slack_webhook_android }}
        with:
          channel: '#expo-android'
          status: ${{ job.status }}
          fields: job,message,ref,eventName,author,took
          author_name: Expo Go (Android)
