What this guide covers
When you finish, build jobs no longer re-clone dependencies every time, and the resolved versions match what the repository records. You need: Xcode installed and a project that uses Swift packages.
Steps
Commit Package.resolved to the repository first
Apple's package dependencies documentation explains that when you add a package dependency, Xcode generates
Package.resolved, which records the exact commit each dependency resolved to, and that you should commit it to Git so everyone uses the same versions. For a plain Xcode project it sits under.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/.Without this file there is no point talking about caching: every machine may resolve to different versions.
Check packages out to a fixed directory
xcodebuildprovides-clonedSourcePackagesDirPathto say where remote packages are checked out. Give it a fixed path that build cleanup does not touch:xcodebuild -resolvePackageDependencies -project App.xcodeproj -clonedSourcePackagesDirPath "$HOME/ci-cache/spm"Pass the same option to the build commands that follow. That way the package checkouts survive even when derived data is cleared.
Resolve only to the recorded versions
On CI you do not want dependencies upgraded quietly. According to
xcodebuild -help, both of the options below prevent automatic resolution to versions other than those inPackage.resolved:-disableAutomaticPackageResolution -onlyUsePackageVersionsFromResolvedFileThere is also
-skipPackageUpdates, which skips updating dependencies from the remote. Leave dependency upgrades to developers, who do them locally and commitPackage.resolved.What each cleanup command clears
For a standalone Swift package directory, the
swift packagehelp states:swift package clean: deletes build artifacts.swift package reset: resets the complete cache and build directory.swift package purge-cache: purges the global repository cache.
In Xcode, Apple's documentation mentions that resolving and updating can be triggered manually from the File > Packages menu; go by your own Xcode for the names of the items under that menu. When the resolution result is clearly wrong, clearing and then resolving again works better than retrying over and over.
Access credentials for private packages
When dependencies include private repositories, the build machine needs permission to fetch them. Issue a read-only deploy key or token just for this machine, scoped only to the repositories it needs. Revoke it on your code hosting platform before the rental ends. Do not put a personal account's full-permission credentials on a build machine.
Do not switch off package plug-in validation casually
xcodebuildhas a-skipPackagePluginValidationoption, and its own help text notes that skipping validation is a security risk if the plug-ins come from an untrusted source. On a build machine that stays on for long periods and holds signing certificates, do not add it just to get a build through unless you know where every plug-in comes from.On a rented build machine, the cache is a speed-up, not a backup
This machine stays on and its disk contents persist between jobs, so a cache in a fixed directory stays valid. That is a practical advantage of a self-hosted build machine over a one-off cloud environment.
But the cache exists only on this machine. When the rental ends the machine is reclaimed and the cache goes with it, and the first build on a new machine fetches everything in full. Nothing that cannot be downloaded again should live only in the cache directory. Include the cache directory in regular cleanup too, so it does not fill the disk.
FAQ
Why are packages fetched again after I clear DerivedData?
When no separate directory is specified, Xcode keeps the checked-out packages together with derived data, so they are cleared along with it. Use -clonedSourcePackagesDirPath to keep them apart.
Dependency resolution keeps hanging. What should I check?
First confirm this machine can reach the code hosting platform where the dependencies live, then check whether a private package is missing credentials. Fetch speed depends on the line and the load.
If two jobs build at once, will they fight over the same cache directory?
That is a risk. When several jobs run in parallel on one machine, give each runner its own checkout directory, or have build jobs run one after another.
Do I need to back up the cache directory elsewhere?
No. As long as Package.resolved is in the repository, any machine can obtain the same dependencies again.
Related
- Rented Mac mini as an always-on build machine for small teams? Count your build minutes first
- Self-hosted Mac CI build cache: what to keep and what to clear every time
- Xcode filled up my disk: how to clear DerivedData and what else is safe to delete
- How to install CocoaPods on a remote Mac and get pod install working
- How to archive and export an ipa from the command line with xcodebuild
Pricing and availability: see the plans page · Help center (中文) · Refund policy (中文) · Telegram Contact support