5

I'm using Jenkins (CloudBees) to build my project, and this runs some scripts in each build to download some node packages using npm. Yesterday the npm registry server was having troubles and this blocked the build cycle of the project.

In order not to depend on external servers, is there a way to persist my node_modules folder in Jenkins so I don't have to download them in every build?

Nick Jones
  • 4,395
  • 6
  • 33
  • 44
farolfo
  • 388
  • 1
  • 4
  • 13
  • Use a site npm proxy/cache like sonatype Nexus repository or artifactory? – Zeitounator Aug 05 '21 at 22:19
  • Does this answer your question? [Cache NPM dependencies on Jenkins pipeline](https://stackoverflow.com/questions/46870020/cache-npm-dependencies-on-jenkins-pipeline) – T.Todua Mar 04 '22 at 20:19

2 Answers2

4

You can check the package.json file and backup node_modules directory.

When you start next build in jenkins, just check package.json file and node_modules backup, if package.json file is not changed, just using previous backup.

PKG_SUM=$(md5sum package.json|cut -d\  -f 1)
CACHED_FILE=${PKG_SUM}.tgz
[[ -f ${CACHED_FILE} ]] && tar zxf ${CACHED_FILE}
npm install
[[ -f ${CACHED_FILE} ]] || tar zcf ${CACHED_FILE} node_moduels

above is quite simple cache implementation, otherwise you should check the cache file is not damaged.

Guixing Bai
  • 350
  • 2
  • 11
  • 2
    I believe `tar zcf ${CACHED_FILE} node_moduels` should be `tar zcf ${CACHED_FILE} node_modules` - just a minor spelling mistake – gsaqui Oct 13 '20 at 23:18
1

CloudBees uses a pool of slaves to support your builds, and by nature you can have builds to run on various hosts, so start with a fresh workspace. Anyway, we try to allocate a slave that you already used to avoid download delays - this works for all file stored in workspace.

I don't think this would have prevented issue with npm repository being offline anyway.

nicolas de loof
  • 2,593
  • 1
  • 13
  • 11