概要
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.yml
がdeploydirディレクトリ配下に存在する
この階層構造の場合、下記エラーの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