# Notarize a macOS App Inside a CI Pipeline

Canonical: https://figment.so/blog/mac-notarize-app-ci-pipeline

Sign the app inside-out, wrap it in a signed DMG, submit the DMG with xcrun notarytool, poll until Accepted on a deadline, staple the ticket and verify with spctl. notarytool does not accept a bare .app.

**To notarize in CI, package the signed app into a signed DMG, submit it with `xcrun notarytool submit`, poll `notarytool info` until the status is `Accepted` or a deadline passes, staple the ticket to the DMG, and verify with `spctl`.** `notarytool submit` does not accept a `.app` bundle directly. Apple's guide says "you can't upload the `.app` bundle directly to the notary service", which accepts UDIF disk images, signed flat installer packages and ZIP archives. The commands are documented in [Customizing the notarization workflow](https://developer.apple.com/documentation/security/customizing-the-notarization-workflow), and the prerequisites in [Notarizing macOS software before distribution](https://developer.apple.com/documentation/security/notarizing-macos-software-before-distribution).

## What do I need before Apple accepts a submission?

You need a Developer ID Application certificate, an App Store Connect API key and an unlocked keychain. All three must line up.

**Developer ID Application certificate.** Sign an app or DMG with a **Developer ID Application** identity. **Developer ID Installer** is a different certificate type used only for `.pkg` installer payloads. It is not interchangeable, and signing a `.app` or `.dmg` with the installer certificate fails notarization. The private key must be in a keychain on the current user's search list and unlocked when `codesign` runs. The login keychain is the common place, but any custom keychain in the search path works while it stays unlocked for the signing step, for example a job-scoped keychain created with `security create-keychain` and added with `security list-keychains -s`. Import the identity from an encrypted `.p12` using a password kept in the CI secret manager, never in shell history, and never by writing the whole environment to a plaintext file, which routinely contains unrelated secrets. Provisioning profiles, where a target uses them, are files under `~/Library/MobileDevice/Provisioning Profiles/`, not keychain exports.

**App Store Connect API key.** For unattended CI, authenticate `notarytool` with an API key. Create it in App Store Connect under Users and Access → Integrations → App Store Connect API, download the `.p8` once (it cannot be downloaded again), and note the Key ID and your team's Issuer ID. Store the `.p8` in your CI secret manager and write it to a path at job start. On a long-lived Mac, the keychain-profile shortcut from Apple's workflow guide may be more convenient: `xcrun notarytool store-credentials "notarytool-profile" --key /path/to/AuthKey.p8 --key-id ABC123 --issuer 12345-...` writes the credential into a keychain, and later commands pass `--keychain-profile "notarytool-profile"`. That profile is scoped to one keychain on one machine, so ephemeral runners should pass `--key`, `--key-id` and `--issuer` directly from secrets.

**An unlocked keychain.** `codesign` and `--keychain-profile` lookups both need the backing keychain open. This command returns a non-zero exit code when the password is wrong or the file is missing, so check it and do not swallow the output:

```bash
security unlock-keychain -p "$KEYCHAIN_PASSWORD" "$KEYCHAIN_PATH"
```

If the keychain can relock on idle, set `security set-keychain-settings -lut <seconds>` to a value longer than the job.

## How do I sign the app and build the DMG?

Sign from the inside out, then build and sign the DMG as a separate step. Apple's notary service is strict about signatures. Use a Developer ID Application identity, the hardened runtime (`--options runtime`) and a secure timestamp (`--timestamp`). Avoid `--deep`: sign every embedded framework, helper tool and XPC service explicitly, then the outer bundle, so each nested binary gets its own fresh signature. Then create the DMG around the signed app and sign the DMG with the same identity. That gives one artifact that `notarytool submit` can upload, `stapler staple` can attach a ticket to, and `spctl --assess --type open` can verify.

```bash
# Untested example. Run against your own artifact before trusting it in CI.
# 1) Sign each embedded framework/helper first, then the outer bundle.
codesign --sign "Developer ID Application: Example Corp (ABCD123456)" \
  --options runtime --timestamp --force \
  MyApp.app/Contents/Frameworks/SomeHelper.framework
codesign --sign "Developer ID Application: Example Corp (ABCD123456)" \
  --options runtime --timestamp --force \
  MyApp.app

# 2) Build the DMG around the signed app, then sign the DMG.
hdiutil create -volname "MyApp" -srcfolder MyApp.app \
  -ov -format UDZO MyApp.dmg
codesign --sign "Developer ID Application: Example Corp (ABCD123456)" \
  --timestamp --force MyApp.dmg
```

