首页 > 解决方案 > 如何缓存 NodeJS 全局模块 AWS CodeBuild

问题描述

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

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

为了处理这个问题,首先我用命令找出 npm install Lerna 的位置npm list -g并返回

/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-clnpm( webpackinside )?/usr/local/lib/node_modules/

谢谢!

标签: node.jsamazon-web-servicesnpmnpm-installaws-codebuild

解决方案


感谢@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

推荐阅读