Why are my Azure DevOps Pipeline cache hits missing

I have blogged in the past about Caching NVD Vulnerability Dependency data on hosted Azure DevOps Pipeline agents. Using the cache is a great way to speed up slow builds.

However, today I was surprised to find I was getting cache misses on my pipeline, even though I was sure the cache should have been hit.

There are rules over how the cache is used:

  • The cache is specific to a pipeline definition, so there is no sharing of the cache between pipeline definitions
  • The cache is only created if the pipeline is successful (running the post run tasks)
  • The cache only lasts 7 days
  • but what I had not realised was the cache is also specific to the branch in a not so obvious way.

My pipeline was triggered off a PR, so the cache was being created on the ‘branch’ PR #123. This was working as expected, all runs of the PR triggered build used the cache after the initial run. However, if I manually triggered pipeline run of the same branch as the PR was using, there was a cache miss.

As far as the Azure DevOps cache task is concerned, the branch used for PR #123 and a manual run off the underlying branch used in the PR are different things, so the cache created by the PR was not being used by the manual branch build.

That explains a few slow builds I have had.

For the original version of this post see Richard Fennell's personal blog at Why are my Azure DevOps Pipeline cache hits missing