# Rotate Apple Certs in CI Without Build Downtime

Canonical: https://figment.so/blog/mac-certificate-rotation-ci-zero-downtime

Generate the replacement distribution certificate before the old one expires, drain in-flight CI, run fastlane match nuke then match appstore, and verify with a signed build and a TestFlight upload.

Generate the replacement Apple Distribution certificate before the old one expires, drain in-flight CI builds, run `fastlane match nuke distribution` then `fastlane match appstore`, and verify with a signed archive and a TestFlight upload. An expired Apple Distribution certificate stops App Store Connect uploads the moment it lapses, so rotate during business hours and keep an encrypted rollback asset in case the new certificate fails verification. Commands come from the primary docs at [Apple Developer certificates](https://developer.apple.com/support/certificates/) and [Fastlane match](https://docs.fastlane.tools/actions/match/).

## What do Apple's certificate rules mean for the rotation?

Apple's certificates page is explicit about the constraints you have to design around:

- **Apple Distribution belongs to the team**, not a user. Only one type of each distribution certificate is allowed per team at a time. Developer ID is the exception and can have several.
- When an Apple Distribution certificate expires, apps already on the App Store keep working, but you cannot upload new builds signed with the expired cert.
- Developer ID Application: previously signed apps continue to run; you cannot sign new versions until a new certificate is issued. A revoked (not merely expired) Developer ID cert blocks installs.
- Only the Account Holder or Admin role can create distribution certificates.

Because only one Apple Distribution certificate is active per team, you cannot run two overlapping App Store distribution certs in parallel the way you might with Developer ID. The "overlap window" here is a time overlap: generate the replacement before the old one expires, not two simultaneously trusted certificates.

## When should I start, and how do I get a warning?

Put a calendar alert 30 days before expiry and use the time to:

1. Pick a low-traffic rotation slot (Tuesday morning beats Friday afternoon).
2. Confirm every CI runner can reach the Match repository.
3. Freeze release-branch merges for the rotation window.
4. Export an encrypted `.p12` of the current certificate and private key for rollback.

Automate the alert with a weekly check that fails CI when the cert has 30 days or less left:

```bash
#!/usr/bin/env bash
# cert-expiry-check.sh: run weekly on a macOS runner
set -euo pipefail

: "${CERT_P12_PATH:?set CERT_P12_PATH}"
: "${CERT_P12_PASSWORD:?set CERT_P12_PASSWORD via CI secret}"

EXPIRY=$(openssl pkcs12 -in "$CERT_P12_PATH" -nokeys -passin "pass:${CERT_P12_PASSWORD}" \
  | openssl x509 -noout -enddate | cut -d= -f2)

EXPIRY_EPOCH=$(date -j -f "%b %e %T %Y %Z" "$EXPIRY" +%s)
DAYS_LEFT=$(( (EXPIRY_EPOCH - $(date +%s)) / 86400 ))

printf 'Distribution cert expires: %s (%d days)\n' "$EXPIRY" "$DAYS_LEFT"
[ "$DAYS_LEFT" -gt 30 ] || { echo "Rotate now: $DAYS_LEFT days left" >&2; exit 1; }
```

Read `CERT_P12_PASSWORD` from the CI secret manager and never hard-code it. This is an untested example; run it against your own encrypted `.p12` on a macOS runner and confirm the output before scheduling.

## How do I rotate the certificate with Fastlane match?

Per the [Fastlane match docs](https://docs.fastlane.tools/actions/match/), `match nuke` "revokes your certificates and provisioning profiles" for the chosen environment, and `readonly` mode "only fetch[es] existing certificates and profiles, don't generate new ones". Fastlane recommends readonly on CI. `--force` renews provisioning profiles every run; it does not by itself revoke a certificate.

The clean, repeatable rotation on a macOS workstation (not on CI):

```bash
# 1. Revoke the current distribution cert and its App Store profiles
fastlane match nuke distribution   # confirms interactively; review the list before approving

# 2. Regenerate a fresh distribution cert plus App Store profiles and commit to the Match repo
fastlane match appstore

# 3. Optional: refresh development material at the same time
fastlane match development
```

Match writes the regenerated assets to its Git repository in a single commit. CI runners pick up the new cert on their next `match ... readonly` fetch. `match nuke distribution` also invalidates existing App Store provisioning profiles tied to the revoked cert; `match appstore` regenerates them. Do not run `nuke` on an Enterprise (in-house) program casually, because revoking that distribution cert blocks previously installed in-house apps from launching.

## What is the step-by-step runbook?

Run in order. Do not skip step 4.

| # | Step | Command or check |
|---|------|------------------|
| 1 | Pre-flight: Match reachability | `fastlane match appstore --readonly` from one of each runner type |
| 2 | Pre-flight: freeze releases | Hold merges to the release branch; announce the window |
| 3 | Rollback asset | Export current cert + private key as an encrypted `.p12`; store in the secret manager, not in Git |
| 4 | Drain CI | Wait for all in-flight builds to finish; pause new jobs on release lanes |
| 5 | Rotate | `fastlane match nuke distribution` then `fastlane match appstore` on an admin macOS machine |
| 6 | Verify build | Trigger a signed Release archive on a non-production branch |
| 7 | Verify upload | `fastlane pilot upload` to a TestFlight internal group |
| 8 | Resume | Unfreeze releases |
| 9 | Reset alert | Update the 30-day calendar entry and the weekly script baseline |

**Rollback path.** The window is narrow: once `match nuke` revokes the old cert, previously signed in-flight builds that have not been uploaded may be marked Invalid Binary. If the new cert fails step 6 before you nuke, abort, because nothing has changed on Apple's side. If it fails after step 5, re-import the encrypted `.p12` from step 3 on an admin machine, upload the preserved public certificate back to the Apple Developer portal only if Apple still accepts it, then re-run `fastlane match appstore` pointed at the restored material. If Apple will not reinstate the revoked certificate, your fastest recovery is finishing the new-cert path: re-run `match appstore`, re-sign the pending build, and push.

## How do I keep the match password out of shell history?

Store the Match encryption password in a CI secret named `MATCH_PASSWORD`; Fastlane reads it from the environment. Use readonly on every CI job:

```ruby
# Fastfile: CI-side lane; interactive rotation is a separate admin lane
lane :sync_signing do
  match(type: "appstore", readonly: is_ci)
end
```

Do not echo the password, do not write a plaintext dump of the full environment to disk, and keep the signing job restricted to trusted branches. If you need to move a secret between systems, transfer the name list and restore values through the secret manager.

## Can the Intel Mac VM host the certificate expiry check?

The rotation needs a Mac with the match repository cloned, Fastlane installed and the right Xcode for your target. The [Intel Mac VM](/mac-hosting#intel-vm) at $25 a month runs macOS Sequoia, where Apple lists Xcode 26.0 to 26.3. It has Keychain, `security` and `codesign`, so it can host the weekly expiry check and readonly `match` fetches. Xcode 26.4 and later need macOS Tahoe 26.2 or later, and Xcode 27 needs Tahoe 26.6 or later, so those need the [Apple Silicon Mac VM](/mac-hosting#apple-silicon-vm) at $39 a month or the [Dedicated M4 Pro 48 GB](/mac-hosting#dedicated-m4-pro) at $349 a month. Apple's porting guide says to [use Xcode 26 or earlier on Intel](https://developer.apple.com/documentation/apple-silicon/porting-your-macos-apps-to-apple-silicon).

For a rotation month with one admin Mac and one CI runner, the arithmetic is:

| Setup | Cost per month |
|---|---|
| Two Intel Mac VMs (admin plus CI runner), Xcode 26.0 to 26.3 | 2 × $25 = $50 |
| One Apple Silicon Mac VM for admin and CI | $39 |
| One Dedicated M4 Pro 48 GB for admin and CI | $349 |

Figment runs the Mac and network. You manage match, the secret manager and the rotation alert. Prices are USD before tax; a daily plan ($2 on the Intel VM or $6 on the Apple Silicon VM) covers a one-off rotation without a monthly commitment. See [Mac hosting](/mac-hosting) for plan details.

---

*Related guides:*
- [Essential Xcodebuild CLI Flags for Faster CI](/blog/mac-xcodebuild-cli-flags-ci)
- [Manage Secrets and Env Vars in iOS CI Pipelines](/blog/mac-ci-env-var-secrets-ios)
- [Xcode on Windows: Remote Mac Setup](/blog/mac-xcode-on-windows-remote-setup)
