阅读背景:

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

来源:互联网 

I am currently trying to automate deployment of a nodejs application to an EC2 instance via Github and AWS Codedeploy. I have followed the instructions from here as closely as possible, but I have hit a snag with my AfterInstall hook event.

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

Here is my yml file:

这是我的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

I invoke the deploy via the AWS CLI like so:

我通过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

Everything works fine, until I reach the AfterInstall phase and my 'afterinstall.sh' is executed. That file looks like this:

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

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

And produces the following error log, causing a failed deployment:

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

Error Code: ScriptFailed

错误代码:ScriptFailed

Message: Script at specified location: backend/app/deploy/afterinstall.sh run as user ec2-user failed with exit code 127

消息:指定位置的脚本: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

However, if I ssh into my ec2 instance, navigate to either the temp directory:

但是,如果我ssh到我的ec2实例,导航到临时目录:

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

or

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

and either manually run 'npm install', or run my script via ./afterinstall.sh, then npm runs fine.

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

Why are things different for the Codedeploy Agent? I'm using 'runas: ec2-user', so I would assume permissions etc are the same as when I'm ssh'ed into the box as ec2-user.

为什么Codedeploy代理的情况有所不同?我正在使用'runas:ec2-user',所以我假设权限等与我作为ec2-user进入盒子时的权限相同。

What idiotic thing am I doing wrong? Many, many thanks.

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

2 个解决方案

#1


12  

As accurately noted in the comments by mbaird and Chris - it was that I didn't have my PATH set. So npm, and node, and pm2 and... all failed.

正如mbaird和Chris的评论中准确指出的那样 - 我没有设置PATH。所以npm,节点,pm2和......都失败了。

Through experimentation, it appeared I needed to reestablish my path with every step of the Codedeploy deploy process. So at the top of my stop.sh/beforeinstall.sh/afterinstall.sh/start.sh, I included:

通过实验,我似乎需要在Codedeploy部署过程的每一步中重新建立我的路径。所以在我的stop.sh/beforeinstall.sh/afterinstall.sh/start.sh的顶部,我包括:

source /home/ec2-user/.bash_profile

and life was good. I then ran into other issues with pm2 not starting node in the right working directory, but similar tweaking to the codedeploy scripts got that working.

生活很美好。然后我遇到了其他问题,pm2没有在正确的工作目录中启动节点,但是对codedeploy脚本进行了类似的调整。

This was all obvious in hindsight, but I'm extremely grateful for the help. Thank you guys!

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

#2


-2  

The host agent uses roots fairly stripped down environment. An exit code of 127 indicates that the OS can't find some file it needs to load the script (it could be the script of something that is needed to execute it).

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

The best thing to do is make sure npm is installed for root.

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

Since, the host agent sources /etc/profile when launched as a service, you can also add anything you need to get npm working there.

因为,当作为服务启动时,主机代理源/ etc / profile,您还可以添加任何所需的内容以使npm在那里工作。


分享到: