2015-04-23 73 views
1

package.json有:如何爲AWS Elastic Beanstalk部署運行npm腳本?

"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" 
    }, 

當我部署到彈性魔豆,我想運行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 

然而,這似乎並不要麼運行。我做錯了什麼?

+1

嘗試引用你的命令,這是一個要求 – sap1ens

+0

另外,不知道是否$ NODE_HOME工作 - 你可以運行簡單的測試,如echo $ NODE_HOME> /tmp/test.txt? – sap1ens

+0

您可以檢查/var/log/eb-activity.log和/var/log/eb-commandprocessor.log。這些文件會告訴你這些命令執行時發生了什麼。 –

回答

1

幾點建議:

  • 儘量附上你的命令在引號,這是一個要求
  • 另外,不知道$ NODE_HOME工作 - 可以運行簡單的測試樣回聲$ NODE_HOME>/tmp目錄/test.txt?
1

我想出瞭解決此問題的解決方法。 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

相關問題