Code signing & build

xcodebuild

The command-line tool that builds, tests, archives, and exports Xcode projects. Every CI pipeline that ships an Apple app runs xcodebuild under the hood.

xcodebuild commandXcode command line build

xcodebuild is the command-line interface to Xcode's build system. Everything the Xcode GUI does when you press Product > Archive, xcodebuild does from a terminal: compile, run tests, produce an .xcarchive, and export a signed .ipa. If a build runs on a CI machine, xcodebuild is the tool doing the work, whether you call it directly or through fastlane, which wraps it.

The archive and export flow

Shipping a build from the command line is two invocations. The first compiles and signs the app into an archive. The second re-signs and packages it for a specific distribution method, driven by an exportOptions.plist file. When the signing step fails, xcodebuild reports it as Command CodeSign failed with a nonzero exit code and prints the real reason a few lines above, which is worth knowing before you pipe the output through a log formatter.

bash
# 1. Build and archive
xcodebuild archive \
  -scheme MyApp \
  -configuration Release \
  -archivePath build/MyApp.xcarchive

# 2. Export a signed .ipa from the archive
xcodebuild -exportArchive \
  -archivePath build/MyApp.xcarchive \
  -exportOptionsPlist ExportOptions.plist \
  -exportPath build/

The signing flags that matter

CODE_SIGN_IDENTITY
Which certificate signs the build, matched by name against the keychain (for example "Apple Distribution"). Usually left to the project settings rather than overridden on the command line.
-allowProvisioningUpdates
Lets xcodebuild talk to the Apple Developer portal to create or fix profiles during the build, the command-line counterpart of Xcode's automatic signing. On CI, pair it with -authenticationKeyPath and the related flags so it authenticates with an App Store Connect API key instead of an Apple ID.
-exportOptionsPlist
Path to the exportOptions.plist that tells -exportArchive which distribution method, team, and profiles to use.
OTHER_CODE_SIGN_FLAGS="--keychain <path>"
Points signing at a specific keychain. CI jobs create a temporary keychain, import the .p12 into it, and pass it here so builds never touch the login keychain.

Where the signing material comes from

xcodebuild does not fetch certificates or profiles by itself (unless -allowProvisioningUpdates is on). It signs with whatever is already present: certificates and private keys come from a macOS keychain, and provisioning profiles are read from ~/Library/Developer/Xcode/UserData/Provisioning Profiles (and, on newer Xcode versions, ~/Library/MobileDevice/Provisioning Profiles). CI setup is mostly the work of getting the right files into those two places before xcodebuild runs.

FAQ

Common questions about xcodebuild

What is the difference between xcodebuild and Xcode?

Same build system, different interface. Xcode is the GUI; xcodebuild drives the identical build engine from the command line, which is what CI machines need. An archive produced by xcodebuild is byte-for-byte the same kind of artifact as one produced from the Xcode Organizer.

How does xcodebuild sign a build?

During archive it signs with the certificate and provisioning profile resolved from the project's signing settings, looking the certificate up in the keychain. During -exportArchive it re-signs according to the exportOptions.plist. If the certificate, its private key, or the profile is missing, the build fails with a signing error.

Do I need a Mac to run xcodebuild?

Yes. xcodebuild ships with Xcode and only runs on macOS. Cloud CI providers offer macOS runners for exactly this reason, and services like Codemagic, Bitrise, and GitHub Actions all invoke xcodebuild on Mac hardware behind the scenes.

Why does xcodebuild fail in CI when the same build works in Xcode?

Almost always signing state: the CI keychain does not contain the certificate and private key, the provisioning profile is not installed on the runner, or the keychain is locked so codesign cannot read the key. Staging those pieces (or letting a tool stage them) before calling xcodebuild fixes the vast majority of CI-only failures.

Keep reading

Related terms

Next step

Help articles on this topic

Notice a definition that has drifted? Email support@hexsign.io and we'll update it. Browse the full glossary or jump to the help center.

Try HexSign

Know before a certificate or profile expires.

HexSign tracks every Apple certificate and provisioning profile and alerts you ahead of expiry. The Free plan covers tracking and alerts. Paid plans renew them for you.