version: 2
executorType: docker
jobs:
  build-app:
    resource_class: medium
    environment:
      - GRADLE_OPTS: '-Dorg.gradle.daemon=false -Dorg.gradle.jvmargs="-Xmx3072m -XX:+HeapDumpOnOutOfMemoryError"'
      - REACT_NATIVE_MAX_WORKERS: 2
      - ANDROID_BUILD_TOOLS_VERSION: '28.0.3'
    working_directory: ~/app
    docker:
      - image: reactnativecommunity/react-native-android
    steps:
      - checkout
      - restore_cache:
          keys:
            - v1-npm-{{ .Branch }}-{{ checksum "yarn.lock" }}
            - v1-npm
      - run:
          name: Install Dependencies
          command: yarn install --ignore-engines
      - save_cache:
          key: v1-npm
          paths:
            - node_modules/
      - save_cache:
          key: v1-npm-{{ .Branch }}-{{ checksum "yarn.lock" }}
          paths:
            - node_modules/
      - run:
          name: Lint
          command: yarn lint
      - restore_cache:
          keys: 
            - v1-gradle-{{ checksum "android/gradle/wrapper/gradle-wrapper.properties" }}-{{ checksum "examples/basic/android/gradle/wrapper/gradle-wrapper.properties" }}
            - v1-gradle-wrapper
      - restore_cache:
          keys:
            - v1-gradle-cache-{{ checksum "android/build.gradle" }}-{{ checksum "examples/basic/android/build.gradle" }}
            - v1-gradle-cache
      - run:
          name: Run Checks
          command: |
            cd android
            chmod +x ./gradlew && ./gradlew check
      - store_artifacts:
          path: android/build/reports
      - run:
          name: Run Yarn to Generate react.gradle
          command: cd examples/basic/android && yarn
      - run:
          name: Build Sample App
          command: |
            cd examples/basic/android && chmod +x ./gradlew && ./gradlew clean && ./gradlew build
      - store_artifacts:
          path: examples/basic/android/app/build/reports
          destination: app
      - save_cache:
          key: v1-gradle-wrapper-{{ checksum "examples/basic/android/gradle/wrapper/gradle-wrapper.properties" }}
          paths:
            - ~/.gradle/wrapper
      - save_cache:
          key: v1-gradle-cache-{{ checksum "examples/basic/android/build.gradle" }}
          paths:
            - ~/.gradle/caches
      - deploy:
          command: |
            if [ "${CIRCLE_BRANCH}" == "master" ]; then
              yarn ci:publish
            fi
  deploy-docs:
    working_directory: ~/app
    docker:
      - image: circleci/node:8.11.1
    steps:
      - checkout
      - run:
          name: Deploying to GitHub Pages
          command: |
            git config --global user.email "${GH_EMAIL}@users.noreply.github.com"
            git config --global user.name "${GH_NAME}"
            echo "machine github.com login $GH_NAME password $GH_TOKEN_DOCS" > ~/.netrc
            cd website && yarn install && GIT_USER=${GH_NAME} yarn run publish-gh-pages
workflows:
  version: 2
  build-and-deploy-docs:
    jobs:
      - build-app
      - deploy-docs:
          filters:
            branches:
              only: master
