bash 正在从代码部署代理目录运行 AWS CodeDeploy AfterInstall 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33311874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 13:48:12  来源:igfitidea点击:

AWS CodeDeploy AfterInstall script is being run from code-deploy agent dir

node.jsbashamazon-web-servicesaws-code-deploy

提问by matewilk

I'm trying to run AfterInstall script in AWS code deploy, but it is being run from the /opt/codedeploy-agent/ dir instead of my app directory.

我正在尝试在 AWS 代码部署中运行 AfterInstall 脚本,但它是从 /opt/codedeploy-agent/ 目录而不是我的应用程序目录运行的。

This is how appspec.yml file looks like:

这是 appspec.yml 文件的样子:

version: 0.0

os: linux

files:
  - source: /
    destination: /tmp/epub

hooks:
  AfterInstall:
    - location: server/install-packages.sh
      runas: root

As you can see it's a basic example.

如您所见,这是一个基本示例。

Now, the bash script looks like this:

现在,bash 脚本如下所示:

#!/bin/bash
npm install

I just want to npm install and that's it.

我只想 npm install 就是这样。

Unfortunately I'm getting the error:

不幸的是我收到错误:

LifecycleEvent - AfterInstall
Script - server/install-packages.sh
[stderr]npm ERR! install Couldn't read dependencies
[stderr]npm ERR! Linux 3.13.0-48-generic
[stderr]npm ERR! argv "/usr/bin/nodejs" "/usr/bin/npm" "install"
[stderr]npm ERR! node v4.2.1
[stderr]npm ERR! npm  v2.14.7
[stderr]npm ERR! path /opt/codedeploy-agent/package.json
[stderr]npm ERR! code ENOPACKAGEJSON
[stderr]npm ERR! errno -2
[stderr]npm ERR! syscall open
[stderr]
[stderr]npm ERR! package.json ENOENT: no such file or directory, open '/opt/codedeploy-agent/package.json'
[stderr]npm ERR! package.json This is most likely not a problem with npm itself.
[stderr]npm ERR! package.json npm can't find a package.json file in your current directory.
[stderr]
[stderr]npm ERR! Please include the following file with any support request:
[stderr]npm ERR!     /opt/codedeploy-agent/npm-debug.log

I was trying different appspec.yml configs like adding runas or adding "/" at the beginning of the location path. All the time it's trying to run from /opt/codedeoploy-agent/ directory.

我正在尝试不同的 appspec.yml 配置,例如在位置路径的开头添加 runas 或添加“/”。它一直试图从 /opt/codedeoploy-agent/ 目录运行。

In desperation, I've set absolute path to the script, but then I got :

无奈之下,我已经为脚本设置了绝对路径,但后来我得到了:

Script does not exist at specified location: /tmp/epub/server/install-packages.sh

It's really annoying as I'm doing everything according to docs, but probably I'm missing something very very small !

这真的很烦人,因为我按照文档做所有事情,但可能我错过了一些非常非常小的东西!

Thanks

谢谢

回答by matewilk

Ok,

好的,

So I've found out, that codedeoloy-agent is running AfterInstall (and probably all the other steps) from the temporary directory created by the agent on deploy instance, so in my case I had to modify the bash script by cd-ing to the proper directory:

所以我发现,codedeoloy-agent 正在从代理在部署实例上创建的临时目录运行 AfterInstall(可能还有所有其他步骤),所以在我的情况下,我不得不通过 cd-ing 修改 bash 脚本正确的目录:

#!/bin/bash
cd /tmp/epub/server/
npm install