简体   繁体   English

在 VSTS 中缓存 npm install 任务

[英]Cache npm install Task in VSTS

I have configured a private agent in VSTS and have installed NPM there globally.我已经在 VSTS 中配置了一个私有代理,并在那里全局安装了 NPM。 When I'm trying to install NPM through my build task, it is still installing NPM packages for every build which is taking an aweful lot of time- approximately 12 minutes.当我尝试通过构建任务安装 NPM 时,它仍在为每个构建安装 NPM 包,这花费了大量时间 - 大约 12 分钟。

How can I cache the NPM installations so that the build time is reduced?如何缓存 NPM 安装以减少构建时间?

We use npm-cache , npm-cache is a node module that will calculate a hash of your package.json file for every hash it will create zip folder on your build server with the content of node_modules, now npm install is reduced to extracting a zip on every build (of course only in case you didn't actually change package.json). 我们使用npm-cache ,npm-cache是​​一个节点模块,它将为每个哈希计算一个package.json文件的哈希,它将使用node_modules的内容在构建服务器上创建zip文件夹,现在npm install简化为提取一个在每个版本上压缩(当然,只有在您实际上没有更改package.json的情况下)。

The idea is: in the first time the tool download the npm packages and save them locally, in the second time if the package.json not changed he takes the packages from the local disk and copy them to the build agent folder, only if the package.json changed he downloads the packages from the internet. 这个想法是:第一次,该工具下载npm软件包并将其保存在本地;第二次,如果package.json没有更改,他将从本地磁盘上获取软件包并将其复制到build agent文件夹中,除非package.json更改为他从互联网下载软件包。

  1. Install the npm-cache on the build machine: 在构建机器上安装npm-cache:

    npm install npm-cache -g

  2. In the build definition add Command Line task (Tool: C:\\Windows\\User\\AppData\\Roaming\\npm\\npm-cache (or just npm-cache if you add the tool to environment path variables); Arguments: install npm ; Working folder: $(Build.SourcesDirectory) (or where package.json located). 在构建定义中,添加命令行任务(工具: C:\\Windows\\User\\AppData\\Roaming\\npm\\npm-cache (如果将工具添加到环境路径变量,则仅添加npm-cache );参数: install npm ;工作文件夹: $(Build.SourcesDirectory) (或package.json所在的位置)。

MS has finally implemented this feature (currently in beta) https://docs.microsoft.com/en-us/azure/devops/pipelines/caching/index?view=azure-devops#npm MS终于实现了此功能(当前处于测试版) https://docs.microsoft.com/zh-cn/azure/devops/pipelines/caching/index?view=azure-devops#npm

From there: 从那里:

variables:
  npm_config_cache: $(Pipeline.Workspace)/.npm

steps:
- task: CacheBeta@0
  inputs:
    key: $(Build.SourcesDirectory)/package-lock.json
    path: $(npm_config_cache)
  displayName: Cache npm

- script: npm ci

Unfortunately we cannot cache the NPM installations as no such a built-in feature for now. 不幸的是,由于目前尚无此类内置功能,因此我们无法缓存NPM安装。

However there's already a user voice submitted to suggest the feature : Improve hosted build agent performance with build caches , and seems the VSTS team are actively working on this now... 但是,已经有用户提出了建议该功能的声音: 通过构建缓存提高托管的构建代理性能 ,似乎VSTS团队现在正在积极地进行这项工作...

For now, you can try to speed Up NPM INSTALL on Visual Studio Team Services 目前,您可以尝试在Visual Studio Team Services上加快NPM安装

Use Cache task 使用缓存任务

Caching is added to a pipeline using the Cache pipeline task.使用缓存管道任务将缓存添加到管道中。 This task works like any other task and is added to the steps section of a job此任务的工作方式与任何其他任务一样,并添加到作业的步骤部分

With the following configuration:使用以下配置:

pool:
  name: Azure Pipelines

steps:
- task: Cache@2
  inputs:    
    key: 'YOUR_WEB_DIR/package.json'
    path: 'YOUR_WEB_DIR/node_modules/'

- task: Npm@1
  inputs:
    command: 'install'
    workingDir: 'YOUR_WEB_DIR/frontend'

You can use key YOUR_WEB_DIR/package-lock.json too, but be aware that file might be changed by other next step like npm install so hash also will be changed.您也可以使用键YOUR_WEB_DIR/package-lock.json ,但请注意,该文件可能会被其他下一步(如npm install更改,因此哈希值也会更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM