Skip to content
Latchkey

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.resolved so the key is deterministic.
  • Cache .build for a swift build workflow, keyed on Package.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 release

Gotchas

  • Cache resolved packages, not compiled .o files from a different toolchain; key on the Swift/Xcode version too if you switch toolchains.
  • A stale Package.resolved that no longer matches Package.swift causes resolution to update and miss the cache.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →