CodeDeploy 挂钩代理安装文件夹中的 运行 个脚本

CodeDeploy hooks running scripts in the agent instalation folder

所以,我正在设置我的第一个使用 CodeDeploy (EC2 + S3) 的应用程序,我很难弄清楚如何在安装后 运行 脚本。

所以我在 AppSpec 文件中定义了一个 AfterInstall 挂钩,引用我在项目目录中的 bash 脚本文件。当脚本中的命令 运行 我收到错误消息,指出找不到文件。所以我在它之前放了一个 ls 命令并检查了日志。

我的脚本文件 运行ning 在 CodeDeploy 代理文件夹中。那里有很多我在测试时不小心创建的文件,但我希望它们位于我的项目根文件夹中。

--Root
----init.sh
----requirements.txt
----server.py

appspec.yml

version: 0.0
os: linux
files:
  - source: ./
    destination: /home/ubuntu/myapp
    runas: ubuntu
hooks:
  AfterInstall:
    - location: init.sh
      timeout: 600

init.sh

#!/bin/bash

ls
sudo apt install python3-pip
pip3 install -r ./requirements.txt
python3 ./server.py

所以当执行ls时,它没有列出我的项目根目录中的文件。我也试过 ${PWD} 而不是 ./ 但它没有用。它正在将脚本文件复制到代理文件夹并运行将其安装。

参考这个https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html

这个写在上面文档的最后

The location of scripts you specify in the 'hooks' section is relative to the root of the application revision bundle. In the preceding example, a file named RunResourceTests.sh is in a directory named Scripts. The Scripts directory is at the root level of the bundle.

但显然它仅指 appspec 文件中的路径。

有人可以帮忙吗?这是正确的吗?我必须使用脚本文件中硬编码的绝对路径?

是的,正确。如您所料,脚本不会在目标文件夹中执行。您需要对目标目录的引用进行硬编码 /home/ubuntu/myapp 以解析生命周期脚本中的文件路径。

先用cd切换目录:

cd  /home/ubuntu/myapp
ls
sudo apt install python3-pip
pip3 install -r ./requirements.txt
python3 ./server.py