# Automate TestFlight Uploads from a CI Mac

Canonical: https://figment.so/blog/mac-testflight-ci-distribution

Use a Fastlane beta lane that authenticates with an App Store Connect API key, bumps the build number, archives with gym and uploads with pilot, then trigger it from a scheduled workflow on a self-hosted Mac runner.

**To automate TestFlight uploads, add a Fastlane `beta` lane that authenticates with an App Store Connect API key, bumps the build number, archives with `gym` and uploads with `pilot`, then trigger it on a schedule from a self-hosted Mac runner.** Shipping a beta by hand means opening Xcode, waiting for an archive and babysitting an upload. The lane below makes it one command with no dialogs. References used throughout: the [Fastlane `upload_to_testflight` documentation](https://docs.fastlane.tools/actions/upload_to_testflight/) and the [App Store Connect API documentation](https://developer.apple.com/documentation/appstoreconnectapi).

## What do I need before I automate TestFlight uploads?

You need an App Store Connect API key, Fastlane and a licensed Xcode on the Mac. Fastlane supports two authentication methods, and the **App Store Connect API key** is the right one for CI: it uses a short-lived JWT, never triggers a two-factor challenge, and works without an Apple ID session.

- **App Store Connect API key**: requires the **Admin** or **App Manager** role in App Store Connect. The key is a Key ID, an Issuer ID and a `.p8` private key file.
- **Fastlane** installed on the CI Mac via Bundler (pin the version per project).
- **Xcode** installed and licensed on the Mac (`sudo xcodebuild -license accept`).

Install Fastlane via Bundler in your project directory:

```bash
echo 'gem "fastlane"' >> Gemfile
bundle install
bundle exec fastlane --version
```

## How do I create the API key and store it as a CI secret?

In App Store Connect, go to **Users and Access → Integrations → App Store Connect API**, click **+**, name the key `ci-upload`, assign the **App Manager** role, and download the `.p8` once, because Apple offers only one download. Note the **Key ID** and **Issuer ID** shown on the same page.

Store three values as CI secrets:

| Secret name | Value |
|---|---|
| `ASC_KEY_ID` | The 10-character key identifier |
| `ASC_ISSUER_ID` | The UUID on the API keys page |
| `ASC_KEY_CONTENT` | Contents of the `.p8` file, base64-encoded |

Never commit the `.p8` to the repo. In GitHub Actions, add each value under **Settings → Secrets and variables → Actions**. Do not dump the shell environment to disk at any point, because the keys would land in a file. Write out only the `.p8` content, and only at runtime:

```bash
echo "$ASC_KEY_CONTENT" | base64 --decode > "$RUNNER_TEMP/asc_key.p8"
chmod 600 "$RUNNER_TEMP/asc_key.p8"
```

Signing certificates travel separately. Export the distribution identity from your signing Mac as an encrypted `.p12` (private key included) and import it into a dedicated CI keychain on first provisioning. Provisioning profiles are plain files; copy them into `~/Library/MobileDevice/Provisioning Profiles/`.

## How do I auto-increment the build number?

Query the highest TestFlight build from App Store Connect and add one, because duplicate build numbers cause upload failures:

```ruby
current = latest_testflight_build_number(
  api_key:        api_key,
  app_identifier: ENV.fetch("BUNDLE_ID"),
  version:        get_version_number(xcodeproj: ENV.fetch("XCODEPROJ"))
)
increment_build_number(build_number: current + 1, xcodeproj: ENV.fetch("XCODEPROJ"))
```

Scoping by `version:` keeps parallel release trains from stepping on each other.

## How do I archive with gym and upload with pilot?

`gym` (alias `build_app`) drives `xcodebuild archive` and `xcodebuild -exportArchive`. A minimum App Store export call:

```ruby
gym(
  workspace:        ENV.fetch("WORKSPACE"),
  scheme:           ENV.fetch("SCHEME"),
  configuration:    "Release",
  export_method:    "app-store",
  output_name:      "MyApp",
  output_directory: "build"
)
```

Use `workspace:` for CocoaPods projects and `xcodeproj:` only for standalone projects. If you use manual signing, pass `export_options` with the profile name.

`pilot` (alias `upload_to_testflight`) submits the IPA. Useful parameters per the [Fastlane docs](https://docs.fastlane.tools/actions/upload_to_testflight/):

| Parameter | Purpose |
|---|---|
| `skip_waiting_for_build_processing` | Return right after upload; do not block the runner for Apple's processing queue |
| `groups` | Internal tester group names to distribute to after processing |
| `changelog` | "What to Test" text visible to testers |
| `distribute_external` | External distribution requires Apple review; keep `false` for internal groups |
| `wait_processing_timeout_duration` | Override the default processing-wait timeout when `skip_waiting_for_build_processing` is false |

Fastlane's own guidance applies: if `skip_waiting_for_build_processing` is true, `distribute_external` has no effect in the same run, because processing must finish first.

## What does the complete beta lane look like?

Place this in `fastlane/Fastfile` and replace the project-specific env names with your own:

```ruby
default_platform(:ios)

BUNDLE_ID = ENV.fetch("BUNDLE_ID")
WORKSPACE = ENV.fetch("WORKSPACE")   # e.g. "MyApp.xcworkspace"
XCODEPROJ = ENV.fetch("XCODEPROJ")   # e.g. "MyApp.xcodeproj"
SCHEME    = ENV.fetch("SCHEME")      # your real scheme name

platform :ios do
  lane :beta do
    api_key = app_store_connect_api_key(
      key_id:       ENV.fetch("ASC_KEY_ID"),
      issuer_id:    ENV.fetch("ASC_ISSUER_ID"),
      key_filepath: ENV.fetch("ASC_KEY_PATH"),
      duration:     1200,
      in_house:     false
    )

    current_build = latest_testflight_build_number(
      api_key:        api_key,
      app_identifier: BUNDLE_ID,
      version:        get_version_number(xcodeproj: XCODEPROJ)
    )
    increment_build_number(build_number: current_build + 1, xcodeproj: XCODEPROJ)

    unlock_keychain(
      path:     "~/Library/Keychains/CI.keychain-db",
      password: ENV.fetch("KEYCHAIN_PASSWORD")
    )

    gym(
      workspace:        WORKSPACE,
      scheme:           SCHEME,
      configuration:    "Release",
      export_method:    "app-store",
      output_name:      "MyApp",
      output_directory: "build"
    )

    changelog = changelog_from_git_commits(
      commits_count:          10,
      pretty:                 "- %s",
      merge_commit_filtering: "exclude_merges"
    )

    upload_to_testflight(
      api_key:                           api_key,
      app_identifier:                    BUNDLE_ID,
      ipa:                               "build/MyApp.ipa",
      skip_waiting_for_build_processing: true,
      distribute_external:               false,
      groups:                            ["Internal Beta"],
      changelog:                         changelog
    )

    UI.success "Beta uploaded — build #{current_build + 1}"
  end
end
```

This is an untested example; run it against your real project and scheme before you schedule it. If any step fails, Fastlane halts and the lane exits non-zero. Do not wrap it in `|| true`, and never treat a failed archive as a successful run.

## How do I run the lane nightly with GitHub Actions?

A self-hosted runner on your Mac sends jobs to the environment where Xcode, Fastlane and the signing keychain already exist:

```yaml
name: Nightly TestFlight Beta
on:
  schedule:
    - cron: '0 2 * * *'     # 02:00 UTC nightly
  workflow_dispatch:

concurrency:
  group: testflight-upload
  cancel-in-progress: false

jobs:
  beta:
    runs-on: [self-hosted, macOS]
    timeout-minutes: 45
    steps:
      - uses: actions/checkout@v4
      - name: Materialise API key
        run: |
          echo "$ASC_KEY_CONTENT" | base64 --decode > "$RUNNER_TEMP/asc_key.p8"
          chmod 600 "$RUNNER_TEMP/asc_key.p8"
        env:
          ASC_KEY_CONTENT: ${{ secrets.ASC_KEY_CONTENT }}
      - name: Install gems
        run: bundle install
      - name: Upload to TestFlight
        run: bundle exec fastlane beta
        env:
          ASC_KEY_ID:        ${{ secrets.ASC_KEY_ID }}
          ASC_ISSUER_ID:     ${{ secrets.ASC_ISSUER_ID }}
          ASC_KEY_PATH:      ${{ runner.temp }}/asc_key.p8
          BUNDLE_ID:         ${{ secrets.BUNDLE_ID }}
          WORKSPACE:         MyApp.xcworkspace
          XCODEPROJ:         MyApp.xcodeproj
          SCHEME:            MyApp
          KEYCHAIN_PASSWORD: ${{ secrets.KEYCHAIN_PASSWORD }}
```

The `concurrency` block serializes uploads so two runs cannot both query `latest_testflight_build_number` at the same moment. Firewall allowlists, if you use them, belong on your own egress. Apple does not publish a general allowlist for App Store Connect ingress traffic.

## What do the common errors mean?

**"No .p8 key file found."** `ASC_KEY_PATH` points at a file that was never written in this shell. Add `ls -l "$ASC_KEY_PATH"` before the Fastlane step.

**Duplicate build number on upload.** Two jobs raced. The `concurrency` block above fixes it; for Buildkite, use a concurrency group with limit 1.

**"The request timed out."** Large IPAs can exceed the default wait. Either keep `skip_waiting_for_build_processing: true` and check processing status in a later job, or raise `wait_processing_timeout_duration`. See the [upload_to_testflight parameter reference](https://docs.fastlane.tools/actions/upload_to_testflight/).

**"You don't have permission to perform this action."** The API key's role is below App Manager. Reissue it with the correct role in App Store Connect.

## Which Figment Mac plan fits weekly betas?

It depends on whether the Mac must be on when a schedule fires. A nightly trigger needs an always-on runner: the [Apple Silicon Mac VM](/mac-hosting#apple-silicon-vm) at $39 a month runs current Xcode, and the [Intel Mac VM](/mac-hosting#intel-vm) at $25 a month works only when Xcode 26.3 or earlier is enough (macOS Sequoia; no arm64-only binaries). A weekly beta is cheaper by the day: four Apple Silicon VM days a month cost 4 × $6 = $24, and the [Dedicated M4 Pro 48 GB](/mac-hosting#dedicated-m4-pro) (monthly only: $349 a month, or $299 a month on a 12-month term) suits teams that archive large apps in parallel. Start the Mac, run the lane, and stop it in Figment or let the daily plan lapse; shutting down macOS does not stop billing. Figment runs the Mac; you manage Fastlane, signing and your App Store Connect account, and we do not offer managed CI. Check the macOS version shown before purchase against Apple's Xcode table.

---

**Related guides**
- [Apple Silicon CI Binaries on Figment](/blog/mac-arm64-binary-ci-apple-silicon)
- [Universal Binary CI Pipeline](/blog/mac-universal-binary-ci-pipeline)
- [Xcode on Windows: Remote Mac Setup](/blog/mac-xcode-on-windows-remote-setup)
