如何缓存NodeJS全局模块AWS CodeBuild

时间:2019-05-30 21:08:51

标签: node.js amazon-web-services npm npm-install 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/内部)?

谢谢!

1 个答案:

答案 0 :(得分:0)

由于@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