Liero
Liero

Reputation: 27348

How to cache nuget packages for all projects in Azure Pipelines using Cache task

I'm using following step to perform solution level caching:

- task: Cache@2
  inputs:
    key: 'nuget | "$(Agent.OS)" | $(Build.SourcesDirectory)/**/packages.lock.json'
    restoreKeys: |
       nuget | "$(Agent.OS)"
       nuget
    path: $(NUGET_PACKAGES)
  displayName: Cache NuGet packages    

- task: DotNetCoreCLI@2
  displayName: 'dotnet restore using package.lock.json'
  inputs:
    command: 'restore'
    restoreArguments: '--locked-mode'
    feedsToUse: 'config'
    nugetConfigPath: 'nuget.config'

However, in the Post-job: Cache NuGet packages there's warning:

##[warning]The given cache key has changed in its resolved value between restore and save steps

I've realized it's because also lock files copied to output direcotries */bin/release/net5.0/packages.lock.json files are picked when resolving key.

How to I fix this?

Upvotes: 5

Views: 4090

Answers (1)

Liero
Liero

Reputation: 27348

Fixed by using following pattern in key:

key: 'nuget | "$(Agent.OS)" | **/packages.lock.json,!**/bin/**,!**/obj/**'

Upvotes: 10

Related Questions