Automation

Azure DevOps: validate a pipeline cache before production promotion

A production runbook for diagnosing exact and partial Azure DevOps cache restores, proving dependency integrity, and choosing promotion, clean rebuild or rollback.

15 Oct 2026 azure-devopspipelinescachedependencieslockfileartifactssupply-chaindevopsautomationobservabilityrunbookrollbackproduction

A dependency lockfile changes, the Azure DevOps pipeline stays green, and the deployment candidate is ready unusually quickly. The cache task reports a restore, tests pass, but the generated artifact contains a dependency version or native binary that does not match the new lockfile. Rerunning may return the same cache and produce the same ambiguity.

The running case is a Node.js build using Cache@2, but the decision applies to NuGet, Maven, Gradle, Yarn and other dependency caches. The objective is to prove whether the run received an exact cache hit, a fallback entry selected through restoreKeys, or no cache at all, then decide whether the artifact can be promoted, must be rebuilt without cache, or requires a rollback of the cache policy.

Freeze the candidate before clearing anything

Pause promotion and preserve one affected run plus the last known-good run. A cache is acceleration state, but it influences the build inputs observed by the package manager. Record enough context to reproduce that influence without copying credentials or package contents into the incident record.

yaml 01-cache-incident-contract.yml
candidate:
pipeline: build-orders-api
run_id: <run-id>
source_commit: <full-sha>
pipeline_revision: <yaml-commit-or-definition-revision>
agent_os: <os-and-architecture>
runtime: node-<version>-npm-<version>
lockfile_sha256: <sha256>
cache_state: true | inexact | false
artifact_sha256: <sha256>

change:
lockfile_changed: true
cache_task_changed: false
restore_keys_changed: false

decision:
production_promotion_paused: true
last_known_good_artifact: <run-id-and-digest>
owner: platform-delivery

Do not delete the cache or rebuild over the same staging directory yet. Preserve task logs, the evaluated key, the cache state variable, the lockfile and the artifact manifest. The investigation needs identities and hashes, not an archive of every dependency.

Read the cache result as three states

With Cache@2, a cache state variable is true for an exact key match, inexact when a restoreKeys prefix returns a fallback entry, and false on a miss. Treating every non-empty value as a full hit is the dangerous shortcut: an inexact result can contain dependencies associated with an older lockfile.

yaml 02-cache-state.yml
variables:
npm_config_cache: $(Pipeline.Workspace)/.npm

steps:
- task: Cache@2
displayName: Restore npm download cache
inputs:
  key: '"npm" | "$(Agent.OS)" | package-lock.json'
  restoreKeys: |
    "npm" | "$(Agent.OS)"
  path: $(npm_config_cache)
  cacheHitVar: NPM_CACHE_STATE

- bash: |
  set -euo pipefail
  printf 'cache_state=%s
' "$(NPM_CACHE_STATE)"
  sha256sum package-lock.json
  npm ci --prefer-offline
displayName: Restore locked dependencies

The cache stores downloaded packages, not the authoritative dependency graph. npm ci still reconciles the workspace with package-lock.json; the cache only reduces network work. For other ecosystems, use the equivalent locked restore and keep its verification active on exact, partial and missed restores.

If the pipeline skips dependency restoration when NPM_CACHE_STATE is merely non-empty, correct that condition first. When skipping is safe for a particular package manager, skip only on the documented exact state and prove that the cached directory is the intended restore output.

Prove what contributed to the build

Separate four identities: the lockfile, the cache key, the resolved dependencies and the published artifact. A successful task does not prove those identities agree.

bash 03-build-input-evidence.sh
set -euo pipefail

mkdir -p "$(Pipeline.Workspace)/evidence"

sha256sum package-lock.json > "$(Pipeline.Workspace)/evidence/lockfile.sha256"

npm ci --prefer-offline
npm ls --all --json > "$(Pipeline.Workspace)/evidence/resolved-dependencies.json"

npm run build
find dist -type f -print0 | sort -z | xargs -0 sha256sum > "$(Pipeline.Workspace)/evidence/artifact-files.sha256"

Adapt the inventory command to avoid leaking private registry URLs, tokens or sensitive metadata. The evidence must answer which versions were resolved and which files were produced. It should not become another unrestricted artifact.

Include every input that can change binary compatibility in the key: operating system, architecture, runtime or SDK version, package-manager generation and the relevant lockfile content. Do not use a branch name alone. Do not place deployable output in the dependency cache; publish it as an immutable pipeline artifact with its own digest.

Compare cached and cache-free canaries

Run the same commit in two isolated jobs. The first follows the candidate cache policy. The second uses an empty dependency-cache directory and performs a locked restore. Neither job deploys. Compare resolved dependency inventories, tests and artifact manifests.

text 04-cache-canary-matrix.txt
Cached canary
Same commit, pipeline revision, agent image and runtime
Exact evaluated cache key retained in logs
Cache state recorded as true, inexact or false
Locked restore still executed

Cache-free canary
New empty cache directory
Same registry configuration and lockfile
Locked restore from declared sources
No artifact or node_modules reused from another job

Compare
Resolved package names and versions
Native package architecture and runtime compatibility
Test results
File-by-file artifact digest
Build log warnings and install scripts

Stop
Dependency inventory differs without an explained platform reason
Lockfile changes but the exact cache key does not
Partial hit bypasses locked restore
Artifact digest differs and the build is expected to be deterministic

A different artifact digest is not automatically a cache defect: timestamps, source maps or generated metadata may make a build non-deterministic. Explain those fields or normalize them before using digest equality as a gate. Unexplained dependency or binary differences remain a stop condition.

Correct the key without hiding the incident

Azure DevOps pipeline caches are immutable. If a key points to incorrect content, do not expect a later run to repair that entry in place. Change a version segment in the key, narrow or remove unsafe restoreKeys, and let a successful job create a new cache entry.

Use an explicit generation such as npm-v3 when the cache contract changes. A generation is not a substitute for the lockfile hash; it gives operators a controlled invalidation lever. Keep fallback prefixes narrow enough that they cannot cross operating systems, architectures or incompatible runtime generations.

The permanent correction should leave a small audit surface: evaluated key, cache state, lockfile digest, restore result and artifact digest. Cache contents themselves rarely need long retention when the package manager and artifact manifest provide reproducible evidence.

Decide promotion, rebuild or rollback

Promote only when the cache key includes the effective dependency contract, the locked restore succeeds, the resolved inventory matches the lockfile, tests pass, and the artifact is tied to the same run. An exact hit improves confidence only when the key is correct.

Rebuild without cache when the state is inexact, evidence is missing, the cache path contains generated output, or the cached and cache-free canaries diverge. Publish a new artifact under a new run ID; do not relabel the suspect package.

Roll back the cache-policy change when it introduced a broad key, unsafe fallback or skipped restore. Keep production on the last known-good artifact while the corrected policy creates and validates a new cache generation. After recovery, repeat one exact hit and one miss to prove both paths.

Conclusion

A green cache task proves that Azure DevOps found reusable files. It does not prove that those files match the current lockfile or that the resulting artifact is safe to promote.

The production decision is therefore explicit: promote when cache state, locked restore, dependency inventory and artifact identity agree; rebuild cleanly when evidence is incomplete; roll back the cache policy when it can turn a partial hit into an unverified build input.