Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
608 views
in Technique[技术] by (71.8m points)

caching - Azure Pipeline - how to save node-modules cache of multiple different projects

I have multiple angular projects that I ran in Azure DevOps to publish them. the step of the NPM INSTALL task takes too long because it's running on every project separately (I have duplicated packages). I'm looking for a solution that gives me the ability to save installed packages to cache.

so far I saw examples of only one project cache saving. but for me it's not good because I have different projects with different dependencies & shared dependencies.

I thought maybe to write a script that distinct all packages from all projects node_modules, install them all and then give each project the dependencies he needs.

I'll appreciate your help, thank you.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can try the following ways.

1.Save node-modules cache for each projects. You can refer to the sample in this document:

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

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

- script: npm ci

You can add multiple cache tasks to create a cache for each of your node_modules folders.

2.Make multiple projects share node_modules directory.

If the module identifier passed to require() is not a core module, and does not begin with '/', '../', or './', then Node.js starts at the parent directory of the current module, and adds /node_modules, and attempts to load the module from that location. Node.js will not append node_modules to a path already ending in node_modules.

If it is not found there, then it moves to the parent directory, and so on, until the root of the file system is reached.

Please find more details in this ticket and document. Then, you only need to cache one node_modules folder.

3.Use Self-hosted agents instead of Microsoft-hosted agents.

  • In Microsoft-hosted agents, each time you run a pipeline, you get a fresh virtual machine. The virtual machine is discarded after one use.
  • In Self-hosted agents, machine-level caches and configuration persist from run to run , which can boost speed.

See Capabilities and limitations of Microsoft-hosted agents. If you want to install and use a self-hosted agent, you can refer to this document.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...