name: TestFlight

on:
  workflow_dispatch:
    inputs:
      changelog: 
        type: string
        description: What to test
        required: true

  push:
    branches:
      - experiment/test-flight/**

defaults:
  run:
    shell: zsh --login --errexit --pipefail {0}

env:
  NSUnbufferedIO: YES # display xcodebuild output in the correct order for github action log
  version_offset: 0   # Offset build version number from Github run number

concurrency:
  group: one-test-flight-${{ github.ref }}
  cancel-in-progress: true

jobs:
  xcode-archive:
    name: Build iOS Release
    timeout-minutes: 30
    runs-on: self-hosted

    steps:
      - name: Show what to test
        run: echo "::notice::What to test - ${{ github.event.inputs.changelog }}"
        
      - name: Checkout
        uses: actions/checkout@v3

      - name: Resolve Package Dependencies
        run: scripts/resolve-swift-package-dependencies.zsh /tmp/spm-clone/${{ runner.name }}

      - name: Set version number
        run: |
          next_version=$((GITHUB_RUN_NUMBER+version_offset))
          agvtool new-version $next_version
          echo "::notice::$(agvtool what-version | tr -d '\n')"
          marketing_version=$(xcodebuild -showBuildSettings | grep MARKETING_VERSION | tr -d 'MARKETING_VERSION =')
          echo "TAG_NAME=$marketing_version-$next_version" >> $GITHUB_ENV
          echo "ARCHIVE=MyApp-$next_version.xcarchive" >> $GITHUB_ENV

      - name: Unlock Keychain # to workaround: https://github.com/fastlane/fastlane/issues/15185#issuecomment-595977936
        run: |
          security unlock-keychain -p ${{ secrets.KEYCHAIN_PASSWORD }} login.keychain
          security show-keychain-info login.keychain

      - name: Install Ruby Gems
        run: |
          bundle config set --local path '/tmp/gem-bundle/${{ runner.name }}'
          bundle install
          
      - name: Build, Archive and Release
        env:
          FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT: 180
        run: >
          bundle exec fastlane release
          archive_path:${{ env.ARCHIVE }}
          packages_path:/tmp/spm-clone/${{ runner.name }}
          changelog:"${{ github.event.inputs.changelog }}"

      - name: Upload fastlane logs to Artifacts
        if: always()
        uses: actions/upload-artifact@v3
        with:
          path: MyBinary-MyApp.log
          name: fastlane-MyBinary-MyApp.log

      - name: Save Archive to Xcode Organizer
        run: |
          open $ARCHIVE
          find -s ~/Library/Developer/Xcode/Archives/ -name \*.xcarchive

      - name: Compress Xcode Archive # due to: https://github.com/actions/upload-artifact/issues/326
        if: always()
        run: >
            tar --use-compress-program pbzip2
            --check-links --verbose
            --create --file $ARCHIVE.tar.bz2 $ARCHIVE || true

      - name: Upload Xcode archive to Artifacts
        if: always()
        uses: actions/upload-artifact@v3
        with:
          path: ${{ env.ARCHIVE }}.tar.bz2
          name: ${{ env.ARCHIVE }}

      - name: GitHub Tag
        uses: mathieudutour/github-tag-action@v6.1
        with:
          custom_tag: ${{ env.TAG_NAME }}
          github_token: ${{ secrets.GITHUB_TOKEN }}

      - name: Upload Debug Symbols to Crashlytics
        run: >
          /tmp/spm-clone/${{ runner.name }}/checkouts/firebase-ios-sdk/Crashlytics/upload-symbols
          --google-service-plist MyApp/GoogleService-Info.plist
          --platform ios $ARCHIVE/dSYMs