【发布时间】:2016-03-06 02:30:38
【问题描述】:
我目前正在尝试通过 Github 和 AWS Codedeploy 将 nodejs 应用程序自动部署到 EC2 实例。我已尽可能严格地按照here 的说明进行操作,但我的 AfterInstall 挂钩事件遇到了障碍。
这是我的 yml 文件:
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-user ssh 进入框时相同。
我做错了什么愚蠢的事情? 非常非常感谢。
【问题讨论】:
-
值得强调一下,因为错误信息很长。最终的错误是:npm: command not found
-
我猜它以
ec2-user运行,但没有运行您的登录脚本,例如.bash_profile和.bashrc,所以它的路径上没有npm。 -
将
source /path_to_bash_profile放在 afterinstall.sh 的顶部
标签: node.js amazon-web-services amazon-ec2 npm aws-code-deploy