TExcelTAD
Home › Mobile Build Pipelines: Gradle/AGP, Xcode, SwiftPM & Fastlane › xcodebuild: Build, Test, Archive & Export

xcodebuild: Build, Test, Archive & Export

xcodebuild is Xcode without the window. Everything a CI job does on the Apple side goes through it: listing schemes, building, running tests on a simulator, producing an archive and exporting an .ipa from it. It's the counterpart of ./gradlew assembleRelease bundleRelease.

Note: non-run (macOS only). Commands use the flags documented in man xcodebuild for current Xcode releases; outputs are trimmed to the lines that matter.

Finding out what's there

bash
xcodebuild -version
xcodebuild -list -project Hello.xcodeproj          # or -workspace Hello.xcworkspace
xcrun simctl list devices available | grep iPhone   # simulators you can test on
text
Information about project "Hello":
    Targets:
        Hello
        HelloTests
    Build Configurations:
        Debug
        Release
    If no build configuration is specified and -scheme is not passed then "Release" is used.
    Schemes:
        Hello

Use -workspace whenever the project lives in a workspace (CocoaPods projects always do); -project with a workspace-only setup builds without the other projects and fails on missing modules.

The four actions

bash
# 1. build: compile for the simulator, no signing needed
xcodebuild build \
  -scheme Hello -configuration Debug \
  -destination 'generic/platform=iOS Simulator' \
  -derivedDataPath build/DerivedData

# 2. test: build and run the scheme's tests on a simulator, keep a result bundle
xcodebuild test \
  -scheme Hello \
  -destination 'platform=iOS Simulator,name=iPhone 16' \
  -resultBundlePath build/TestResults.xcresult

# 3. archive: Release build for devices, signed, into an .xcarchive
xcodebuild archive \
  -scheme Hello -configuration Release \
  -destination 'generic/platform=iOS' \
  -archivePath build/Hello.xcarchive

# 4. export: re-sign and package the archive as an .ipa for a distribution method
xcodebuild -exportArchive \
  -archivePath build/Hello.xcarchive \
  -exportOptionsPlist ExportOptions.plist \
  -exportPath build/export
Flag Purpose
-scheme, -configuration what to build and with which settings
-destination concrete (platform=iOS Simulator,name=iPhone 16) or generic (generic/platform=iOS) target
-derivedDataPath keep intermediates inside the job's folder (cacheable, no cross-job pollution)
-resultBundlePath an .xcresult with test results, logs and coverage, readable with xcrun xcresulttool
-allowProvisioningUpdates let Xcode create/download signing assets via the App Store Connect API (with -authenticationKeyPath, -authenticationKeyID, -authenticationKeyIssuerID)
-skipPackagePluginValidation, -skipMacroValidation trust Swift package plugins/macros non-interactively on CI
-onlyUsePackageVersionsFromResolvedFile fail instead of re-resolving when Package.resolved is out of date
KEY=value override any build setting for this run

xcodebuild test exits non-zero on failures. Pipe its very verbose log through xcbeautify (see Toolkit: xcbeautify & the Store APIs) with set -o pipefail, or the pipe hides the exit code.

Archives

An archive is a folder bundle:

text
Hello.xcarchive/
├── Info.plist                         scheme, version, creation date, team
├── Products/Applications/Hello.app    the signed app
└── dSYMs/Hello.app.dSYM               debug symbols for crash symbolication (keep them!)

The .dSYM plays the role of R8's mapping.txt: without the exact one for a build, crash reports from that build stay unsymbolicated addresses. Upload it to your crash reporter (and App Store Connect receives it with the upload when uploadSymbols is on), and archive it with the build.

ExportOptions.plist

-exportArchive needs to know how to distribute. The answer lives in a property list:

xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>method</key>
    <string>app-store-connect</string>
    <key>teamID</key>
    <string>ABCDE12345</string>
    <key>signingStyle</key>
    <string>manual</string>
    <key>provisioningProfiles</key>
    <dict>
        <key>com.example.hello</key>
        <string>Hello App Store</string>
    </dict>
    <key>uploadSymbols</key>
    <true/>
    <key>manageAppVersionAndBuildNumber</key>
    <false/>
</dict>
</plist>
method For Older name (still accepted)
app-store-connect App Store and TestFlight app-store
release-testing ad hoc installs on registered devices ad-hoc
enterprise in-house distribution (Enterprise Program)
debugging development-signed builds for your devices development
developer-id / mac-application macOS outside / inside the Mac App Store

Set manageAppVersionAndBuildNumber to false on CI: otherwise Xcode may bump the build number during export, and the artifact no longer matches what your pipeline computed. With destination = upload the export uploads straight to App Store Connect instead of writing an .ipa.

Teams often generate this file in the pipeline rather than committing one per method. A runnable model that writes and checks it with Python's plistlib (the format Xcode reads):

python · try it yourself
import plistlib

METHODS = {"app-store-connect", "release-testing", "enterprise", "debugging", "developer-id", "mac-application"}
RENAMED = {"app-store": "app-store-connect", "ad-hoc": "release-testing", "development": "debugging"}

def export_options(method, team, profiles):
    method = RENAMED.get(method, method)
    if method not in METHODS:
        raise ValueError(f"unknown export method {method!r}")
    if len(team) != 10 or not team.isalnum():
        raise ValueError(f"team ID {team!r} must be 10 alphanumeric characters")
    return {
        "method": method,
        "teamID": team,
        "signingStyle": "manual",
        "provisioningProfiles": dict(sorted(profiles.items())),
        "uploadSymbols": method == "app-store-connect",
        "manageAppVersionAndBuildNumber": False,
    }

opts = export_options("app-store", "ABCDE12345", {"com.example.hello": "Hello App Store",
                                                 "com.example.hello.widget": "Hello Widget App Store"})
xml = plistlib.dumps(opts, sort_keys=True).decode()
print("\n".join(xml.splitlines()[3:]))
for bad in [("store", "ABCDE12345"), ("ad-hoc", "abc")]:
    try:
        export_options(bad[0], bad[1], {})
    except ValueError as e:
        print("error:", e)
<dict>
	<key>manageAppVersionAndBuildNumber</key>
	<false/>
	<key>method</key>
	<string>app-store-connect</string>
	<key>provisioningProfiles</key>
	<dict>
		<key>com.example.hello</key>
		<string>Hello App Store</string>
		<key>com.example.hello.widget</key>
		<string>Hello Widget App Store</string>
	</dict>
	<key>signingStyle</key>
	<string>manual</string>
	<key>teamID</key>
	<string>ABCDE12345</string>
	<key>uploadSymbols</key>
	<true/>
</dict>
</plist>
error: unknown export method 'store'
error: team ID 'abc' must be 10 alphanumeric characters

Note the second profile: every target that ships inside the app (here a widget extension) has its own bundle ID and needs its own provisioning profile in a manual-signing export. Forgetting the extension is the most common -exportArchive failure.

The two bad inputs are rejected before Xcode ever sees them: a pipeline that validates its own configuration fails in a second instead of after a 20-minute archive.

Test yourself

Which xcodebuild step turns an .xcarchive into an .ipa for TestFlight?

  1. xcodebuild build
  2. xcodebuild archive
  3. xcodebuild -exportArchive with an ExportOptions.plist whose method is app-store-connect
  4. xcrun simctl

What is the iOS equivalent of R8's mapping.txt for reading crash reports?

  1. Info.plist
  2. The .dSYM debug symbols in the archive
  3. ExportOptions.plist
  4. Package.resolved

Why pass -derivedDataPath build/DerivedData on CI?

  1. It's required for archives
  2. It keeps intermediates inside the job's workspace, so they can be cached and don't leak between jobs
  3. It enables code signing
  4. It makes builds incremental across Xcode versions