複数行に渡るコマンドを書く
CodeBuildのビルド仕様を定義するbuildspec.yml
内にて、複数行に渡るコマンドを定義したいです。
失敗
コンソールで複数行に渡るコマンドを実行したい場合は、行末に\
を入れます。そうすることで、次行の入力内容も同行として解釈して実行してくれますよね。
% terraform plan -no-color -input=false \
-target="module.base.null_resource.remove_default_coredns_deployment" \
-target="module.base.null_resource.modify_kube_dns" \
-target="module.base.helm_release.coredns"
これと同じ様にbuildspec.ymlにも書いてみました。
buildspec.yml
build:
commands:
- terraform plan -no-color -input=false \
-target="module.base.null_resource.remove_default_coredns_deployment" \
-target="module.base.null_resource.modify_kube_dns" \
-target="module.base.helm_release.coredns"
するとビルド実行結果は以下のようにエラーになりました。
[Container] 2022/09/29 06:30:26 Running command terraform plan -no-color -input=false \ -target="module.base.null_resource.remove_default_coredns_deployment" \ -target="module.base.null_resource.modify_kube_dns" \ -target="module.base.helm_release.coredns"
Error: Too many command line arguments
成功
どうやらシンプルに複数行に渡って書けば良いみたいです。
buildspec.yml
build:
commands:
- terraform plan -no-color -input=false
-target="module.base.null_resource.remove_default_coredns_deployment"
-target="module.base.null_resource.modify_kube_dns"
-target="module.base.helm_release.coredns"
改行は無視してコマンド実行してくれます。
[Container] 2022/09/29 06:34:05 Running command terraform plan -no-color -input=false -target="module.base.null_resource.remove_default_coredns_deployment" -target="module.base.null_resource.modify_kube_dns" -target="module.base.helm_release.coredns"
module.base.module.eks.data.aws_partition.current: Reading...
module.base.module.eks.aws_cloudwatch_log_group.this[0]: Refreshing state... [id=/aws/eks/eks-cluster/cluster]
module.base.module.eks.module.fargate.data.aws_iam_policy_document.eks_fargate_pod_assume_role[0]: Reading...
module.base.module.vpc.aws_vpc.this[0]: Refreshing state... [id=vpc-03d10983a498eeb04]
module.base.data.aws_availability_zones.available: Reading...
(その後も通常通りTerraformのログが出力されていきました)
条件分岐を書く
では、以下のような条件分岐処理はどの様に書けば良いでしょうか。
HOGE=9
if [ $HOGE -gt 10 ]; then
echo “HOGE is over 10”
else
echo “HOGE is under 10”
fi
失敗1
以下のように各行1コマンドにすると、
buildspec.yml
build:
commands:
- HOGE=9
- if [ $HOGE -gt 10 ]; then
- echo “HOGE is over 10”
- else
- echo “HOGE is under 10”
- fi
当然エラーになります。
[Container] 2022/09/30 11:40:16 Running command HOGE=9
[Container] 2022/09/30 11:40:16 Running command if [ $HOGE -gt 10 ]; then
/codebuild/output/tmp/script.sh: line 9: syntax error: unexpected end of file
[Container] 2022/09/30 11:40:16 Command did not exit successfully if [ $HOGE -gt 10 ]; then exit status 2
[Container] 2022/09/30 11:40:16 Phase complete: BUILD State: FAILED
[Container] 2022/09/30 11:40:16 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: if [ $HOGE -gt 10 ]; then. Reason: exit status 2
失敗2
さっきやったようにそのまま改行すると、
buildspec.yml
build:
commands:
- HOGE=9
- if [ $HOGE -gt 10 ]; then
echo “HOGE is over 10”
else
echo “HOGE is under 10”
fi
1行で解釈されて、これはこれでエラーになります。
[Container] 2022/09/30 11:44:32 Running command HOGE=9
[Container] 2022/09/30 11:44:32 Running command if [ $HOGE -gt 10 ]; then echo “HOGE is over 10” else echo “HOGE is under 10” fi
/codebuild/output/tmp/script.sh: line 9: syntax error: unexpected end of file
[Container] 2022/09/30 11:44:32 Command did not exit successfully if [ $HOGE -gt 10 ]; then echo “HOGE is over 10” else echo “HOGE is under 10” fi exit status 2
[Container] 2022/09/30 11:44:32 Phase complete: BUILD State: FAILED
[Container] 2022/09/30 11:44:32 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: if [ $HOGE -gt 10 ]; then echo “HOGE is over 10” else echo “HOGE is under 10” fi. Reason: exit status 2
成功
literal styleなるものを使います。
buildspec.yml
build:
commands:
- HOGE=9
- |
if [ $HOGE -gt 10 ]; then
echo “HOGE is over 10”
else
echo “HOGE is under 10”
fi
成功しました!
[Container] 2022/09/30 11:54:09 Running command HOGE=9
[Container] 2022/09/30 11:54:09 Running command if [ $HOGE -gt 10 ]; then
echo “HOGE is over 10”
else
echo “HOGE is under 10”
fi
“HOGE is under 10”