AWS CodeDeploy执行AfterInstall脚本时是从code-deploy代理目录运行的

17

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

以下是appspec.yml文件的样子:

version: 0.0

os: linux

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

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

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

现在,bash脚本看起来像这样:

#!/bin/bash
npm install

我只想运行npm install,但是出现了错误:
很不幸,我遇到了以下错误:
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

我尝试了不同的appspec.yml配置,比如添加runas或在位置路径前面添加“/”。但每次它都试图从/opt/codedeploy-agent/目录中运行。

绝望之下,我将脚本设置为绝对路径,但是接下来我得到了:

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

我正在按照文档进行操作,但可能我错过了非常微小的东西,这真的很烦人!

谢谢。

1个回答

29

好的,

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

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

你从哪里得知这个消息的? - artburkart
2
我不记得是否是从这里得出的结论:http://docs.aws.amazon.com/codedeploy/latest/userguide/app-spec-ref.html#app-spec-ref-hooks - 可能有一句话建议这样做 - 我记得我花了很长时间 - 或更可能是通过试错或两者的组合 :) - matewilk
@matewilk - 超级有帮助! - Ben
谢谢,实际上这个问题被投票否决,说“这个问题没有展示任何研究努力;它不清楚或者没有用”。所以感谢你,很高兴能帮忙! - matewilk
这是正确的,在测试期间也有同样的认识。另外,你可以在你的bash脚本中使用"(cd /your/path && npm install)"来在子shell中进行目录更改,以便不影响其他事情。 - DougW
1
我在每个脚本的顶部放置了 cd "$( dirname "${BASH_SOURCE[0]}" )"。紧随其后的是 source config.sh,用于...嗯...配置。 - musicin3d

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接