# Set Up Xcode Code Signing on a Remote Mac

Canonical: https://figment.so/blog/mac-xcode-code-signing-remote

Import the .p12 into a temporary, unlocked keychain, install the provisioning profile, run xcodebuild archive with manual signing, then verify the exported .ipa with codesign before upload.

**Code signing on a remote Mac works once three things are in place: a signing certificate (with its private key) in an unlocked keychain, a matching provisioning profile on disk, and the right flags passed to `xcodebuild`.** Drive the whole flow from the Mac's own terminal, over SSH or in a remote desktop session.

## What does code signing require?

Three pieces must be in place before `xcodebuild` can sign a build. Apple's reference for the archive and export flow is [Distributing your app to registered devices](https://developer.apple.com/documentation/xcode/distributing-your-app-to-registered-devices).

1. **A signing certificate** (Distribution or Development) in the Mac's keychain, including its private key. The certificate record lives in your Apple Developer account. The private key was generated on the Mac where the Certificate Signing Request was created, and Apple does not hold it. Export both together as an encrypted `.p12`.
2. **A provisioning profile** (`.mobileprovision`) that ties your App ID, certificate and, for Development and Ad Hoc, registered devices. Download it from the Apple Developer site. The profile is a CMS-signed file and carries no private key.
3. **An unlocked keychain at build time**, so `codesign` can read the private key without a GUI prompt. On an unattended Mac, any dialog stalls the build.

## How do I export a .p12 from my local Mac?

On the Mac where the CSR was originally created:

1. Open **Keychain Access** (`/Applications/Utilities/Keychain Access.app`).
2. Under *My Certificates*, find your certificate. A disclosure triangle shows the private key beneath it. Select the certificate **and** its private key together.
3. Right-click, choose **Export 2 items**, and pick the `.p12` format.
4. Set a strong passphrase and store it in your team secret manager (GitHub Actions secrets, Vault, 1Password), not in a plaintext file.

Transfer the `.p12` and `.mobileprovision` to the remote Mac with `scp` over SSH, or download them there from an authorized artifact store:

```bash
scp -i ~/.ssh/your_key ~/Desktop/AppDist.p12 user@YOUR_MAC_IP:~/signing/AppDist.p12
scp -i ~/.ssh/your_key ~/Desktop/AppStore_com.example.app.mobileprovision \
    user@YOUR_MAC_IP:~/signing/
```

Move only the exact secret names you need. Never dump a whole shell environment into a file, because it will contain unrelated API keys. Restore each secret value on the Mac side from your secret manager.

## How do I import the .p12 into a build keychain?

Create a short-lived build keychain, so the private key does not linger in the login keychain between sessions. The `security import` and `set-key-partition-list` pattern below follows GitHub's documented macOS-runner recipe: [Installing an Apple certificate on macOS runners for Xcode development](https://docs.github.com/en/actions/use-cases-and-examples/deploying/installing-an-apple-certificate-on-macos-runners-for-xcode-development).

```bash
#!/usr/bin/env bash
# sign-setup.sh — prepares a one-shot build keychain
set -euo pipefail
umask 077   # restrict perms before we touch secrets

KEYCHAIN_NAME="build-$(date +%Y%m%d%H%M%S).keychain"
KEYCHAIN_PATH="$HOME/Library/Keychains/${KEYCHAIN_NAME}-db"
KEYCHAIN_PASSWORD="$(openssl rand -base64 32)"   # ephemeral; not stored

# Preserve the current user keychain search list so we can restore it
ORIGINAL_LIST=$(security list-keychains -d user | tr -d '"' | xargs)

cleanup() {
  # Restore the original search list, then drop the ephemeral keychain
  # shellcheck disable=SC2086
  security list-keychains -d user -s $ORIGINAL_LIST || true
  security delete-keychain "$KEYCHAIN_NAME" 2>/dev/null || true
}
trap cleanup EXIT

security create-keychain -p "$KEYCHAIN_PASSWORD" "$KEYCHAIN_NAME"
security set-keychain-settings -lut 21600 "$KEYCHAIN_NAME"   # 6-hour timeout
security unlock-keychain -p "$KEYCHAIN_PASSWORD" "$KEYCHAIN_NAME"

# Prepend the build keychain; keep the user's original entries
# shellcheck disable=SC2086
security list-keychains -d user -s "$KEYCHAIN_NAME" $ORIGINAL_LIST

# P12_PASSWORD comes from your CI secret manager (never from shell history)
security import ~/signing/AppDist.p12 \
  -P "$P12_PASSWORD" \
  -A -t cert -f pkcs12 \
  -k "$KEYCHAIN_NAME"

security set-key-partition-list \
  -S apple-tool:,apple: \
  -s -k "$KEYCHAIN_PASSWORD" \
  "$KEYCHAIN_NAME"

security find-identity -vp codesigning "$KEYCHAIN_NAME"

# If the next step is the build, disable the EXIT trap so the keychain stays.
# The build script must call its own cleanup (see end of this page).
trap - EXIT
echo "KEYCHAIN_NAME=$KEYCHAIN_NAME"
```

The `-A` flag on `security import` grants application access, and `set-key-partition-list` then narrows it to `codesign` and `security` through Apple's partition identifiers. Skipping that call is the common cause of a `codesign` "allow access" dialog, which stalls any unattended build.

