name: Android Client - EAS Build

on:
  workflow_dispatch:
    inputs:
      buildType:
        required: true
        type: 'choice'
        description: 'buildType'
        options:
          - versioned-client
          - unversioned-client
          - versioned-client-add-sdk
  schedule:
    # 5:20 AM UTC time on every Monday, Wednesday and Friday
    # Build a versioned client
    - cron: '20 5 * * 1,3,5'
    # 5:20 AM UTC time on every Monday
    # Run versioning process for the next sdk and build a versioned client
    - cron: '20 5 * * 1'
  pull_request:
    paths:
      - .github/workflows/client-android-eas.yml
      - apps/eas-expo-go/**
      - android/versioned-abis/**
      - android/versioned-react-native/**
      - android/expoview/**
      - android/**/*.gradle
  push:
    branches: [main, sdk-*]
    paths:
      - .github/workflows/client-android-eas.yml
      - apps/eas-expo-go/**
      - android/versioned-abis/**
      - android/versioned-react-native/**
      - android/expoview/**
      - android/**/*.gradle

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

jobs:
  build:
    runs-on: ubuntu-22.04
    if: ${{ github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]' }}
    steps:
      - name: 👀 Checkout
        uses: actions/checkout@v3
        with:
          submodules: true
      - name: 🔓 Decrypt secrets if possible
        uses: ./.github/actions/expo-git-decrypt
        with:
          key: ${{ secrets.GIT_CRYPT_KEY_BASE64 }}
      - name: ➕ Add `bin` to GITHUB_PATH
        run: echo "$(pwd)/bin" >> $GITHUB_PATH
      - name: ♻️ Restore caches
        uses: ./.github/actions/expo-caches
        id: expo-caches
        with:
          yarn-workspace: 'true'
          yarn-tools: 'true'
      - name: 🧶 Yarn install
        if: steps.expo-caches.outputs.yarn-workspace-hit != 'true'
        run: yarn install --frozen-lockfile
      - name: Install eas-cli
        run: npm install -g eas-cli
      - 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: Resolve profile
        id: profile
        run: |
          DISPATCH_PROFILE="${{ github.event.inputs.buildType }}"
          IS_VERSIONED="${{ steps.flavor.outputs.versioned }}"
          if [[ ! -z "$DISPATCH_PROFILE" ]]; then
            echo "profile=$DISPATCH_PROFILE" >> $GITHUB_OUTPUT
          elif [[ "${{ github.event.schedule }}" == "20 5 * * 1,3,5" ]]; then
            echo "profile=versioned-client" >> $GITHUB_OUTPUT
          elif [[ "${{ github.event.schedule }}" == "20 5 * * 1" ]]; then
            echo "profile=versioned-client-add-sdk" >> $GITHUB_OUTPUT
          elif [[ "$IS_VERSIONED" == "true" ]]; then
            echo "profile=versioned-client" >> $GITHUB_OUTPUT
          else
            echo "profile=unversioned-client" >> $GITHUB_OUTPUT
          fi
      - name: Generate local credentials.json
        working-directory: ./apps/eas-expo-go
        run: |
          cat >credentials.json <<EOL
          {
            "android": {
              "keystore": {
                "keystorePath": "release.keystore",
                "keystorePassword": "$ANDROID_KEYSTORE_PASSWORD",
                "keyAlias": "ExponentKey",
                "keyPassword": "$ANDROID_KEY_PASSWORD"
              }
            }
          }
          EOL
          echo $ANDROID_KEYSTORE_B64 | base64 -d > release.keystore
        env:
          ANDROID_KEYSTORE_B64: ${{ secrets.ANDROID_KEYSTORE_B64 }}
          ANDROID_KEYSTORE_PASSWORD: ${{ secrets.ANDROID_KEYSTORE_PASSWORD }}
          ANDROID_KEY_PASSWORD: ${{ secrets.ANDROID_KEY_PASSWORD }}
      - name: Build
        uses: ./.github/actions/eas-build
        id: build
        with:
          platform: 'android'
          profile: ${{ steps.profile.outputs.profile  }}
          projectRoot: './apps/eas-expo-go'
          expoToken: ${{ secrets.EAS_BUILD_BOT_TOKEN }}
          noWait: ${{ github.event.schedule }}
          message: ${{ github.event.pull_request.title }}
      - name: On workflow canceled
        if: ${{ cancelled() && steps.build.outputs.build_id }}
        run: eas build:cancel ${{ steps.build.outputs.build_id }}
        working-directory: ./apps/eas-expo-go
        env:
          EXPO_TOKEN: ${{ secrets.EAS_BUILD_BOT_TOKEN }}
          EAS_BUILD_PROFILE: ${{ steps.profile.outputs.profile  }}
