name: iOS Client

on:
  workflow_dispatch:
  schedule:
    - cron: '20 5 * * 1,3,5' # 5:20 AM UTC time on every Monday, Wednesday and Friday
  pull_request:
    paths:
      - .github/workflows/client-ios.yml
      - ios/**
      - tools/src/dynamic-macros/**
      - tools/src/commands/IosGenerateDynamicMacros.ts
      - tools/src/client-build/**
      - tools/src/commands/ClientBuild.ts
      - secrets/**
      - fastlane/**
      - Gemfile.lock
      - yarn.lock
  push:
    branches: [main, sdk-*]
    paths:
      - .github/workflows/client-ios.yml
      - ios/**
      - tools/src/dynamic-macros/**
      - tools/src/commands/IosGenerateDynamicMacros.ts
      - tools/src/client-build/**
      - tools/src/commands/ClientBuild.ts
      - secrets/**
      - fastlane/**
      - Gemfile.lock
      - yarn.lock

concurrency:
  group: ${{ github.workflow }}-${{ github.event_name }}-${{ github.ref }}
  cancel-in-progress: true

jobs:
  build:
    runs-on: macos-13
    steps:
      - name: 👀 Checkout
        uses: actions/checkout@v3
        with:
          submodules: true
      - name: 🔨 Switch to Xcode 14.3
        run: sudo xcode-select --switch /Applications/Xcode_14.3.app
      - name: 🔓 Decrypt secrets if possible
        uses: ./.github/actions/expo-git-decrypt
        with:
          key: ${{ secrets.GIT_CRYPT_KEY_BASE64 }}
      - name: ➕ Add `bin` to GITHUB_PATH
        run: echo "$(pwd)/bin" >> $GITHUB_PATH
      - name: 💎 Setup Ruby and install gems
        uses: ruby/setup-ruby@v1
        with:
          bundler-cache: true
          ruby-version: 3.2.2
      - name: ♻️ Restore caches
        uses: ./.github/actions/expo-caches
        id: expo-caches
        with:
          yarn-workspace: 'true'
          yarn-tools: 'true'
          ios-pods: 'true'
      - name: 🧶 Yarn install
        if: steps.expo-caches.outputs.yarn-workspace-hit != 'true'
        run: yarn install --frozen-lockfile
      - name: 🏭 Generating dynamic macros
        run: expotools ios-generate-dynamic-macros
      - name: 🥥 Install CocoaPods in `ios`
        if: steps.expo-caches.outputs.ios-pods-hit != 'true'
        run: pod install
        working-directory: ios
      - name: 🔎 Check which flavor to build
        id: flavor
        uses: dorny/paths-filter@v2
        with:
          # this action fails when base is not set on schedule event
          base: ${{ github.ref }}
          filters: |
            versioned:
              - ios/versioned-react-native/**
              - ios/Exponent/Versioned/**
      - name: 🏗 Build Expo Go for simulator
        run: |
          [[ "$IS_VERSIONED_FLAVOR" == "true" ]] && FLAVOR="versioned" || FLAVOR="unversioned"
          echo "Building with $FLAVOR flavor"
          expotools client-build --platform ios --flavor $FLAVOR
        timeout-minutes: 120
        env:
          IS_VERSIONED_FLAVOR: ${{ github.event_name == 'schedule' || steps.flavor.outputs.versioned == 'true' }}
      - name: 💾 Save test results
        if: always()
        uses: actions/upload-artifact@v3
        with:
          name: fastlane-logs
          path: ~/Library/Logs/fastlane
      - name: 🔔 Notify on Slack
        uses: 8398a7/action-slack@v3
        if: failure() && (github.event.ref == 'refs/heads/main' || startsWith(github.event.ref, 'refs/heads/sdk-'))
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          SLACK_WEBHOOK_URL: ${{ secrets.slack_webhook_ios }}
        with:
          channel: '#expo-ios'
          status: ${{ job.status }}
          fields: job,message,ref,eventName,author,took
          author_name: Expo Go (iOS)
