首页 > 解决方案 > NPM 缓存步骤在 Azure DevOps 中不起作用

问题描述

我按照 Microsoft 的以下文档为我尝试在 azure 中构建的 android 应用程序配置 npm 缓存步骤,而不是 package.json-Lock 我使用的是 package.json。

https://docs.microsoft.com/en-us/azure/devops/pipelines/release/caching?view=azure-devops#nodejsnpm

我能够在缓存后步骤中上传缓存依赖文件,并在第二次运行管道时正确上传该文件,但即使在工作区中下载了 npm 缓存数据后,npm install 步骤仍在调用远程库和下载远程依赖项。

我还尝试为 npm install 步骤运行npm install --prefer-offline但确实有效。如果我还缺少任何东西,请告诉我。

谢谢你。

标签: androidnode.jsazurecachingnpm

解决方案


使用该Cache任务缓存应用程序的 node_modules 文件夹。使用缓存命中变量 ( cacheHitVar) 来存储缓存恢复的结果。true缓存恢复时(缓存命中)将设置为 ,否则设置为false

然后为安装依赖项的任务使用条件(例如npm ci)。仅在缓存未命中时安装它们。

steps:
  - task: Cache@2
    displayName: Cache node_modules
    inputs:
      key: 'npm | "$(Agent.OS)" | $(Build.SourcesDirectory)/package-lock.json'
      path: $(Build.SourcesDirectory)/node_modules
      cacheHitVar: CACHE_RESTORED

  - task: Npm@1
    displayName: 'Install the dependencies'
    inputs:
      command: custom
      verbose: false
      customCommand: 'ci'
    condition: ne(variables.CACHE_RESTORED, 'true')

成功恢复缓存后,您将在管道执行中看到以下输出。

Azure 管道执行


推荐阅读