name: Android Client

on:
  workflow_dispatch:
    inputs:
      releaseAPK:
        description: 'type "release-apk" to confirm upload to S3'
        required: false
      releaseGooglePlay:
        description: 'type "release-google-play" to confirm release to Google Play'
        required: false
  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
      - .ruby-version
      - yarn.lock
  push:
    branches: [main, sdk-*]
    paths:
      - .github/workflows/client-android.yml
      - secrets/**
      - android/**
      - fastlane/**
      - Gemfile.lock
      - .ruby-version
      - yarn.lock

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

jobs:
  build:
    runs-on: ubuntu-20.04
    env:
      GRADLE_OPTS: -Dorg.gradle.jvmargs=-Xmx3072m -XX:MaxMetaspaceSize=1024m
    steps:
      - name: 👀 Checkout
        uses: actions/checkout@v3
        with:
          submodules: true
      - 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
      - name: ♻️ Restore caches
        uses: ./.github/actions/expo-caches
        id: expo-caches
        with:
          yarn-workspace: 'true'
          yarn-tools: 'true'
          gradle: 'true'
          hermes-engine-aar: 'true'
          ndk: '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:
          ANDROID_KEYSTORE_B64: ${{ secrets.ANDROID_KEYSTORE_B64 }}
          ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }}
          ANDROID_KEY_ALIAS: ExponentKey
          ANDROID_KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }}
          IS_APP_BUNDLE: ${{ github.event.inputs.releaseGooglePlay == 'release-google-play' }}
          IS_RELEASE_BUILD: ${{ github.event.inputs.releaseAPK == 'release-apk' || github.event.inputs.releaseGooglePlay == 'release-google-play' }}
          IS_VERSIONED_FLAVOR: ${{ github.event_name == 'schedule' || steps.flavor.outputs.versioned == 'true' }}
        run: |
          if [ "$IS_RELEASE_BUILD" == "false" ]; then
            export ORG_GRADLE_PROJECT_reactNativeArchitectures="x86_64"
            BUILD_TYPE="Debug"
            echo "Using ABI filters: $ORG_GRADLE_PROJECT_reactNativeArchitectures"
          else
            BUILD_TYPE="Release"
          fi
          [[ "$IS_VERSIONED_FLAVOR" == "true" ]] && FLAVOR="Versioned" || FLAVOR="Unversioned"
          echo "Building with $FLAVOR flavor"
          if [ -z "$ANDROID_KEYSTORE_B64" ]; then
            echo "External build detected, APK will not be signed"
            fastlane android build build_type:$BUILD_TYPE flavor:$FLAVOR sign:false
          else
            echo "Internal build detected, APK will be signed"
            echo $ANDROID_KEYSTORE_B64 | base64 -d > android/app/release-key.jks
            fastlane android build build_type:$BUILD_TYPE flavor:$FLAVOR aab:$IS_APP_BUNDLE
          fi
      - 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: 📤 Upload APK to S3 and update staging versions endpoint
        if: ${{ github.event.inputs.releaseAPK == 'release-apk' }}
        run: expotools client-build --platform android --release
        env:
          AWS_ACCESS_KEY_ID: AKIAJ3SWUQ4QLNQC7FXA
          AWS_SECRET_ACCESS_KEY: ${{ secrets.android_client_build_aws_secret_key }}
          AWS_DEFAULT_REGION: 'us-east-1'
          EXPO_VERSIONS_SECRET: ${{ secrets.expo_versions_secret }}
      - name: 📤 Upload APK to Google Play and release to production
        if: ${{ github.event.inputs.releaseGooglePlay == 'release-google-play' }}
        run: fastlane android prod_release
        env:
          SUPPLY_JSON_KEY_DATA: ${{ secrets.SUPPLY_JSON_KEY_DATA }}
      - 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)
