variables:
  GIT_SUBMODULE_STRATEGY: recursive

Build:
  rules:
    - if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_REF_NAME == "develop"
      when: on_success
      allow_failure: false
    - if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_TAG != null
      when: on_success
      allow_failure: false
  tags:
    - macos
    - flutter
  stage: build
  artifacts:
     paths:
       - ./Result
     expire_in: 3 hours
  script:
    - chmod +x ./toolchain/build_platforms.sh
    - ./toolchain/build_platforms.sh nut_player device

ManualBuild:
  rules:
    - if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_REF_NAME =~ /^feature/
      when: manual
      allow_failure: false
  tags:
    - macos
    - flutter
  stage: build
  artifacts:
     paths:
       - ./Result
     expire_in: 3 hours
  script:
    - chmod +x ./toolchain/build_platforms.sh
    - ./toolchain/build_platforms.sh nut_player device

Deploy:
  rules:
    - if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_REF_NAME == "develop"
      when: on_success
      allow_failure: false
    - if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_TAG != null
      when: on_success
      allow_failure: false
  tags:
    - macos
    - flutter
  dependencies:
    - Build
  needs:
    - job: Build
      artifacts: true
  variables:
    IPA_DEPLOY_TARGET: "TESTFLIGHT"
    IPA_FILE: NutPlayerFlutter.ipa
    IPA_PATH: ./Result
  stage: deploy
  script:
    - chmod +x ./toolchain/deploy_iOS.sh
    - chmod +x ./toolchain/deploy_Android.sh
    - ./toolchain/deploy_iOS.sh nut_player ${IPA_DEPLOY_TARGET} ${IPA_PATH} ${IPA_FILE} iOS
    - ./toolchain/deploy_Android.sh nut_player

Deploy_Feature:
  rules:
    - if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_REF_NAME =~ /^feature/
      when: manual
      allow_failure: false
  tags:
    - macos
    - flutter
  dependencies:
    - ManualBuild
  needs:
    - job: ManualBuild
      artifacts: true
  variables:
    IPA_DEPLOY_TARGET: "TESTFLIGHT"
    IPA_FILE: NutPlayerFlutter.ipa
    IPA_PATH: ./Result
  stage: deploy
  script:
    - chmod +x ./toolchain/deploy_iOS.sh
    - chmod +x ./toolchain/deploy_Android.sh
    - ./toolchain/deploy_Android.sh nut_player
    - ./toolchain/deploy_iOS.sh nut_player ${IPA_DEPLOY_TARGET} ${IPA_PATH} ${IPA_FILE} iOS

notifyMessengerFail:
  rules:
    - if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_REF_NAME == "develop"
      when: on_failure
      allow_failure: false
    - if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_TAG != null
      when: on_failure
      allow_failure: false
  needs:
    - job: Deploy
      artifacts: false
  stage: notify
  tags:
    - macos
    - flutter
  script:
    - chmod +x ./toolchain/notification.sh
    - ./toolchain/notification.sh FAIL

notifyMessengerSuccess:
  rules:
    - if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_REF_NAME == "develop"
      when: on_success
      allow_failure: false
    - if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_TAG != null
      when: on_success
      allow_failure: false
  needs:
    - job: Deploy
      artifacts: false
  stage: notify
  tags:
    - macos
    - flutter
  script:
    - chmod +x ./toolchain/notification.sh
    - ./toolchain/notification.sh SUCCESS

#ManualUpdateApplications:
#    rules:
#      - if: $CI_COMMIT_REF_NAME == "develop"
#        when: manual
#        allow_failure: false
#      - if: $CI_COMMIT_TAG != null
#        when: manual
#        allow_failure: false
#    tags:
#      - macos
#      - flutter
#    stage: notify
#    script:
#      - |
#        curl -X "POST" "https://gitlab.nut.team/api/v4/projects/574/trigger/pipeline" \
#           -H 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' \
#           --data-urlencode "token=$K8S_TOKEN_UPDATE_CONFRONTARE_APPLICATION" \
#           --data-urlencode "ref=develop"