Skip to Content

CodeDeployでappspec.ymlが見つからないエラーの対処

概要

CodeDeploy使用するappsepec.ymlの圧縮(zip)時の階層構造について
下記エラーの対応

エラーメッセージ

The CodeDeploy agent did not find an AppSpec file within the unpacked revision directory at revision-relative path "appspec.yml". The revision was unpacked to directory "/opt/codedeploy-agent/deployment-root/xxxxx/xxx/deployment-archive", and the AppSpec file was expected but not found at path "/opt/codedeploy-agent/deployment-root/xxxxx/xxx/deployment-archive/appspec.yml". Consult the AWS CodeDeploy Appspec documentation for more information at http://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file.html

結論

zipで固めたいディレクトリにcdで移動してからzipコマンドを打つ
unzip -lの結果を確認し、直下にappspec.ymlがあるか確認

1
2
3
cd targetdir
zip -r ../targetdir.zip *
unzip -l ../targetdir.zip

状況

S3に設置したファイルをCodeDeployを利用して、EC2インスタンスにファイルをコピー(配置)したい
deploydirをzipで固め、S3にアップロード

deploydirディレクトリ

1
2
3
4
> tree deploydir/
deploydir/
├── appspec.yml
└── test.txt

appspec.ymlファイル
内容は同階層のtext.txt/home/ec2-user配下にコピーするだけ

1
2
3
4
5
6
> cat deploydir/appspec.yml
version: 0.0
os: linux
files:
  - source: test.txt
    destination: /home/ec2-user

CodeDeployからデプロイ

appspec.ymlが見つからないとエラーで終了

キャプチャ

原因

.zip直下にappspec.ymlがないといけないのに何も考えずにzipコマンドで圧縮した結果、
zip後の階層構造が意図しない構造になっていた

zipコマンドのオプション・引数例

この時点で必要だったのは-j指定でディレクトリ構造を無視して格納するオプションだった

OKパターン

ディレクトリ直下にappspec.ymlが存在している

-jとディレクトリ指定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
> zip -j deploydir_ok1.zip -r deploydir

> unzip -l deploydir_ok1.zip
Archive:  deploydir_ok1.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       83  2020-05-29 12:04   appspec.yml
        0  2020-05-29 12:03   test.txt
---------                     -------
       83                     2 files

-jとディレクトリ配下全指定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
> zip -j deploydir_ok2.zip deploydir/*

> unzip -l deploydir_ok2.zip
Archive:  deploydir_ok2.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       83  2020-05-29 12:04   appspec.yml
        0  2020-05-29 12:03   test.txt
---------                     -------
       83                     2 files

NGパターン

appspec.ymldeploydirディレクトリ配下に存在する
この階層構造の場合、下記エラーのappspec.ymlが見つからないに至る

the AppSpec file was expected but not found at path "/opt/codedeploy-agent/deployment-root/xxxxx/xxx/deployment-archive/appspec.yml

ただのディレクトリ指定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
> zip deploydir_ng1.zip -r deploydir/

> unzip -l deploydir_ng1.zip
Archive:  deploydir_ng1.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2020-05-29 12:04   deploydir/
       83  2020-05-29 12:04   deploydir/appspec.yml
        0  2020-05-29 12:03   deploydir/test.txt
---------                     -------
       83                     3 files

ただのディレクトリ配下全指定

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
> zip deploydir_ng2.zip deploydir/*

> unzip -l deploydir_ng2.zip
Archive:  deploydir_ng2.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       83  2020-05-29 12:04   deploydir/appspec.yml
        0  2020-05-29 12:03   deploydir/test.txt
---------                     -------
       83                     2 files

でも、-jだと不都合も

-j--junk-pathsの省略形でディレクトリ構造を無視する
たとえば以下のようなscriptsディレクトリ配下にスクリプトを保存していた場合にzip -jで圧縮すると

1
2
3
4
5
6
> tree deploydir/
deploydir/
├── appspec.yml
├── scripts
│   └── script.sh
└── test.txt

ディレクトリ構造が無視され、トップディレクトリにスクリプトファイルが保存されてしまう
appspec.yml内で指定しているあろうscript.shのパスが狂ってしまう

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
> zip -j deploydir_test.zip -r deploydir

> unzip -l deploydir_test.zip
Archive:  deploydir_test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       83  2020-05-29 12:04   appspec.yml
        0  2020-05-29 12:03   test.txt
        0  2020-05-29 13:28   script.sh
---------                     -------
       83                     3 files

結局、zipで固めたいディレクトリに移動してからzipするのが一番早そう

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
> cd deploydir

> zip -r ../deploydir_test.zip ./*

> unzip -l ../deploydir_test.zip
Archive:  deploydir_test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
       83  2020-05-29 12:04   appspec.yml
        0  2020-05-29 12:03   test.txt
        0  2020-05-29 13:28   script.sh
        0  2020-05-29 13:28   scripts/
        0  2020-05-29 13:28   scripts/script.sh
---------                     -------
       83                     5 files

感想

zipコマンドをなにも理解していなかった
CodeDeployの設定が悪いと疑って1時間過ぎ、zipコマンドを検証してさらに1時間経ってしまった

参考

ディレクトリ構造を無視して圧縮する場合はzipコマンドの-jオプションを使う - Qiita
【 zip 】コマンド(基礎編)――ファイルをZIP形式で圧縮する:Linux基本コマンドTips(34) - @IT
解凍した時にディレクトリができないようにzipで圧縮する方法 - Qiita