简体   繁体   English

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

[英]How can I run an npm script for an AWS Elastic Beanstalk Deployment?

My package.json has: 我的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"
  },

When I deploy to Elastic Beanstalk, I'd like to run the postinstall , but apparently it doesn't do that. 当我部署到Elastic Beanstalk时,我想运行postinstall ,但是显然它没有这样做。 Okay, no problem. 好的没问题。

I created a file called .ebextensions/00.decrypt.config which has: 我创建了一个名为.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

However this doesn't seem to run either. 但是,这似乎也没有运行。 What am I doing incorrectly? 我做错了什么?

I figured out a workaround for this issue. 我想出了解决此问题的方法。 The npm binary on an EB instance is located in /opt/elasticbeanstalk/node-install/node-{version} . EB实例上的npm二进制文件位于/opt/elasticbeanstalk/node-install/node-{version} You should make sure this is present in your PATH first. 您应该先确保它出现在PATH

00_setpath.config 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

As you can see I am appending to .bash_profile and also adding PATH to the current shell. 如您所见,我将附加到.bash_profile并将PATH添加到当前shell。 The former should be sufficient for your purpose. 前者应足以满足您的目的。 I added the second one as I'm using the npm command within a script in my package.json , and it seems these scripts are run inside the same shell. 我在package.json的脚本中使用npm命令时添加了第二个脚本,似乎这些脚本在同一shell中运行。 TL/DR: You should now be able to use npm in both places. TL / DR:您现在应该可以在两个地方使用npm了。

As for your npm scripts, try using prestart instead of postinstall . 至于您的npm脚本,请尝试使用prestart而不是postinstall

A few suggestions: 一些建议:

  • Try to enclose your commands in quotes, that's a requirement 尝试将命令括在引号中,这是必需的
  • Also, not sure if $NODE_HOME is working - could you run simple test like echo $NODE_HOME > /tmp/test.txt? 另外,不确定$ NODE_HOME是否正常工作-您可以运行简单的测试,例如echo $ NODE_HOME> /tmp/test.txt吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM