name: iOS Beta
on:
  workflow_dispatch: # Manual trigger
  push:
    branches:
      - master
    paths-ignore:
      - .gitignore
      - .github/**
      - '!.github/workflows/ios-beta.yaml'  # Run check on self change
      - '**/*_tests/**'
      - '**/CMakeLists.txt'
      - CONTRIBUTORS
      - LICENSE
      - NOTICE
      - README.md
      - android/**
      - data/strings/**
      - docs/**
      - generator/**
      - packaging/**
      - platform/*_android*
      - platform/*_linux*
      - platform/*_mac*
      - platform/*qt*
      - platform/*_win*
      - pyhelpers/**
      - qt*/**
      - skin_generator/**
      - tools/**
      - track_generator/**

jobs:
  ios-beta:
    name: Apple TestFlight
    runs-on: macos-26
    env:
      DEVELOPER_DIR: /Applications/Xcode_26.2.app/Contents/Developer
      LANG: en_US.UTF-8  # Fastlane complains that the terminal is using ASCII.
      LANGUAGE: en_US.UTF-8
      LC_ALL: en_US.UTF-8
    environment: beta
    defaults:
      run:
        shell: bash
    steps:
      - name: Checkout sources
        uses: actions/checkout@v4
        with:
          fetch-depth: 100 # enough to get all commits for the current day

      - name: Restore Boost submodule from cache
        uses: actions/cache@v4
        with:
          path: |
            3party/boost
            .git/modules/3party/boost
          key: boost-submodule

      - name: Parallel submodules checkout
        run: git submodule update --depth 1 --init --recursive --jobs=$(($(sysctl -n hw.logicalcpu) * 20))

      - name: Restore beta keys
        shell: bash
        run: |
          mkdir -p xcode/keys
          echo "$PRIVATE_H" | base64 -d > private.h
          echo "$APPSTORE_JSON" | base64 -d > xcode/keys/appstore.json
          echo "$CERTIFICATES_DEV_P12" | base64 -d > xcode/keys/CertificatesDev.p12
          echo "$CERTIFICATES_DISTR_P12" | base64 -d > xcode/keys/CertificatesDistr.p12
        env:
          PRIVATE_H: ${{ secrets.PRIVATE_H }}
          APPSTORE_JSON: ${{ secrets.APPSTORE_JSON }}
          CERTIFICATES_DEV_P12: ${{ secrets.CERTIFICATES_DEV_P12 }}
          CERTIFICATES_DISTR_P12: ${{ secrets.CERTIFICATES_DISTR_P12 }}

      - name: Configure repository
        shell: bash
        run: ./configure.sh

      - name: Compile and upload to TestFlight
        run: |
          echo "IOS_VERSION=$(../tools/unix/version.sh ios_version)-$(../tools/unix/version.sh ios_build)" >> "$GITHUB_ENV"
          ./fastlane.sh upload_testflight
        env:
          APPSTORE_CERTIFICATE_PASSWORD: '${{ secrets.APPSTORE_CERTIFICATE_PASSWORD }}'
        working-directory: xcode

      - name: Upload ipa and DSYMs artifacts
        uses: actions/upload-artifact@v4
        with:
          name: ipa and DSYM archive ${{ env.IOS_VERSION }}
          path: xcode/build/*
          if-no-files-found: error
