# Use the latest 2.1 version of CircleCI pipeline process engine.
# See: https://circleci.com/docs/2.0/configuration-reference
# For a detailed guide to building and testing on Android, read the docs:
# https://circleci.com/docs/2.0/language-android/ for more details.
version: 2.1

# Orbs are reusable packages of CircleCI configuration that you may share across projects, enabling you to create encapsulated, parameterized commands, jobs, and executors that can be used across multiple projects.
# See: https://circleci.com/docs/2.0/orb-intro/
orbs:
  android: circleci/android@2.3.0

commands:
  setup_repo:
    description: checkout repo and android dependencies
    steps:
      - checkout
      - run:
          name: Give gradle permissions
          command: |
            sudo chmod +x ./gradlew
      - android/restore-gradle-cache
  run_tests:
    description: run tests for flavour specified
    parameters:
      flavour:
        type: string
        default: "AtlasWeather"
    steps:
      # The next step will run the unit tests
      - run:
          name: Run non-instrumentation unit tests
          command: |
            ./gradlew test<< parameters.flavour >>DebugUnitTest
      - android/save-gradle-cache
      - store_artifacts:
          path: app/build/reports
          destination: reports
      - store_test_results:
          path: app/build/test-results
  run_ui_tests:
    description: run tests for flavour specified
    parameters:
      flavour:
        type: string
        default: "AtlasWeather"
      package_suffix:
        type: string
        default: "atlasWeather"
    steps:
      - android/start-emulator-and-run-tests:
          post-emulator-launch-assemble-command: ./gradlew assemble<< parameters.flavour >>DebugAndroidTest
          test-command: ./gradlew connected<< parameters.flavour >>DebugAndroidTest --continue
          system-image: system-images;android-26;google_apis;x86
      # store screenshots for failed ui tests
      - when:
          condition: on_fail
          steps:
            - store_artifacts:
                path: app/build/outputs/connected_android_test_additional_output/monoWeatherDebugAndroidTest/connected
                destination: connected_android_test
      # store test reports
      - store_artifacts:
          path: app/build/reports/androidTests/connected
          destination: reports
      - store_test_results:
          path: app/build/outputs/androidTest-results/connected
  deploy_to_play_store:
    description: deploy to playstore based on flavour
    parameters:
      flavour:
        type: string
        default: "AtlasWeather"
    steps:
      # The next step will run the unit tests
      - android/decode-keystore:
          keystore-location: "./app/keystore.jks"
      - run:
          name: Setup playstore key
          command: |
            echo "$GOOGLE_PLAY_KEY" > "google-play-key.json"
      - run:
          name: Run fastlane command to deploy to playstore
          command: |
            pwd
            bundle exec fastlane deploy<< parameters.flavour >>
      - store_test_results:
          path: fastlane/report.xml
# Define a job to be invoked later in a workflow.
# See: https://circleci.com/docs/2.0/configuration-reference/#jobs
jobs:
  # Below is the definition of your job to build and test your app, you can rename and customize it as you want.
  build-and-test:
    # Parameters used for determining
    parameters:
      flavour:
        type: string
        default: "AtlasWeather"
    # These next lines define the Android machine image executor.
    # See: https://circleci.com/docs/2.0/executor-types/
    executor:
      name: android/android-machine
      tag: 2023.05.1
    # Add steps to the job
    # See: https://circleci.com/docs/2.0/configuration-reference/#steps
    steps:
      - setup_repo
      - run_tests:
          flavour: << parameters.flavour >>
  run_instrumentation_test:
    # Parameters used for determining
    parameters:
      flavour:
        type: string
        default: "AtlasWeather"
      package_suffix:
        type: string
        default: "atlasWeather"
    # These next lines define the Android machine image executor.
    # See: https://circleci.com/docs/2.0/executor-types/
    executor:
      name: android/android-machine
      tag: 2023.05.1
    # Add steps to the job
    # See: https://circleci.com/docs/2.0/configuration-reference/#steps
    steps:
      - setup_repo
      - run_ui_tests:
          flavour: << parameters.flavour >>
          package_suffix: << parameters.package_suffix >>
  deploy-to-playstore:
    parameters:
      flavour:
        type: string
        default: "Driver"
    docker:
      - image: cimg/android:2023.07-browsers
        auth:
          username: ${DOCKER_USERNAME}
          password: ${DOCKER_PASSWORD}
    steps:
      - setup_repo
      - deploy_to_play_store:
          flavour: << parameters.flavour >>

# Invoke jobs via workflows
# See: https://circleci.com/docs/2.0/configuration-reference/#workflows
workflows:
  version: 2
  build-release-mono:
    jobs:
      - build-and-test:
          context: appttude
          flavour: "MonoWeather"
          filters:
            branches:
              ignore:
                - main_atlas
      - run_instrumentation_test:
          context: appttude
          flavour: "MonoWeather"
          package_suffix: "monoWeather"
          filters:
            branches:
              only:
                - master
                - main_mono
      - deploy-to-playstore:
          context: appttude
          flavour: "MonoWeather"
          filters:
            branches:
              only:
                - main_mono
          requires:
            - run_instrumentation_test
  build-release-atlas:
    jobs:
      - build-and-test:
          context: appttude
          flavour: "AtlasWeather"
          filters:
            branches:
              ignore:
                - main_mono
      - run_instrumentation_test:
          context: appttude
          flavour: "AtlasWeather"
          package_suffix: "atlasWeather"
          filters:
            branches:
              only:
                - master
                - main_atlas
      - deploy-to-playstore:
          context: appttude
          flavour: "AtlasWeather"
          filters:
            branches:
              only:
                - main_atlas
          requires:
            - run_instrumentation_test