ZIP submissions are also accepted, but `stapler` cannot staple a ZIP directly, so a signed DMG keeps upload, staple and the `spctl` check on one artifact.

## What does notarize-and-staple.sh look like?

The script takes exactly one argument, the path to a signed `.dmg`, and refuses anything else before it spends an upload. `TIMEOUT_SECONDS` and `POLL_SECONDS` are validated as positive integers. The poll loop calls `xcrun notarytool info` with `--output-format json`, reads the parsed `status` field, and treats anything other than `Accepted` as a failure, including a timeout. The log is always fetched, because Apple's docs note it can carry warnings even on success. `spctl --assess --type open --context context:primary-signature` is the Gatekeeper check that matches a DMG. The script has not been executed here; run it against one of your own signed DMGs before you trust it in CI.

```bash
#!/usr/bin/env bash
# notarize-and-staple.sh — Notarize and staple a signed .dmg.
#
# Usage: notarize-and-staple.sh /path/to/Signed.dmg
#
# Required env (set by your CI secret manager, never written to a dumped env file):
#   ASC_API_KEY_PATH    Filesystem path to the App Store Connect .p8 key
#   ASC_API_KEY_ID      App Store Connect Key ID
#   ASC_API_ISSUER_ID   App Store Connect Issuer ID
#
# Optional (defaults in parentheses):
#   TIMEOUT_SECONDS (1800)  Deadline for Apple-side processing
#   POLL_SECONDS    (30)    Seconds between status polls
#
# Untested example.

set -euo pipefail

DMG_PATH="${1:-}"

is_positive_int() { [[ "$1" =~ ^[1-9][0-9]*$ ]]; }

if [[ -z "$DMG_PATH" ]]; then
  echo "Usage: $0 /path/to/Signed.dmg" >&2; exit 2
fi
if [[ "$DMG_PATH" != *.dmg ]]; then
  echo "Refusing: this script submits a signed .dmg (got '$DMG_PATH')." >&2; exit 2
fi
if [[ ! -f "$DMG_PATH" ]]; then
  echo "Refusing: '$DMG_PATH' does not exist or is not a file." >&2; exit 2
fi

: "${ASC_API_KEY_PATH:?Set ASC_API_KEY_PATH}"
: "${ASC_API_KEY_ID:?Set ASC_API_KEY_ID}"
: "${ASC_API_ISSUER_ID:?Set ASC_API_ISSUER_ID}"

TIMEOUT_SECONDS="${TIMEOUT_SECONDS:-1800}"
POLL_SECONDS="${POLL_SECONDS:-30}"
is_positive_int "$TIMEOUT_SECONDS" || { echo "TIMEOUT_SECONDS must be a positive integer." >&2; exit 2; }
is_positive_int "$POLL_SECONDS"    || { echo "POLL_SECONDS must be a positive integer." >&2; exit 2; }

AUTH=(--key "$ASC_API_KEY_PATH" --key-id "$ASC_API_KEY_ID" --issuer "$ASC_API_ISSUER_ID")

parse_json() { /usr/bin/python3 -c "import sys,json;print(json.load(sys.stdin).get('$1',''))"; }

echo "==> Submitting $DMG_PATH"
SUBMIT_JSON=$(xcrun notarytool submit "$DMG_PATH" "${AUTH[@]}" --output-format json)
SUBMISSION_ID=$(printf '%s' "$SUBMIT_JSON" | parse_json id)
if [[ -z "$SUBMISSION_ID" ]]; then
  echo "Submit did not return an id. Raw output:" >&2
  printf '%s\n' "$SUBMIT_JSON" >&2
  exit 1
fi
echo "    Submission ID: $SUBMISSION_ID"

DEADLINE=$(( $(date +%s) + TIMEOUT_SECONDS ))
STATUS="In Progress"
while [[ "$STATUS" == "In Progress" ]]; do
  if (( $(date +%s) > DEADLINE )); then
    echo "Timed out after ${TIMEOUT_SECONDS}s waiting for Apple." >&2
    xcrun notarytool log "$SUBMISSION_ID" "${AUTH[@]}" notarization.log || true
    exit 1
  fi
  sleep "$POLL_SECONDS"
  INFO_JSON=$(xcrun notarytool info "$SUBMISSION_ID" "${AUTH[@]}" --output-format json)
  STATUS=$(printf '%s' "$INFO_JSON" | parse_json status)
  echo "    Status: ${STATUS:-<empty>}"
done

# Always fetch the log, even on success — Apple notes it can carry warnings.
xcrun notarytool log "$SUBMISSION_ID" "${AUTH[@]}" notarization.log || true

if [[ "$STATUS" != "Accepted" ]]; then
  echo "Notarization finished with status '$STATUS'. See notarization.log." >&2
  exit 1
fi

echo "==> Stapling ticket to DMG"
xcrun stapler staple "$DMG_PATH"
xcrun stapler validate "$DMG_PATH"

echo "==> Gatekeeper assessment"
spctl --assess --type open --context context:primary-signature --verbose "$DMG_PATH"

echo "OK: $DMG_PATH is notarized, stapled and Gatekeeper-accepted."
```

