如何在Amazon Elastic Beanstalk中使用.ebextensions运行“npm install”

11

我想使用eb deploy命令将我的PHP应用部署到Amazon Elastic Beanstalk。但是我的应用程序使用gulp来合并和压缩scss和js。

因此,我尝试在文件.ebextensios/03npm.config中运行以下命令:

commands:
  01-install-node:
    command: "yum install nodejs npm --enablerepo=epel -y"

container_commands:
  01-install-dependencies:
    command: "npm install"
  02-build:
    command: "npm run build"

但最终我收到了这个错误。

[Instance: i-c7800103] Command failed on instance. Return code: 1 Output: (TRUNCATED)...ttps://registry.npmjs.org/acorn npm http 304 https://registry.npmjs.org/amdefine npm http 304 https://registry.npmjs.org/wrappy npm ERR! npm ERR! Additional logging details can be found in: npm ERR! /var/app/ondeck/npm-debug.log npm ERR! not ok code 0. container_command 01-install-dependencies in .ebextensions/03npm.config failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.

我不确定,但似乎npm install遇到了一个可以被忽略的包错误,但它正在分派EB错误并停止整个过程。

我正在使用这台机器:64位Amazon Linux 2015.09 v2.0.4运行PHP 5.6

有人知道我们如何修复这个问题吗?


我在.ebextensions中使用几乎相同的.config文件遇到了同样的问题。你找到解决方案了吗?如果有,能否分享一下?谢谢。 - Thomas
我在答案中发布了我的解决方案,以便更好地解释它。 - Harrison
1
昨天我遇到了同样的问题,但不喜欢任何答案,最终在日志中找到了原因。具体来说,您需要查看 eb-activity.logeb-commandprocessor.log 以获取有关此错误的更多信息。我发现我的问题与我的 npm 安装本身有关。所以这不是一个 eb 问题本身,我修复了简单的 npm 错误,然后继续前进。希望这些信息能帮助那里的某些人 =) - clodal
3个回答

4

以下是我试过有效的方法:

packages:
    yum:
      git: []

commands:
    01_node_install:
        cwd: /tmp
        test: '[ ! -f /usr/bin/node ] && echo "node not installed"'
        command: 'yum install -y nodejs --enablerepo=epel'
    02_npm_install:
        cwd: /tmp
        test: '[ ! -f /usr/bin/npm ] && echo "npm not installed"'
        command: 'sudo rm -rf /usr/bin/npm | curl -L http://npmjs.org/install.sh | sh'
    03_node_update:
        cwd: /tmp
        test: '[ ! -f /usr/bin/n ] && echo "node not updated"'
        command: 'npm install -g n && n stable'

container_commands:
  # your container commands

更新: 在某些情况下,yum可能无法从仓库安装nodejs,因为有时候仓库URL不可用或下载失败。这种情况下,整个构建将会失败,因为npm install也将无法成功。解决方案是使用预先安装了nodejs的自定义AWS AMIs(Amazon Machine Images): 文档


这是唯一对我有效的选项。谢谢! - kevgathuku
这个注释绝对是一个时间节省器! - Fabio

1

到目前为止,我有一个丑陋的解决方案,但它能够正常工作。我在package.json中创建了这个脚本,按顺序完成了繁重的工作。它会创建一个新分支,在部署到EB之前提交编译后的文件。

package.json

{
  "scripts": {
    "production": "node ./node_modules/gulp/bin/gulp.js --production",
    "deploy": "git checkout -b deploy && npm run production && git add . && git commit -m \"build\" && eb deploy && git checkout master && git branch -D deploy"
  }
}

.elasticbeanstalk/config.yml

branch-defaults:
  deploy:
    environment: NAME_OF_ENVIRONMENT

谢谢。实际上,我最终使用了Circle CI和Docker(因为我在AWS Node.js环境方面的经验不是很好)。通过使用Circle CI,我能够直接从Github部署。也就是说,Circle CI获取最新的push,例如master,然后运行npm install,然后将项目构建到一个distribution文件夹中,然后传递给EB。使用.ebignore,我可以忽略除distribution文件夹和生产服务器之外的所有内容。因此,这些是传递给AWS EB的唯一文件。从那里,AWS EB创建一个Docker容器并在大约3-4分钟内运行它。 - Thomas

0
packages:
  yum:
    git: []

files:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/03_npm_install.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)
      cd $EB_APP_STAGING_DIR

      npm install bower
      npm install
      ./node_modules/bower/bin/bower install --allow-root --config.interactive=false
      ./node_modules/grunt-cli/bin/grunt dust

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接