如何为AWS Elastic Beanstalk部署运行npm脚本?

我的package.json有:

  "scripts": { "start": "node_modules/.bin/coffee server.coffee", "test": "NODE_ENV=test node test/runner.js", "coverage": "NODE_ENV=test COVERAGE=1 node test/runner.js -R html-cov test/ > ./test/coverage.html", "testw": "fswatch -o test src | xargs -n1 -I{} sh -c 'coffeelint src server.coffee ; npm test'", "db:drop": "node scripts/drop-tables.js", "encryptConfig": "node_modules/.bin/coffee config/encrypt.coffee", "decryptConfig": "node_modules/.bin/coffee config/decrypt.coffee", "postinstall": "npm run decryptConfig" }, 

当我部署到Elastic Beanstalk时,我想要运行postinstall ,但显然不这样做。 好的没问题。

我创build了一个名为.ebextensions/00.decrypt.config的文件,它具有:

 commands: 00-add-home-variable: command: sed -i 's/function error_exit/export HOME=\/root\n\nfunction error_exit/' /opt/elasticbeanstalk/hooks/appdeploy/pre/50npm.sh container_commands: 02-decrypt-config: command: $NODE_HOME/bin/npm run decryptConfig 

但是,这似乎并没有运行。 我做错了什么?

几点build议:

  • 尝试用引号括起你的命令,这是一个要求
  • 此外,不知道是否$ NODE_HOME工作 – 你可以运行简单的testing,如echo $ NODE_HOME> /tmp/test.txt?

我想出了一个解决这个问题的办法。 EB实例上的npm二进制文件位于/opt/elasticbeanstalk/node-install/node-{version} 。 你应该确保它首先出现在PATH

00_setpath.config

 commands: 01_set_path: command: echo 'export PATH=$PATH:`ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin' >> /root/.bash_profile 02_set_path: command: export PATH=$PATH:`ls -td /opt/elasticbeanstalk/node-install/node-* | head -1`/bin 

正如你所看到的,我正在附加到.bash_profile ,并将PATH添加到当前的shell。 前者应该足够你的目的。 我添加了第二个,因为我在我的package.json脚本中使用了npm命令,看起来这些脚本是在同一个shell中运行的。 TL / DR:你现在应该可以在两个地方使用npm了。

至于你的npm脚本,请尝试使用prestart而不是postinstall