Fastlane for iOS: match, gym & pilot
On iOS, Fastlane earns its keep by taming code signing. match keeps the team's certificates and provisioning profiles in one encrypted store and installs them on any machine; gym (build_app) wraps xcodebuild archive and -exportArchive; pilot (upload_to_testflight) uploads to TestFlight through the App Store Connect API.
Note: non-run (macOS and Fastlane only). Actions and parameters are from Fastlane's documentation.
App Store Connect API key
Fastlane authenticates with an App Store Connect API key (created under Users and Access → Integrations): a .p8 private key plus a key ID and an issuer ID. It avoids Apple ID passwords and two-factor prompts on CI. Store all three as CI secrets and load them at the start of the lane:
api_key = app_store_connect_api_key(
key_id: ENV.fetch("ASC_KEY_ID"),
issuer_id: ENV.fetch("ASC_ISSUER_ID"),
key_content: ENV.fetch("ASC_KEY_P8"),
is_key_content_base64: false
)Most later actions pick the key up automatically from lane_context.
match: signing as a shared, encrypted repository
bundle exec fastlane match init # choose storage: git, google_cloud, s3, gitlab_secure_files
bundle exec fastlane match appstore # create/fetch the App Store certificate and profiles
bundle exec fastlane match development# fastlane/Matchfile
git_url("git@github.com:example/ios-certificates.git") # a private repository, nothing else in it
storage_mode("git")
type("appstore")
app_identifier(["com.example.hello", "com.example.hello.widget"]) # include every extension
team_id("ABCDE12345")
readonly(true) # CI never creates or revokes anythingEverything in the store is encrypted with MATCH_PASSWORD. One person (or a protected job) runs match in write mode when certificates expire or a new extension appears; every other machine runs it read-only and just installs what's there. That turns "it signs on Ana's Mac" into "it signs anywhere with the password". fastlane match nuke distribution revokes everything for a clean restart (it affects the whole team, so treat it as an incident procedure).
The Fastfile
default_platform(:ios)
platform :ios do
desc "Tests on the simulator"
lane :test do
run_tests(scheme: "Hello", devices: ["iPhone 16"], result_bundle: true) # a.k.a. scan
end
desc "Archive, sign and upload a build to TestFlight"
lane :beta do
setup_ci # temporary keychain on CI, no-op locally
api_key = app_store_connect_api_key(
key_id: ENV.fetch("ASC_KEY_ID"), issuer_id: ENV.fetch("ASC_ISSUER_ID"), key_content: ENV.fetch("ASC_KEY_P8")
)
match(type: "appstore", readonly: true) # install certificate + profiles
increment_build_number(build_number: ENV.fetch("BUILD_NUMBER"), xcodeproj: "Hello.xcodeproj")
build_app( # a.k.a. gym
scheme: "Hello",
configuration: "Release",
export_method: "app-store",
output_directory: "build",
xcargs: "-onlyUsePackageVersionsFromResolvedFile"
)
upload_to_testflight( # a.k.a. pilot
api_key: api_key,
skip_waiting_for_build_processing: true,
changelog: "Build #{ENV.fetch('BUILD_NUMBER')}"
)
end
endWhat each step replaces from the previous lessons:
| Action | Does for you |
|---|---|
setup_ci |
the temporary keychain dance from Code Signing & Provisioning |
match |
importing the .p12, installing profiles, set-key-partition-list |
increment_build_number |
writes CURRENT_PROJECT_VERSION (via agvtool) from CI's build number |
build_app |
xcodebuild archive + -exportArchive with a generated ExportOptions.plist, .dSYM zipped next to the .ipa |
upload_to_testflight |
the upload via the App Store Connect API; skip_waiting_for_build_processing avoids paying for a Mac while Apple processes the build |
Build numbers that never collide
App Store Connect rejects a build number already used for the same version. If several pipelines upload (branches, re-runs), ask Apple for the last one instead of trusting a counter:
latest = latest_testflight_build_number(version: "1.4.2", api_key: api_key)
increment_build_number(build_number: latest + 1)The model below compares that strategy with a CI run counter when two workflows upload to the same app:
uploaded = set()
def upload(build, source):
if build in uploaded:
return f"{source:<10} build {build:>3}: REJECTED (already used)"
uploaded.add(build)
return f"{source:<10} build {build:>3}: accepted"
# Strategy 1: each workflow uses its own run counter
for source, run in [("main", 41), ("hotfix", 1), ("main", 42), ("hotfix", 2), ("hotfix", 42)]:
print(upload(run, source))
print("---")
uploaded.clear()
# Strategy 2: ask App Store Connect for the latest number, add one
for source in ["main", "hotfix", "main", "hotfix", "hotfix"]:
latest = max(uploaded, default=0)
print(upload(latest + 1, source))main build 41: accepted
hotfix build 1: accepted
main build 42: accepted
hotfix build 2: accepted
hotfix build 42: REJECTED (already used)
---
main build 1: accepted
hotfix build 2: accepted
main build 3: accepted
hotfix build 4: accepted
hotfix build 5: accepted
The "latest + 1" strategy has its own race (two jobs reading the same latest number at the same moment), so serialise release jobs with a CI concurrency group, or combine both: a large per-workflow offset plus the run number.
Test yourself
What does match(type: "appstore", readonly: true) do on a CI machine?
- Creates new certificates every run
- Decrypts the team's stored certificate and profiles and installs them, without creating or revoking anything
- Uploads the app
- Registers devices
Why use an App Store Connect API key rather than an Apple ID on CI?
- Apple IDs can't upload
- API keys don't need passwords or two-factor prompts and can be scoped and revoked
- It's faster to build
- It replaces provisioning profiles
Which Fastlane action wraps xcodebuild archive and -exportArchive?
- pilot
- match
- gym (build_app)
- scan