---
format_version: "11"
default_step_lib_source: https://github.com/bitrise-io/bitrise-steplib.git
project_type: android
trigger_map:
- push_branch: dev
  workflow: staging
- push_branch: release/next
  workflow: test_external
- push_branch: main
  workflow: external
- pull_request_source_branch: "*"
  workflow: pull_request
workflows:
  pull_request:
    envs:
    - BUILD_FLAVOUR: 
      opts:
        is_expand: true
    - BUILD_TASK: Debug
      opts:
        is_expand: true
    after_run:
    - _preflight
    - _test
  staging:
    envs:
    - KEYSTORE_NAME: fw_internal.keystore
      opts:
        is_expand: true
    - KEYSTORE_ALIAS: Allinternal
      opts:
        is_expand: true
    - PACKAGE_NAME: com.futureworkshops.<%= config[:package_name] %>
    - KEYSTORE_PASSWORD: "$INTERNAL_KEYSTORE_PASSWORD"
    - KEYALIAS_PASSWORD: "$INTERNAL_KEYALIAS_PASSWORD"
    after_run:
    - _preflight
    - _build
    - _deploy_bitrise
  external:
    envs:
    - KEYSTORE_NAME: 
      opts:
        is_expand: true
    - KEYSTORE_ALIAS: 
      opts:
        is_expand: true
    - PACKAGE_NAME: <%= config[:package_name] %>
    - KEYSTORE_PASSWORD: 
    - KEYALIAS_PASSWORD: 
    after_run:
    - _preflight
    - _build
    - _deploy_bitrise
    - _deploy_google
  test_external:
    envs:
    - KEYSTORE_NAME: 
      opts:
        is_expand: true
    - KEYSTORE_ALIAS: 
      opts:
        is_expand: true
    - PACKAGE_NAME: com.futureworkshops.<%= config[:package_name] %>
    - KEYSTORE_PASSWORD: 
    - KEYALIAS_PASSWORD: 
    after_run:
    - _preflight
    - _build
    - _deploy_bitrise
    - _deploy_google
  _build:
    steps:
    - s3-download@1:
        title: Download keystore
        inputs:
        - aws_access_key: "$AWS_ACCESS_KEY_ID"
        - aws_secret_access_key: "$AWS_SECRET_ACCESS_KEY"
        - s3_bucket: fw.ci.certificates
        - output_location: "$KEYSTORE_OUTPUT_PATH"
        - s3_filepath: "$KEYSTORE_NAME"
    - gradle-runner@2:
        inputs:
        - gradle_file: $GRADLE_BUILD_FILE_PATH
        - gradle_task: -Pproject.buildnumber=$BITRISE_BUILD_NUMBER -Pproject.buildversion=$BUILD_VERSION
            -Pproject.bundleId=$PACKAGE_NAME bundle$BUILD_FLAVOUR$BUILD_TASK
        - app_file_include_filter: |
            *-universal-*.apk
            *.aab
        - gradlew_path: $GRADLEW_PATH
    - bitrise-step-export-universal-apk@0:
        inputs:
        - keystore_alias: "$KEYSTORE_ALIAS"
        - private_key_password: "$KEYALIAS_PASSWORD"
        - keystore_url: file://$S3_DOWNLOAD_OUTPUT_PATH
        - keystore_password: "$KEYSTORE_PASSWORD"
    envs:
    - KEYSTORE_PATH: "$KEYSTORE_OUTPUT_PATH/$KEYSTORE_NAME"
  _deploy_bitrise:
    steps:
    - deploy-to-bitrise-io@2: {}
  _deploy_google:
    steps:
    - google-play-deploy@3:
        inputs:
        - service_account_json_key_path: "$BITRISEIO_GOOGLE_PLAY_JSON_AUTH_PATH_URL"
        - package_name: "$PACKAGE_NAME"
        - mapping_file: ''
        - track: internal
        - app_path: "$BITRISE_AAB_PATH"
  _preflight:
    steps:
    - build-number-adjust@1:
        inputs:
        - change: ''
        - increase: "$FW_BUILD_NUMBER_OFFSET"
        - decrease: ''
    - activate-ssh-key@4:
        run_if: '{{getenv "SSH_RSA_PRIVATE_KEY" | ne ""}}'
    - git-clone@7:
        inputs:
        - manual_merge: 'no'
    - install-missing-android-tools@3:
        inputs:
        - ndk_version: $ANDROID_NDK_VERSION
  _test:
    steps:
    - gradle-unit-test@1:
        inputs:
        - unit_test_task: "$TEST_COMMAND"
        - unit_test_flags: 
    - deploy-to-bitrise-io@2:
        inputs:
        - deploy_path: "$TEST_REPORTS_PATH"
        - notify_user_groups: none
        - is_compress: 'true'
    envs:
    - TEST_REPORTS_PATH: "$BITRISE_SOURCE_DIR/app/build/reports/"
    - TEST_COMMAND: check test
app:
  envs:
  - BUILD_FLAVOUR: 
    opts:
      is_expand: true
  - BUILD_TASK: Release
    opts:
      is_expand: true
  - GRADLE_BUILD_FILE_PATH: app/build.gradle
    opts:
      is_expand: true
  - GRADLEW_PATH: "./gradlew"
    opts:
      is_expand: true
  - FW_BUILD_NUMBER_OFFSET: '0'
    opts:
      is_expand: true
  - BUILD_VERSION: 1.0.0
    opts:
      is_expand: true
  - ANDROID_NDK_VERSION: '21'
    opts:
      is_expand: true
  - KEYSTORE_OUTPUT_PATH: "$BITRISE_SOURCE_DIR"
meta:
  bitrise.io:
    stack: linux-docker-android-20.04