如何缓存 NodeJS 全局模块 AWS CodeBuild

How to cache NodeJS global modules AWS CodeBuild

有没有办法在 AWS CodeBuild 上缓存 NodeJS 全局模块?

我正在使用 LernaJS 来处理我的存储库,每次构建开始时,我都会使用命令 npm install -g lerna 安装它(需要 30 秒)。

为了处理这个问题,首先我弄清楚 npm 使用命令 npm list -g 安装 Lerna 的位置并返回

/usr/local/lib 
├─┬ grunt@1.0.4 
│ ├── coffeescript@1.10.0 
...
├─┬ lerna@3.14.1 
│ ├─┬ @lerna/add@3.14.0 
│ │ ├── @lerna/bootstrap@3.14.0 deduped 
...

然后我尝试缓存 /usr/local/lib/node_modules/**/* 文件夹,但收到以下错误:

[Container] 2019/05/30 20:09:00 Running command npm install -g lerna 
/codebuild/output/tmp/script.sh: 4: /codebuild/output/tmp/script.sh: npm: not found 

[Container] 2019/05/30 20:09:00 Command did not exit successfully npm install -g lerna exit status 127 
[Container] 2019/05/30 20:09:00 Phase complete: INSTALL State: FAILED 
[Container] 2019/05/30 20:09:00 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: npm install -g lerna. Reason: exit status 127 

所以我检查了 /usr/local/lib/node_modules/ 的内容 我有这些包:

[Container] 2019/05/30 20:19:11 Running command ls /usr/local/lib/node_modules 
grunt 
grunt-cli 
lerna 
npm 
webpack 

我最后一次尝试是缓存 /usr/local/lib/node_modules/lerna/**/*。这样不会抛出任何错误,但缓存也不起作用:

[Container] 2019/05/30 20:30:00 MkdirAll: /codebuild/local-cache/custom/656f09faf2819a785eae5e09f5d26a44ff4f20edf155297d6819c9600540cd26/usr/local/lib/node_modules/lerna 
[Container] 2019/05/30 20:30:00 Symlinking: /usr/local/lib/node_modules/lerna => /codebuild/local-cache/custom/656f09faf2819a785eae5e09f5d26a44ff4f20edf155297d6819c9600540cd26/usr/local/lib/node_modules/lerna 

...

[Container] 2019/05/30 20:30:01 Running command npm install -g lerna 
/usr/local/bin/lerna -> /usr/local/lib/node_modules/lerna/cli.js 
+ lerna@3.14.1 
added 650 packages from 321 contributors and updated 1 package in 40.628s 

我错过了什么吗?有没有办法在构建开始之前将 Lerna 保存为 gruntgrunt-clnpmwebpack(在 /usr/local/lib/node_modules/ 内)?

谢谢!

感谢@JD D 的评论,我创建了一个 docker 图像,将其推送到 AWS ECR 并将其用作我自己的图像。

我的 Dockerfile:

FROM node:lts
RUN npm install -g yarn lerna
RUN apt-get update && \
  apt-get install -y groff less && \
  apt-get clean
RUN curl https://s3.amazonaws.com/aws-cli/awscli-bundle.zip -o awscli-bundle.zip
RUN unzip awscli-bundle.zip  && \
  ./awscli-bundle/install -i /usr/local/aws -b /usr/local/bin/aws && \
  rm awscli-bundle.zip