SPM cache not working on github actions, any ideas?

Viewed 1067

I am trying to cache the SPM packages on GitHub Actions with cache action, I am following this example:

  - uses: actions/cache@v2
  with:
    path: Myproject.xcworkspace/xcshareddata/swiftpm/Package.resolved
    key: ${{ runner.os }}-spm-${{ hashFiles('**/Package.resolved') }}
    restore-keys: |
      ${{ runner.os }}-spm-

I feel like is not possible to use cache, when you add your SPM packages with Xcode

Did anyone have succeed adding cache to your project even managing SPM via Xcode? Or maybe something is wrong in my .yml file but unfortunately I could not make it work.

2 Answers

You are using the path parameter incorrectly.

path - A list of files, directories, and wildcard patterns to cache and restore. See @actions/glob for supported patterns.

Instead of setting path to the resolve file it should point to whatever file/folder that you wish to cache.

The documentation for actions/cache actually shows exactly how to use it for SPM:

- uses: actions/cache@v2
  with:
    path: .build
    key: ${{ runner.os }}-spm-${{ hashFiles('**/Package.resolved') }}
    restore-keys: |
      ${{ runner.os }}-spm-

Since you are letting Xcode manage the Swift packages the files end up being stored in a different location than they would be if you manually managed them using swift package.

This variation should find the files (but Xcode could change where it stores them at any time):

- uses: actions/cache@v2
  with:
    path: /Users/runner/Library/Developer/Xcode/DerivedData/**/SourcePackages/checkouts 
    key: ${{ runner.os }}-spm-${{ hashFiles('**/Package.resolved') }}
    restore-keys: |
      ${{ runner.os }}-spm-

Since you are using CI + SPM I would recommend you stop managing SPM via Xcode and instead manually use swift package. This will allow you to have a more predictable location (.build) for the SPM packages.

The second variation don't know why It didn't work for me, thanks anyway for your help .

I gave it a last shot, and this worked!

- uses: actions/cache@v3
        name: "Cache: SPM"
        with:
          path: ~/Library/Developer/Xcode/DerivedData/AppName*/SourcePackages/
          key: ${{ runner.os }}-spm-${{ hashFiles('AppName.xcworkspace/xcshareddata/swiftpm/Package.resolved') }}
          restore-keys: |
            ${{ runner.os }}-spm-
Related