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
  pull_request:
    branches: [ master ]
    paths:
      - .github/workflows/client-android.yml
      - secrets/**
      - android/**
      - fastlane/**
      - tools-public/**
      - Gemfile.lock
      - .ruby-version
      - yarn.lock
  push:
    branches: [ master, sdk-* ]
    paths:
      - .github/workflows/client-android.yml
      - secrets/**
      - android/**
      - fastlane/**
      - tools-public/**
      - Gemfile.lock
      - .ruby-version
      - yarn.lock

jobs:
  build:
    runs-on: ubuntu-18.04
    steps:
      - uses: actions/checkout@v2
        with:
          submodules: true
      - name: Get yarn cache directory path
        id: yarn-cache-dir-path
        run: echo "::set-output name=dir::$(yarn cache dir)"
      - uses: actions/cache@v1
        with:
          path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
          key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}-${{ hashFiles('tools-public/yarn.lock') }}
          restore-keys: |
            ${{ runner.os }}-yarn-
      - run: yarn install --frozen-lockfile
      - run: yarn install --frozen-lockfile
        working-directory: tools-public
      - uses: ruby/setup-ruby@v1
      - name: bundler cache
        uses: actions/cache@v1
        with:
          path: vendor/bundle
          key: ${{ runner.os }}-gems-${{ hashFiles('.ruby-version') }}-${{ hashFiles('Gemfile.lock') }}
          restore-keys: |
            ${{ runner.os }}-gems-
      - run: echo "::set-env name=BUNDLE_BIN::$(pwd)/.direnv/bin"
      - name: install fastlane
        run: |
          bundle config path vendor/bundle
          bundle install --jobs 4 --retry 3
      - run: echo "::add-path::$BUNDLE_BIN"
      - run: sudo apt-get install git-crypt
      - name: decrypt secrets if possible
        env:
          GIT_CRYPT_KEY_BASE64: ${{ secrets.GIT_CRYPT_KEY_BASE64 }}
        run: |
          if [ -z "${GIT_CRYPT_KEY_BASE64}" ]; then
            echo 'git-crypt key not present in environment'
          else
            git crypt unlock <(echo $GIT_CRYPT_KEY_BASE64 | base64 --decode)
          fi
      - uses: actions/cache@v1
        with:
          path: ~/.gradle/caches
          key: ${{ runner.os }}-gradle-${{ hashFiles('android/*.gradle*') }}
          restore-keys: |
            ${{ runner.os }}-gradle-
      - uses: actions/cache@v2
        id: cache-android-ndk
        with:
          path: /usr/local/lib/android/sdk/ndk/19.2.5345600/
          key: ${{ runner.os }}-ndk-19.2.5345600
          restore-keys: |
            ${{ runner.os }}-ndk-
      - name: Install NDK
        if: steps.cache-android-ndk.outputs.cache-hit != 'true'
        run: |
          sudo $ANDROID_HOME/tools/bin/sdkmanager --install "ndk;19.2.5345600"
      - 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 }}
          ANDROID_NDK_HOME: /usr/local/lib/android/sdk/ndk/19.2.5345600/
        run: |
          if [ -z "$ANDROID_KEYSTORE_B64" ]; then
            echo "External build detected, APK will not be signed"
            fastlane android build build_type:Release 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:Release
          fi
      - uses: actions/upload-artifact@v2
        with:
          name: android-apk
          path: android/app/build/outputs/apk
      - name: Store daemon logs for debugging crashes
        if: failure()
        uses: actions/upload-artifact@v2
        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: bin/expotools client-build --platform android --release
        env:
          AWS_ACCESS_KEY_ID: AKIAJ3SWUQ4QLNQC7FXA
          AWS_SECRET_ACCESS_KEY: ${{ secrets.android_client_build_aws_secret_key }}
      - 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 }}
      - uses: 8398a7/action-slack@v3
        if: failure() && (github.event.ref == 'refs/heads/master' || startsWith(github.event.ref, 'refs/heads/sdk-'))
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SLACK_WEBHOOK_URL: ${{ secrets.slack_webhook_android }}
        with:
          channel: '#platform-android'
          status: ${{ job.status }}
          fields: author,job,message,ref
          author_name: client android build
