1

I'm getting this error every time I run a build through my azure devops pipeline:

The given cache key has changed in its resolved value between restore and save steps

I am trying to cache the cocoapods used in my react native project. I am using the hash of the Podfile.lock in the cache key string (if there are changes in the Podfile.lock then the cache key will change and a new cache will be uploaded after a cache miss)

locally if I delete the Podfile.lock and ios/Pods folder and then run a pod install nothing changes. When the build pipeline runs pod install the Podfile.lock hash does change which means that the cache can never be retrieved because the key is always being modified.

I have tried:

  • changing vmImage to 10.14 instead of macOS-latest
  • made sure that locally all pods are up to date
  • made sure that cocoapods versions are the same locally and in the build pipeline

I can't think of what else to try, there are no examples for cocoapods caching in the microsoft docs and no one seems to have run into this same problem

pivey
  • 21
  • 5

2 Answers2

2

You may try the following approach:

# Podfile.lock and project.pbxproj are changed during the build process, so we make copies
- script: |
   cp ./ios/Podfile.lock ./ios/podfile_key
   cp ./ios/AwesomeProject.xcodeproj/project.pbxproj ./ios/project_key
  displayName: Create pod cache keys

- task: Cache@2
  inputs:
    key: 'pods | "$(Agent.OS)" | ./ios/podfile_key | ./ios/Podfile | ./ios/AwesomeProject.xcworkspace/contents.xcworkspacedata | ./ios/project_key'
    path: './ios/Pods'
    cacheHitVar: 'PODS_CACHE_RESTORED'
  displayName: Cache pods

- task: CocoaPods@0
  displayName: 'pod install using the CocoaPods task with defaults'
  condition: ne(variables.PODS_CACHE_RESTORED, 'true')
  inputs:
   workingDirectory: './ios/'
   forceRepoUpdate: false
aleksander_si
  • 1,021
  • 10
  • 28
  • Thanks aleksander_si thanks for posting on this issue. One thing that I'm not understanding is the /podfile_key and /project_key. Where are these coming from? when do you create these files? – pivey Jan 12 '22 at 18:12
  • Those two files are created by a script just before the Cache task, see example. In my case I wanted to invalidate the cache also if workspace and project files change, so I added the relevant files to the cache key signature. – aleksander_si Jan 13 '22 at 08:53
0

The error indicates you need to make sure none of your build steps between the Cache and Post-Job Cache step change the cache key value. You may either remove the change causing step or remove Podfile.lock from the key.

Cece Dong - MSFT
  • 29,631
  • 1
  • 24
  • 39
  • Wouldn't removing Podfile.lock from the key completely remove the purpose of the cache lookup/generation? Isn't the whole idea that the cache key gets generated from the Podfile.lock hash, and if the Podfile.lock is changed (because the pods have changed), then the next run would render a cache miss? If you just specify the key to be e.g. "foobar", wouldn't that always mean that there would be a cache hit on the pods (even if they changed)? Also, what's strange is that the npm caching tasks work flawlessly, so there seems to be something fishy with the pods specifically. – Emil Feb 08 '21 at 10:40
  • How's your pipeline like? Please check this blog to see whether it helps you: https://medium.com/@zmoola22/caching-cocoapods-on-azure-devops-pipeline-3b1ed918b60e. – Cece Dong - MSFT Feb 09 '21 at 09:11
  • @CeceDong-MSFT I have followed that blog post to the tee and am still encountering this issue. For some reason the pod install step (necessary and cannot be removed) is causing the cache key to be modified which means that the cache is not being hit on the subsequent run of the pipeline. – pivey Feb 09 '21 at 11:06
  • you suggest removing the Podfile.lock from the cache key, is there any other file you would suggest including in place of it? we need to include a hash of a file that changes when the projects pods have changed which would lead to a cache miss (file hash would then be different from stored cache key) and cause a new cache to be uploaded and stored – pivey Feb 09 '21 at 11:08
  • Unfortunately, I don't see there is other files to be cached. – Cece Dong - MSFT Feb 10 '21 at 10:06