How to Cache Swift Package Manager in GitHub Actions
Cache the SwiftPM build directory keyed on Package.resolved so resolved dependencies restore instead of cloning every run.
Cache .build (for swift build) or the Xcode SwiftPM cache keyed on Package.resolved. With dependencies restored, resolution becomes a quick verification step.
Steps
- Commit
Package.resolvedso the key is deterministic. - Cache
.buildfor aswift buildworkflow, keyed onPackage.resolved. - For Xcode projects, also cache
~/Library/Caches/org.swift.swiftpm.
Workflow
.github/workflows/ci.yml
steps:
- uses: actions/checkout@v4
- uses: actions/cache@v4
with:
path: |
.build
~/Library/Caches/org.swift.swiftpm
key: spm-${{ runner.os }}-${{ hashFiles('Package.resolved') }}
restore-keys: spm-${{ runner.os }}-
- run: swift build -c releaseGotchas
- Cache resolved packages, not compiled
.ofiles from a different toolchain; key on the Swift/Xcode version too if you switch toolchains. - A stale
Package.resolvedthat no longer matchesPackage.swiftcauses resolution to update and miss the cache.
Related guides
How to Cache DerivedData in GitHub ActionsCache Xcode DerivedData on a GitHub Actions macOS runner with a fixed -derivedDataPath and actions/cache to r…
How to Cache CocoaPods in GitHub ActionsCache the CocoaPods Pods directory and spec repo on a GitHub Actions macOS runner with actions/cache keyed on…