我目前正在尝试通过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
进入盒子时一样。
我做错了什么愚蠢的事情?非常感谢。
正如mbaird和Chris的评论中准确指出的那样 - 我没有设置PATH。所以npm,node,pm2和...都失败了。
通过实验,我似乎需要在Codedeploy部署过程的每一步中重新建立我的路径。所以在我的stop.sh/beforeinstall.sh/afterinstall.sh/start.sh的顶部,我包括:
source /home/ec2-user/.bash_profile
生活很美好。然后我遇到了其他问题,pm2没有在正确的工作目录中启动节点,但是对codedeploy脚本进行了类似的调整。
事后来看,这一切都很明显,但我非常感谢你的帮助。感谢你们!
主机代理使用根相当剥离的环境。退出代码127表示操作系统无法找到加载脚本所需的文件(它可能是执行它所需的脚本)。
最好的办法是确保为root安装了npm。
因为,当作为服务启动时,主机代理来源/ etc / profile,您还可以添加所需的任何内容以使npm在那里工作。