## How do I install the provisioning profile?

Xcode discovers profiles in one directory, keyed by UUID. The filename does not matter:

```bash
PROFILE_DIR="$HOME/Library/MobileDevice/Provisioning Profiles"
mkdir -p "$PROFILE_DIR"
cp ~/signing/AppStore_com.example.app.mobileprovision "$PROFILE_DIR/"

# Sanity-check the embedded expiry date
security cms -D -i "$PROFILE_DIR/AppStore_com.example.app.mobileprovision" \
  | plutil -extract ExpirationDate xml1 -o - - \
  | grep string
```

## How do I run xcodebuild archive with manual signing?

Set `KEYCHAIN_NAME` to the value printed by `sign-setup.sh`, then archive and export. Replace `YourAppScheme` and `YourApp.xcworkspace` with the real names from your project. This is an untested example, not a benchmark.

```bash
set -euo pipefail

: "${KEYCHAIN_NAME:?run sign-setup.sh first and export KEYCHAIN_NAME}"

SCHEME="YourAppScheme"
WORKSPACE="YourApp.xcworkspace"
ARCHIVE_PATH="$HOME/build/YourApp.xcarchive"
EXPORT_PATH="$HOME/build/export"
EXPORT_OPTIONS="$HOME/signing/ExportOptions.plist"

xcodebuild archive \
  -workspace "$WORKSPACE" \
  -scheme "$SCHEME" \
  -configuration Release \
  -archivePath "$ARCHIVE_PATH" \
  CODE_SIGN_STYLE=Manual \
  CODE_SIGN_IDENTITY="Apple Distribution" \
  PROVISIONING_PROFILE_SPECIFIER="AppStore_com.example.app" \
  OTHER_CODE_SIGN_FLAGS="--keychain $KEYCHAIN_NAME"

xcodebuild -exportArchive \
  -archivePath "$ARCHIVE_PATH" \
  -exportOptionsPlist "$EXPORT_OPTIONS" \
  -exportPath "$EXPORT_PATH"
```

Accepted `method` values in `ExportOptions.plist` have changed across Xcode versions. Run `xcodebuild -exportArchive -help` on the installed toolchain to confirm the exact string. Recent Xcode lines accept `app-store-connect`, and older ones use `app-store`.

```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>YOUR_TEAM_ID</string>
  <key>signingStyle</key> <string>manual</string>
</dict>
</plist>
```

## How do I verify the signature before uploading?

Validate before uploading. If `codesign --verify` exits non-zero, do not treat the failed run as a successful build.

```bash
set -euo pipefail

IPA="$EXPORT_PATH/YourApp.ipa"
WORK=$(mktemp -d)
trap 'rm -rf "$WORK"' EXIT

unzip -q "$IPA" -d "$WORK"
APP_PATH=$(find "$WORK/Payload" -maxdepth 1 -name '*.app' -print -quit)

codesign --verify --deep --strict --verbose=2 "$APP_PATH"
codesign -d --entitlements :- "$APP_PATH" | plutil -p -
```

| Error message | Likely cause |
|---|---|
| `a sealed resource is missing or invalid` | A file changed after signing; re-archive from a clean state |
| `code object is not signed at all` | A framework or dylib was not included in signing; check Embed & Sign |
| `resource envelope is obsolete` | Legacy signature format; rebuild with the current toolchain |
| Entitlement mismatch | A capability in the app is not enabled on the provisioning profile; update the profile in the Apple Developer site |

## How do I clean up after the build?

On a reused Mac, drop the ephemeral keychain and the on-disk `.p12` when the job is done, and restore the original keychain search list you captured earlier:

```bash
# shellcheck disable=SC2086
security list-keychains -d user -s $ORIGINAL_LIST
security delete-keychain "$KEYCHAIN_NAME"
rm -f ~/signing/AppDist.p12
```

Do not rely on `if: always()` to clean up after a machine failure or forced termination. Verify cleanup before reusing the runner, or replace its workspace with a clean image and check that no previous signing material remains.

## Which Figment Mac plan fits code signing?

Pick the plan by the Xcode version your project needs. The [Intel Mac VM](/mac-hosting#intel-vm) ($25 a month, $2 a day) runs macOS Sequoia, fits Xcode 26.3 or earlier, and can sign and archive iOS builds. Apple documents Xcode 26 or earlier as the last line supporting Intel in [Porting your macOS apps to Apple silicon](https://developer.apple.com/documentation/apple-silicon/porting-your-macos-apps-to-apple-silicon). For Xcode 26.4 or later, Xcode 27 or arm64-only tools, use the [Apple Silicon Mac VM](/mac-hosting#apple-silicon-vm) ($39 a month, $6 a day). We do not run CI for you, and a persistent disk is not a backup, so keep signing material in your own secret manager. See all [Mac hosting plans](/mac-hosting).

**Related guides:** [Swift Package Manager on a Remote Mac](/blog/mac-swift-package-manager-remote) · [SwiftUI Previews on a Rented Mac](/blog/mac-swiftui-previews-rented-mac) · [GitHub Actions Self-Hosted Runner on Mac](/blog/mac-github-actions-self-hosted-runner)
