name: Versioning Expo Go

on:
  workflow_dispatch: {}
  schedule:
    - cron: '0 5 * * SUN' # 5AM UTC time on each Sunday
  pull_request:
    paths:
      - .github/workflows/versioning.yml
      - tools/src/commands/AddSDKVersion.ts
      - tools/src/commands/RemoveSDKVersion.ts
      - tools/src/versioning/**

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

jobs:
  ios:
    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: 📂 Adding new SDK version
        run: expotools add-sdk-version --platform ios --sdkVersion next --reinstall
      - name: 🏗 Build versioned Expo Go for simulator
        run: expotools client-build --platform ios --flavor versioned
        timeout-minutes: 120
        env:
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
          AWS_DEFAULT_REGION: 'us-east-1'
      - name: 🗑 Removing SDK version
        run: expotools remove-sdk-version --platform ios --sdkVersion latest
      - name: 🧐 Ensure that Git working dir is clean
        run: git diff --exit-code
      - 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: Versioning Expo Go (iOS)
