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

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

我的 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,但显然它不会那样做。好的,没问题。

我创建了一个名为 .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

然而,这似乎也不 运行。我做错了什么?

几点建议:

  • 请尝试将您的命令括在引号中,这是一项要求
  • 另外,不确定 $NODE_HOME 是否有效 - 你能 运行 像 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