Faster iOS builds on CI: what to cache and what never to cache
Caching can shorten iOS pipelines, but a stale DerivedData folder or a mismatched toolchain can make builds slower or wrong. This analysis separates safe, risky and harmful cache targets, and explains why measuring comes before any optimisation.

Every iOS team that moves builds to CI eventually asks the same question: why does a pipeline that takes moments on a developer laptop take so long on a clean machine? The usual answer is that the laptop is carrying state, such as compiled modules, resolved packages and indexes, that the CI job has to rebuild each time. Caching can return some of that state, but it is also a common source of builds that pass for the wrong reason. This analysis sorts the common cache targets by how safe they are to reuse.
Know what each folder holds
Xcode keeps most intermediate output in DerivedData: compiled objects, module caches, build products, logs and index data. By default it lives under ~/Library/Developer/Xcode/DerivedData. You can move it per invocation with the -derivedDataPath option of xcodebuild, which makes the location explicit and easy to size, archive or delete.
Swift Package Manager dependencies are a separate concern. When a project uses remote packages through Xcode, the checked-out sources can be placed in a folder of your choosing with -clonedSourcePackagesDirPath. The versions chosen are recorded in Package.resolved. For an app project this file is typically kept inside the project or workspace bundle, under xcshareddata/swiftpm, so check where it sits in your own repository and commit it. SwiftPM also keeps its own user-level caches on the Mac. The exact location differs between toolchain releases, so check your installed version rather than copying a path from an old guide.
CocoaPods and Carthage follow their own conventions. CocoaPods records exact versions in Podfile.lock, and its guide says the Podfile and Podfile.lock belong in version control. Whether to also commit the Pods directory is left to the team: the guide leans towards committing it but describes both choices. Carthage records versions in Cartfile.resolved and writes built frameworks to Carthage/Build; its documentation recommends --use-xcframeworks and notes that this option is required on Apple Silicon Macs.
A rough safety ranking
| Target | Reuse risk | Reasoning |
|---|---|---|
| Downloaded package sources and pod sources | Low | Keyed to a lock file, so content is reproducible. |
| Carthage prebuilt binaries | Medium | Valid only for the Swift version that built them. |
| Whole DerivedData | High | Tied to the Xcode build, absolute paths and project state. |
| Signing keys, tokens, profiles | Never | Secrets do not belong in a shared cache. |
The ratings are editorial judgement, not measurements. Your own project may justify a different order.
Invalidation hazards
Toolchain changes. Compiled output depends on the compiler that produced it. If the Xcode build changes, treat every compiled artefact as stale. The simplest protection is to put the output of xcodebuild -version (which includes the build number) into the cache key. Carthage applies the same idea: it compares the Swift version of a downloaded binary with the local toolchain and builds from source when they differ.
Absolute paths. DerivedData is generally tied to the location of the checkout that produced it. A cache restored to a different checkout location, or a different user account, can plausibly lead to confusing rebuilds or odd errors. Keep the workspace path identical across jobs if you restore build products at all, and verify the effect on your own project.
Paths that collide. A Swift Forums thread about Xcode 13.4 describes package resolution failing after -derivedDataPath pointed inside the working directory. The suggested workaround was to also set -clonedSourcePackagesDirPath elsewhere, and a later reply said a hidden folder such as .derivedData avoided the problem. This is a report about one Xcode version from individual users, so test your own version before relying on either remedy.
Lock files. Hash Package.resolved, Podfile.lock or Cartfile.resolved into the key. When a dependency changes, the key changes and a new cache is built.
Persistent Mac versus ephemeral runner
On a persistent Mac, caching mostly happens by default: folders survive between jobs. That is fast, but state accumulates, and one job can leave behind something that changes the next. Give each project its own explicit DerivedData path, clean it on a schedule, and keep an occasional fully clean build to prove the project still builds from nothing.
An ephemeral runner starts empty, so you restore what you chose and nothing else. GitHub's caching documentation describes the mechanics: an exact key match is tried first, then restore-keys prefixes in order, and the newest matching cache wins. Entries are immutable, so a changed cache needs a new key. Unused caches are evicted after seven days, repositories have a default size limit, and pull request caches are scoped to the merge ref. The same page warns against storing secrets in cached files, since people who can open pull requests may reach caches on the base branch. Confirm current limits on that page before designing around them.
Clean versus incremental
Incremental builds are fast but rely on the build system's change tracking being right. Clean builds are slower and give a trustworthy baseline. A common compromise is incremental builds for pull request checks and clean builds for release candidates, so shipped binaries never inherit stale intermediates. Whatever you choose, make it a documented policy rather than an accident of which runner picked up the job.
If dependency resolution is a cost, the -disableAutomaticPackageResolution option of xcodebuild is meant to stop Xcode from resolving packages automatically. Run xcodebuild -help on your Xcode version to see how this flag behaves before depending on it.
Measure before optimising
Start with three timings on the same machine: a clean build, a no-change incremental build and a build after a small source change. Record where the time goes: dependency fetch, compilation, linking, tests and signing. Often the largest cost is not what you assumed. Change one thing at a time, compare against the baseline, and discard any cache that does not earn its complexity. Keep a regular uncached run so that cache problems show up as a difference you can see.