A failed poll is never counted as a successful build. Apple's workflow page states that most submissions finish in under 5 minutes and 98% within 15 minutes, and recommends a daily limit of 75 notarizations. That is comfortable for pre-merge CI, but worth remembering if a release branch retries in a loop.

## How do I read the notarization log when the status is Invalid?

`notarytool log` writes JSON in which each finding carries `severity`, `path` and `message`. Common findings and their fixes:

| Rejection message | Fix |
|---|---|
| The signature of the binary is invalid | Re-sign the binary with Developer ID Application, hardened runtime and timestamp |
| The binary uses an SDK older than the 10.9 SDK | Rebuild linking against the macOS 10.9 SDK or later |
| The executable does not have the hardened runtime enabled | Add `--options runtime` to every `codesign` call |
| The signature does not include a secure timestamp | Add `--timestamp` and confirm the builder can reach Apple's timestamp server |
| `com.apple.security.get-task-allow` is set to true | Remove that entitlement from release builds |
| Binary is not signed with a valid Developer ID certificate | Confirm the identity type is Developer ID Application (not Apple Development or Developer ID Installer) |

Each resubmission gets a new submission ID, and older logs stay retrievable by their own ID. Apple's "Resolving common notarization issues" page is linked from the notarization overview above.

## Which Figment Mac plan fits notarization?

Notarization is a few HTTPS calls and a short staple, so a small host is enough when it is the only step. The useful question is whether archive, DMG build and notarize share one Mac. Time three recent runs on your current host and fill in your own numbers, not someone else's benchmark.

| Step | Your measured minutes |
|---|---|
| `xcodebuild archive` + inside-out codesign (A) | ___ |
| `hdiutil create` + `codesign` of the DMG (B) | ___ |
| `notarytool submit` upload (C) | ___ |
| `notarytool info` poll, Apple-side (D) | ___ |
| `stapler staple` + `spctl` assess (E) | ___ |
| Total | A + B + C + D + E |

Apply this rule to your measurements:

- If **A** dominates and the project needs current Xcode (Xcode 26.4 or later needs Tahoe 26.2 or later, Xcode 27 needs Tahoe 26.6 or later) or arm64-only execution during tests, use an [Apple Silicon Mac VM](/mac-hosting#apple-silicon-vm) ($39 a month, $6 a day), or the [Dedicated M4 Pro 48 GB](/mac-hosting#dedicated-m4-pro) ($349 a month) for heavy archives.
- If **A + B** are small and **C + D + E** are most of the wall clock, and Xcode 26.3 or earlier is enough, the [Intel Mac VM](/mac-hosting#intel-vm) ($25 a month, $2 a day) can run `xcrun notarytool` and `xcrun stapler`. **D** is Apple-side and does not change when you move hosts.

Check the macOS version shown before purchase against Apple's Xcode table. Figment runs the Mac; you manage your signing identities, API key and CI.

**Related guides**
- [Self-Hosted Mac Runner for CircleCI Pipelines](/blog/mac-circleci-self-hosted-runner)
- [Register a GitLab CI Runner on a Hosted Mac](/blog/mac-gitlab-ci-runner-macos)
- [Xcode on Windows: Remote Mac Setup](/blog/mac-xcode-on-windows-remote-setup)
