NPM问题使用AWS codedeploy部署nodejs实例

我目前正在尝试通过Github和AWS Codedeploy自动将nodejs应用程序部署到EC2实例。 我尽可能地遵循了这里的指示,但是我的AfterInstall钩子事件却遇到了麻烦。

这是我的XML文件:

version: 0.0 os: linux files: - source: /backend destination: /home/ec2-user/signal permissions: - object: / pattern: "**" owner: ec2-user group: ec2-user hooks: ApplicationStop: - location: backend/app/deploy/stop.sh timeout: 10 runas: ec2-user BeforeInstall: - location: backend/app/deploy/beforeinstall.sh timeout: 1200 runas: ec2-user AfterInstall: - location: backend/app/deploy/afterinstall.sh timeout: 1200 runas: ec2-user ApplicationStart: - location: backend/app/deploy/start.sh timeout: 60 runas: ec2-user ValidateService: - location: backend/app/deploy/validate.sh timeout: 60 runas: ec2-user 

我通过AWS CLI调用部署,如下所示:

 aws deploy create-deployment --application-name Signal --deployment-config-name CodeDeployDefault.OneAtATime --deployment-group-name Production --description "Deployment" --github-location repository=githubusername/repository,commitId=ABCD123 --ignore-application-stop-failures 

一切工作正常,直到我到达AfterInstall阶段,我的'afterinstall.sh'执行。 该文件看起来像这样:

 #!/bin/bash cd /home/ec2-user/signal/app/ npm install 

并生成以下错误日志,导致部署失败:

错误代码: ScriptFailed

消息:指定位置的脚本:backend / app / deploy / afterinstall.sh以用户身份运行ec2-user失败,退出码为127

 LifecycleEvent - AfterInstall Script - backend/app/deploy/afterinstall.sh [stderr]/opt/codedeploy-agent/deployment-root/be9902d2-8af0-46fd-b186-23ead6bea5a4/d-SBW6YCLKC/deployment-archive/backend/app/deploy/afterinstall.sh: line 7: npm: command not found 

但是,如果我SSH入我的ec2实例,导航到任一临时目录:

 /opt/codedeploy-agent/deployment-root/be9902d2-8af0-46fd-b186-23ead6bea5a4/d-SBW6YCLKC/deployment-archive/backend/app/deploy/ 

要么

 cd /home/ec2-user/signal/app/ 

并手动运行'npm install',或通过./afterinstall.sh运行我的脚本,然后npm运行良好。

Codedeploy代理为什么不同? 我使用的是'runas:ec2-user',所以我会假设权限等等,当我作为ec2用户ssh'ed到框中。

我做错了什么愚蠢的事情? 非常感谢。

正如在mbaird和Chris的评论中准确地指出的那样 – 我没有设置PATH。 所以npm和node,pm2和…都失败了。

通过实验,似乎我需要在Codedeploy部署过程的每一步都重新build立自己的path。 因此,在我的stop.sh/beforeinstall.sh/afterinstall.sh/start.sh的顶部,我包括:

 source /home/ec2-user/.bash_profile 

生活很好 然后,我遇到了其他问题pm2不在正确的工作目录开始节点,但类似的调整codedeploy脚本得到了工作。

事后看来这一切都很明显,但我非常感谢帮助。 感谢你们!

主机代理使用相当剥离的环境。 退出代码127表示操作系统找不到需要加载脚本的文件(可能是执行脚本所需的脚本)。

最好的办法是确保npm是为root安装的。

因为主机代理以/etc/profile作为服务启动时,你也可以添加任何你需要在那里工作的npm。