workflows:
  react-native-ios:
    name: Notesnook iOS Build
    max_build_duration: 120
    instance_type: mac_mini
    environment:
      groups:
        - appstore_credentials
        - certificate_credentials
        - provisioning_profile

      vars:
        XCODE_WORKSPACE: "Notesnook.xcworkspace" # <-- Put the name of your Xcode workspace here
        XCODE_SCHEME: "Notesnook" # <-- Put the name of your Xcode scheme here
        BUNDLE_ID: "org.streetwriters.notesnook" # <-- Put your Bundle Id here e.g com.domain.myapp
        APP_STORE_APP_ID: 1544027013 # <-- Put the app id number here. This is found in App Store Connect > App > General > App Information

      node: 16.13.2
      xcode: latest
      cocoapods: default
    cache:
      cache_paths:
        - $HOME/Library/Caches/CocoaPods
    scripts:
      - name: Install npm dependencies
        script: |
          npm i
      - name: Install CocoaPods dependencies
        script: |
          yarn prepare:ios
      - name: Set up keychain to be used for codesigning using Codemagic CLI 'keychain' command
        script: |
          keychain initialize
      - name: Set up Provisioning profiles from environment variables
        script: |
          PROFILES_HOME="$HOME/Library/MobileDevice/Provisioning Profiles"
          mkdir -p "$PROFILES_HOME"
          for profile in "${!FCI_PROVISIONING_PROFILE_@}"; do
            PROFILE_PATH="$(mktemp "$HOME/Library/MobileDevice/Provisioning Profiles"/ios_$(uuidgen).mobileprovision)"
            echo ${!profile} | base64 --decode > "$PROFILE_PATH"
            echo "Saved provisioning profile $PROFILE_PATH"
          done
      - name: Set up signing certificate
        script: |
          echo $FCI_CERTIFICATE | base64 --decode > /tmp/certificate.p12
          if [ -z ${FCI_CERTIFICATE_PASSWORD+x} ]; then
            # when using a certificate that is not password-protected
            keychain add-certificates --certificate /tmp/certificate.p12
          else
            # when using a password-protected certificate
            keychain add-certificates --certificate /tmp/certificate.p12 --certificate-password $FCI_CERTIFICATE_PASSWORD
          fi
      - name: Set up code signing settings on Xcode project
        script: xcode-project use-profiles
      - name: Build packages
        script: |
          yarn build
      - name: Build ipa for distribution
        script: |
          xcode-project build-ipa --workspace "$FCI_BUILD_DIR/apps/mobile/native/ios/$XCODE_WORKSPACE" --scheme "$XCODE_SCHEME"
    artifacts:
      - build/ios/ipa/*.ipa
      - /tmp/xcodebuild_logs/*.log
      - $HOME/Library/Developer/Xcode/DerivedData/**/Build/**/*.app
      - $HOME/Library/Developer/Xcode/DerivedData/**/Build/**/*.dSYM
    publishing:
      # See the following link for details about email publishing - https://docs.codemagic.io/publishing-yaml/distribution/#email
      email:
        recipients:
          - ammarahmed6506@gmail.com
        notify:
          success: true # To not receive a notification when a build succeeds
          failure: false # To not receive a notification when a build fails
      app_store_connect:
        api_key: $APP_STORE_CONNECT_PRIVATE_KEY # Contents of the API key, can also reference environment variable such as $APP_STORE_CONNECT_PRIVATE_KEY
        key_id: $APP_STORE_CONNECT_KEY_IDENTIFIER # Alphanumeric value that identifies the API key, can also reference environment variable such as $APP_STORE_CONNECT_KEY_IDENTIFIER
        issuer_id: $APP_STORE_CONNECT_ISSUER_ID # Alphanumeric value that identifies who created the API key, can also reference environment variable such as $APP_STORE_CONNECT_ISSUER_ID
        submit_to_testflight: false # Optional boolean, defaults to false. Whether or not to submit the uploaded build to TestFlight beta review. Required for distributing to beta groups. Note: This action is performed during post-processing.
        # beta_groups:                                  # Specify the names of beta tester groups that will get access to the build once it has passed beta review.
        #      - group name 1
        #      - group name 2
