name: Updates e2e EAS

on:
  workflow_dispatch: {}
  pull_request:
    paths:
      - .github/workflows/updates-e2e-basic.yml
      - packages/@expo/cli/**
      - packages/@expo/config-plugins/**
      - packages/@expo/config-types/**
      - packages/expo-json-utils/**
      - packages/expo-manifests/**
      - packages/expo-modules-core/**
      - packages/expo-structured-headers/**
      - packages/expo-updates-interface/**
      - packages/expo-updates/**
      - packages/expo/android/**
      - packages/expo/ios/**
      - templates/expo-template-bare-minimum/**
  push:
    branches: [main, 'sdk-*']
    paths:
      - .github/workflows/updates-e2e-basic.yml
      - packages/@expo/cli/**
      - packages/@expo/config-plugins/**
      - packages/@expo/config-types/**
      - packages/expo-json-utils/**
      - packages/expo-manifests/**
      - packages/expo-modules-core/**
      - packages/expo-structured-headers/**
      - packages/expo-updates-interface/**
      - packages/expo-updates/**
      - packages/expo/android/**
      - packages/expo/ios/**
      - templates/expo-template-bare-minimum/**

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

jobs:
  build:
    strategy:
      fail-fast: false
      matrix:
        platform: [ios, android]
    runs-on: ubuntu-20.04
    timeout-minutes: 40
    env:
      UPDATES_PORT: 4747
    steps:
      - name: 👀 Checkout
        uses: actions/checkout@v3
      - name: ⬢ Setup Node
        uses: actions/setup-node@v3
        with:
          node-version: 16
      - name: ➕ Add `bin` to GITHUB_PATH
        run: echo "$(yarn global bin)" >> $GITHUB_PATH
      - name: ♻️ Restore caches
        uses: ./.github/actions/expo-caches
        id: expo-caches
        with:
          yarn-workspace: 'true'
      - name: 🧶 Yarn install
        if: steps.expo-caches.outputs.yarn-workspace-hit != 'true'
        run: yarn install --frozen-lockfile
      - name: 🛠 Build local Expo CLI
        working-directory: packages/@expo/cli
        run: yarn prepare
      - name: Build expo-updates CLI
        working-directory: packages/expo-updates
        run: yarn build:cli
      - name: 🔧 Install eas-cli
        run: yarn global add eas-cli
      - name: 🌳 Add EXPO_REPO_ROOT to environment
        run: echo "EXPO_REPO_ROOT=$GITHUB_WORKSPACE" >> $GITHUB_ENV
      - name: 🌐 Set updates host
        run: echo "UPDATES_HOST=localhost" >> $GITHUB_ENV
      - name: 🌐 Set updates port
        run: echo "UPDATES_PORT=4747" >> $GITHUB_ENV
      - name: 📦 Get artifacts path
        run: mkdir -p artifact && echo "ARTIFACTS_DEST=$(pwd)/artifact" >> $GITHUB_ENV
      - name: 📦 Get commit message
        run: echo "COMMIT_MESSAGE=$(git log -1 --pretty=oneline | head -c1000)" >> $GITHUB_ENV
      - name: 📦 Set test project location
        run: echo "TEST_PROJECT_ROOT=$GITHUB_WORKSPACE/updates-e2e" >> $GITHUB_ENV
      - name: 📦 Setup test project for updates E2E basic tests
        run: node packages/expo-updates/e2e/setup/create-eas-project.js
      - name: 🚀 Build with EAS for ${{ matrix.platform }}
        uses: ./.github/actions/eas-build
        id: build_eas
        with:
          platform: ${{ matrix.platform }}
          profile: 'updates_testing'
          projectRoot: './updates-e2e'
          expoToken: ${{ secrets.EAS_BUILD_BOT_TOKEN }}
          noWait: ${{ github.event.schedule }}
          message: ${{ github.event.pull_request.title }}
      - name: On ${{ matrix.platform }} workflow canceled
        if: ${{ cancelled() && steps.build_eas.outputs.build_id }}
        run: eas build:cancel ${{ steps.build_eas.outputs.build_id }}
        working-directory: './updates-e2e'
        env:
          EXPO_TOKEN: ${{ secrets.EAS_BUILD_BOT_TOKEN }}
          EAS_BUILD_PROFILE: updates_testing
