Azure DevOps NPM 缓存不匹配

Azure DevOps NPM Cache Not Matching

我在 Azure Dev Ops 中为我的节点模块设置了缓存,它工作正常,但它似乎在我的一个应用程序中随机停止正常工作。当 NPM 缓存步骤运行时,它输出以下内容:

Getting a pipeline cache artifact with one of the following fingerprints:
Fingerprint: \`npm|"Windows_NT"|---------------------------------2dPPpMMy5Q=\`

稍后在 Post-job: NPM Cache 中输出以下内容:

Resolving key:
 - npm                                [string]
 - "Windows_NT"                       [string]
 - D:\a\s/app/package-lock.json [file] --> ---------------------------------FF1E9956A35F9431D19483B2791A486
Resolved to: npm|"Windows_NT"|---------------------------------MSOLgTMsIA=
##[warning]The given cache key has changed in its resolved value between restore and save steps;
original key: npm|"Windows_NT"|---------------------------------2dPPpMMy5Q=
modified key: npm|"Windows_NT"|---------------------------------MSOLgTMsIA=

ApplicationInsightsTelemetrySender will correlate events with X-TFS-Session ---------------------------------ad8
Getting a pipeline cache artifact with one of the following fingerprints:
Fingerprint: `npm|"Windows_NT"|---------------------------------MSOLgTMsIA=`
There is a cache hit: `npm|"Windows_NT"|---------------------------------MSOLgTMsIA=`
Used scope: ---------------------------------b942a8b;refs/heads/dev;---------------------------------.git
Cache with fingerprint `npm|"Windows_NT"|---------------------------------MSOLgTMsIA=` already exists.
ApplicationInsightsTelemetrySender correlated 1 events with X-TFS-Session ---------------------------------ad8

您需要确保 CachePost-Job Cache 步骤之间的构建步骤中的 none 更改缓存键值。

例如,我们使用以下值作为 NPM 示例的缓存键。

npm | $(Agent.OS) | ${{ parameters.WORKING_DIR }}/package-lock.json

所以假设构建中有一个 npm publish 步骤更改了 package-lock.json 的值。在这种情况下,您永远不会获得缓存命中,因为 package-lock.json(哈希)是唯一缓存键的一部分。

There are two workarounds — either remove the change causing step or remove package-lock.json from the key.

查看此文档:

https://medium.com/tenets/azure-pipeline-caching-a53e8117c242

消息“给定的缓存键在还原和保存步骤之间的解析值中发生了变化”可能是因为 package-lock.json 文件是使用与您的管道不同的 node/npm 版本生成的正在使用。

我今天遇到了类似的问题,我通过更新 node/npm 的本地版本以匹配我的管道版本

解决了这个问题

缓存构建或验证管道的 npm install 步骤的唯一可靠方法是在 package.json 文件中使用 explicit dependencies。因此,在 specify 次要或补丁发布更新的版本之前没有 ^~

这是因为 npm install 不尊重您的 package-lock.json 文件。它会根据您 package.json 中的语义版本自动更新任何依赖项ci。这意味着你不能让你的缓存键成为你的 package-lock.json 的散列,因为它只会被后续的 npm install 步骤更新,并且你的散列将不断不同(更不用说您正在更新管道中的依赖项cies,而不是之前,在开发过程中)。

npm 尝试使用 npm ci 命令解决此问题——ci 代表“全新安装”。这 安装 package-lock.json 文件中列出的内容,这很棒。但是,顾名思义,它还会删除您所有的 node_modules 并重新安装它们,从而破坏了之前任何缓存的目的。