我目前正在尝试通过 Github 和 AWS Codedeploy 将 nodejs 应用程序自动部署到 EC2 实例。我已经 尽可能地按照这里的说明进行操作,但是我的 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
并产生以下错误日志,导致部署失败:
错误代码:脚本失败
消息:指定位置的脚本: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
,所以我会假设权限等与我作为 ssh 进入框时相同ec2-user
。
我做错了什么愚蠢的事情?非常感谢。