LoginSignup
1
1

More than 3 years have passed since last update.

AWS codebuildでのshell 出力の注意点

Last updated at Posted at 2019-05-14

目的

環境情報を環境変数で吸収させるため、codebuild内で出力した イメージ定義ファイルimagedefinitions.json の内容を文字列置換したい。

現行ソース

buildspec.yml
post_build: 
    commands: 
      - printf '[{"name":"PREFIX-fargate-container","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json

修正方針

クォート処理が手間なので、出力後にsedコマンドで置換することにした。

修正案buildspec.yml
      - sed -e "s/PREFIX/$PREFIX/g" imagedefinitions.json > imagedefinitions.json 

しかし、テストしてみたところ、置換後に出力したはずのイメージ定義ファイルimagedefinitions.json が空となってしまい、後続のCodeDeployでエラーが発生した。

対応

一時的ファイルをかませることで動作が確認できたので、そちらで対応した。

修正後buildspec.yml
post_build: 
    commands: 
      - printf '[{"name":"PREFIX-fargate-container","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > temp.json
      - sed -e "s/PREFIX/$PREFIX/g" temp.json > imagedefinitions.json 

所感

入力ファイルと出力ファイルを同一ファイルにすると、空になってしまうのはなぜだろう。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1