What this guide covers
After reading, you can set cache rules for your own pipeline: everyday builds reuse, release builds are clean, and the disk does not fill up. You need: a remote Mac already connected to CI.
Steps
First understand the basic difference from cloud runners
One sentence in GitHub's self-hosted runners overview captures the difference: a self-hosted runner does not need to have a clean instance for every job execution. The derived data, dependencies and tools left by the previous build are still on disk.
So on a self-hosted machine, a "cache" is not first of all a feature to configure. It is the default state. Your job is to decide what stays and what gets cleared.
Give each kind of cache a definite location
Do not leave them scattered in default locations. Pin them with options, which makes cleanup and troubleshooting easier:
- Derived data:
xcodebuild's-derivedDataPath <directory>. - Swift package checkouts:
-clonedSourcePackagesDirPath <directory>. - CocoaPods: the
Podsdirectory in the project stays with the workspace, and the download cache is managed bypod cache.
We suggest putting them all under one parent directory in your own user directory, for example
~/ci-cache/<project name>/.- Derived data:
Reuse for everyday builds, a clean directory for release builds
Merge request and nightly builds are after speed, so reuse derived data for incremental compilation. Builds headed for the App Store are after reproducibility: give them a brand-new derived data directory, or empty it at the start of the job, so the product does not depend on the machine's history.
Dependency checkouts can be shared by both kinds of build as long as the versions are locked with an option such as
-onlyUsePackageVersionsFromResolvedFile.Parallel jobs must not share one directory
When several runners or several executors are registered on one machine, two jobs writing the same derived data at once corrupt each other. It shows up as occasional failures that pass on a rerun.
Either give each runner its own cache directory (with the runner name in the path), or have this machine run only one build job at a time. The latter is simpler, and for a single Mac mini it is often the more practical choice too.
Do you still need the CI platform's built-in cache feature
Take GitHub's dependency caching as an example:
actions/cacherestores a cache by key, saves a new one after the job succeeds when there was no hit, and is subject to the repository's cache size limit and to upload and download rate limits.That mechanism is designed for a fresh environment every time. On a self-hosted machine whose disk persists anyway, packing up the same files, uploading them and downloading them next time is usually a redundant step. It only makes sense when the same job moves between several machines.
The order to follow when a mystery failure appears
Rerun once first to rule out a network blip. If it still fails, clear this project's derived data and run again. If that does not help, clear the dependency checkouts and resolve again. Note at each step whether it worked.
If "a clean fixes it" keeps recurring, some build step is not idempotent and should be fixed. Do not make a full clean part of every run; that gives away the speed advantage of a self-hosted machine.
Cache, disk and the rental term
A cache only grows, so have a scheduled cleanup job that deletes old projects' directories by last use. A disk close to full causes problems for builds, the system and even the remote connection. See the plans page for disk configurations.
Also remember that this machine is rented: when the rental ends the whole machine is reclaimed and the cache is gone. Anything that cannot be regenerated, such as symbol files for release versions and test reports, has to be uploaded to your own storage within the job and not left only in a cache directory.
FAQ
The first build after clearing the cache is very slow. Is that normal?
Yes, it amounts to a full cold build. Incremental builds after that recover. How much faster depends on the project; we have no measured figures to offer.
Can I put the cache directory on an external drive?
Any external storage arrangement has to be confirmed with support first. Even if it is possible, derived data is read and written constantly and is usually better kept on the internal disk.
Should I use a third-party remote build cache?
That is a different kind of solution; go by the official documentation of the tool in question for whether it suits your project. Getting the locations and cleanup rules of the local cache in order often solves most of the problem already.
Does restarting the machine clear the cache?
No. The cache is ordinary directories on disk. A restart only ends running jobs and services; afterwards, confirm the build agent is back online.
Related
- Rented Mac mini as an always-on build machine for small teams? Count your build minutes first
- Where does Xcode cache Swift packages, and how do I pin and clear the cache?
- Xcode filled up my disk: how to clear DerivedData and what else is safe to delete
- How to run a GitHub Actions self-hosted runner as a service on a remote Mac
- Remote Mac vs GitHub Actions macOS runner or Codemagic: which should I use?
Pricing and availability: see the plans page · Help center (中文) · Refund policy (中文) · Telegram Contact support