Skip to content
CoreMacs
Security & signingGuide

Code signing in CI without the pain: temporary keychains, profiles and API keys

A headless build cannot answer a keychain prompt. This guide covers what Apple code signing needs in CI: identities, a per-job keychain, profiles and the Xcode 16 folder change, manual versus automatic signing, API keys and common errors.

CoreMacs News Desk·Published ·Last verified ·5 min read
A silver Mac Studio style computer on a wooden desk beside a keyboard, with a display and plant behind.

On a developer's laptop a person is usually present: the keychain is unlocked at login and permission dialogs can be answered. A CI job has nobody to click anything, so signing has to work without one.

The pieces: certificate, private key, profile

Apple's TN3161 defines a code-signing identity as a certificate plus the private key that matches its public key; the certificate alone cannot sign. When you or Xcode request a certificate, that key is generated on the Mac and kept in its login keychain. For CI you export the identity as a password-protected PKCS#12 file, usually .p12.

A provisioning profile, as TN3125 describes it, ties together who may sign (its certificates), which app (an App ID), where it runs (most profiles list devices), when it expires and which entitlements are allowed. It ships inside the app as embedded.mobileprovision, or Contents/embedded.provisionprofile on macOS. When signing fails, check these pieces against each other.

Why the login keychain is a trap

The login keychain is tied to an interactive login and may prompt for access, which a headless job cannot answer. Apple Developer Technical Support's post on errSecInternalComponent says this codesign error typically affects signing over SSH or on CI servers, and that logging in via SSH does not unlock the keychain. A keychain that the job creates, unlocks and configures itself avoids those prompts and, once deleted, leaves no secrets behind.

The steps follow GitHub's guide for macOS runners; option notes come from Apple's security man page source.

Creating and unlocking a temporary keychain

KEYCHAIN_PATH="$RUNNER_TEMP/app-signing.keychain-db"

security create-keychain -p "$KEYCHAIN_PASSWORD" "$KEYCHAIN_PATH"
security set-keychain-settings -lut 21600 "$KEYCHAIN_PATH"
security unlock-keychain -p "$KEYCHAIN_PASSWORD" "$KEYCHAIN_PATH"
security import "$CERTIFICATE_PATH" -P "$P12_PASSWORD" -A -t cert -f pkcs12 -k "$KEYCHAIN_PATH"
security set-key-partition-list -S apple-tool:,apple: -k "$KEYCHAIN_PASSWORD" "$KEYCHAIN_PATH"
security list-keychain -d user -s "$KEYCHAIN_PATH"

What each step does:

  • create-keychain and unlock-keychain take a password; GitHub suggests any newly generated random string.
  • set-keychain-settings -lut 21600 locks the keychain on sleep or after a six-hour timeout, so it stays open through long builds.
  • import loads the .p12. The man page says -A lets any application access the key without warning, calling it "insecure, not recommended!"; the narrower -T names specific applications, as in the DTS post's -T /usr/bin/codesign.
  • set-key-partition-list sets an access rule based on the calling tool's code signature; the man page says apple: must be listed for /usr/bin/codesign to use the key.
  • list-keychain -d user -s sets the user search list to the new keychain rather than adding to it; on machines you keep, restore the old list afterwards.

Keep the .p12 and its password in your CI secret store, never in the repository. GitHub-hosted runners are destroyed after each job; on a self-hosted runner, delete the keychain even after a failure, as GitHub's cleanup step does with if: ${{ always() }}.

security delete-keychain "$KEYCHAIN_PATH"

Where provisioning profiles go

GitHub's example copies the profile into the folder used by Xcode versions before 16:

mkdir -p ~/Library/MobileDevice/Provisioning\ Profiles
cp build_pp.mobileprovision ~/Library/MobileDevice/Provisioning\ Profiles

In an accepted answer on the Apple Developer Forums, an Apple DTS engineer gives ~/Library/Developer/Xcode/UserData/Provisioning Profiles for Xcode 16 and later, so check which folder your Xcode reads and adjust the copy and cleanup steps. Use .mobileprovision for iOS and .provisionprofile for macOS targets, as GitHub notes, and remove profiles after the job on machines you keep.

Manual or automatic signing in xcodebuild

Manual signing

You pick the certificate and profile in the project's signing settings, and the job installs them. Explicit inputs make failures easier to trace, which suits CI well.

Automatic signing with an App Store Connect API key

Automatic signing needs xcodebuild to talk to the Apple Developer website, which -allowProvisioningUpdates permits, as Apple's WWDC21 session Distribute apps in Xcode with cloud signing explains. Signing in to Xcode on the machine also works, but that session persists only for a period of time. Since Xcode 13, according to its release notes, xcodebuild can instead authenticate with an App Store Connect API key, enabling automatic signing on build machines and in CI.

Create a team key in App Store Connect under Users and Access, Integrations, and note the key ID and issuer ID. Apple's documentation says the private key can be downloaded a single time and Apple doesn't keep a copy, and that individual keys cannot use the API's Provisioning endpoints. Pass the values like this:

xcodebuild ... -allowProvisioningUpdates \
  -authenticationKeyPath "$API_KEY_PATH" \
  -authenticationKeyID "$API_KEY_ID" \
  -authenticationKeyIssuerID "$API_ISSUER_ID"

Here ... stands for your build or export arguments. Confirm the options with xcodebuild -help or man xcodebuild on your Xcode version, and test before relying on them. Apple advises against storing keys in a code repository, so write the key to a temporary path and delete it after the job.

Common errors and what to check

This table reflects common, unverified community experience, not an official Apple mapping, and message text varies between Xcode versions.

SymptomWhat to check
codesign reports errSecInternalComponent, or signing stallsKeychain unlocked and in the search list? Partition list set after the import? See the DTS post above.
No signing identity is foundWas the .p12 imported into the searched keychain, and does it contain the private key?
The profile lacks a certificate, device or entitlementUpdate the profile in your developer account, then reinstall it.
No profile is found for the bundle IDRight folder for your Xcode, right extension, matching App ID?
The profile or certificate has expiredCheck the expiration dates; both expire.

A short checklist

  1. Create and unlock a fresh keychain for each job.
  2. Import the .p12, then set the partition list.
  3. Install profiles where your Xcode version looks, with the right extension.
  4. Use manual signing, or automatic signing with an API key.
  5. Clean up the keychain, profiles and key file, even after a failure.

Sources

Share