name: detox-ios

on:
  pull_request:
    types:
      - opened
      - reopened
      - labeled
      - unlabeled
      - synchronize

jobs:
  ios:
    # only run this CI with github label match to the workflow name
    if: contains(github.event.pull_request.labels.*.name, github.workflow)
    name: Detox iOS E2E Testing
    runs-on: ${{ matrix.runners }}
    strategy:
      matrix:
        runners: [[self-hosted, macOS, macos-selfhosted-14-sh, aws]]
    timeout-minutes: 120
    concurrency:
      group: ci-ios-${{ matrix.runners }}-${{ github.head_ref }}
      cancel-in-progress: true

    env:
      DEVELOPER_DIR: /Applications/Xcode_15.2.app
      RCT_NO_LAUNCH_PACKAGER: "true"
      NODE_OPTIONS: "--max-old-space-size=5120"

    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          fetch-depth: 0

      - name: Common detox ios setup
        uses: mattrinternal/sre-reusable-action/actions/common-detox-ios-setup@master

      - name: Build project
        run: |
          yarn install --frozen-lockfile
          yarn build

      - name: Install Example Dependencies
        working-directory: ./example
        run: |
          yarn install --frozen-lockfile
          rbenv install --skip-existing
          ruby -v
          bundle install
          cd ios && bundle exec pod install --repo-update && cd ..

      - name: Detox Build iOS Configuration
        working-directory: ./example
        run: yarn detox:setup && yarn detox:build:ios

      - name: Detox E2E Test
        working-directory: ./example
        run: yarn detox:test:ios --maxWorkers 1 --retries 3 --cleanup --loglevel warn

      - name: Archive detox failed test results
        if: failure()
        uses: actions/upload-artifact@v4
        with:
          name: detox-artifacts-${{ github.sha }}
          include-hidden-files: true
          path: ./example/.detox-artifacts
