継続は力なり

タイトル通り定期的な更新を心掛けるブログです。

『コンテナ時代のWebサービスの作り方』での CircleCI ビルド時にハマったエラー対応を整理する

タダです.

Terraform とコンテナの勉強の一環で「コンテナ時代のWebサービスの作り方」を読んでいます.本書では CI/CD に CircrlCI を使います.CircleCI の定義で本書の内容をそのまま転記した際にエラーが発生する場面がありました.CircleCI を触れるのが初だったので対処した内容をこの記事でまとめていきます.なお,CircleCI の実行バージョンは2.1です.

booth.pm

CircleCI エラー一覧

bundler のバージョン指定でのエラー

本書の Web アプリケーション開発では,Ruby と bundler を使います.そのため,CircleCI の実行環境でも開発同様に Ruby と bunder が使えるように定義している箇所があります.bundler のバージョン指定してインストールを行う時に Unable to parse YAML mapping values are not allowed here というメッセージがでました.

メッセージ詳細

#!/bin/sh -eo pipefail
# Unable to parse YAML
# mapping values are not allowed here
#  in 'string', line 11, column 39:
#      ...     command: gem install bundler: 2.0.2
#                                          ^
# 
# -------
# Warning: This configuration was auto-generated to show you the message above.
# Don't rerun this job. Rerunning will have no effect.
false

エラーメッセージの内容的に YAML の記法的に問題あるかと思い,以下のように修正して対応しました.

# 変更前
command: gem install bundelr:2.0.2

# 変更後
command: |
   gem install bundler:2.0.2

ECR の認証失敗

ECS でアプリケーションを動作させるコンテナイメージを ECR に格納するのですが,その認証でエラーが発生しました.

#!/bin/sh -eo pipefail
$(aws ecr get-login --no-include-email --region ap-northeast-1)
docker build -t ${ECR_DOMAIN}:$CIRCLE_SHA1 -t ${ECR_DOMAIN}:latest .

An error occurred (UnrecognizedClientException) when calling the GetAuthorizationToken operation: The security token included in the request is invalid.

ECR 認証エラーには下記の処理を追加して回避してます.

[追加] aws configure set aws_access_key_id ${AWS_ACCESS_KEY_ID}
[追加] aws configure set aws_secret_access_key ${AWS_SECRET_ACCESS_KEY}
[追加] aws configure set default.region ${AWS_DEFAULT_REGION}
[追加] aws configure set default.output json
$(aws ecr get-login --no-include-email --region ap-northeast-1)
docker build -t ${ECR_DOMAIN}:$CIRCLE_SHA1 -t ${ECR_DOMAIN}:latest .

取り急ぎエラーを回避する対応を行なったものの,メッセージにもでていますがセキュリティ的によろしくないのでこちらの処理も書き換えて再度ビルドにトライしていきます

WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store

Login Succeeded

AWC CLI v2ではget-loginget-login-passwordに置き換わっているため,CLI v2 でビルドしていこうと考えてます.

パスワードをシェルの履歴またはログに公開するリスクを減らすには、代わりに以下の例のコマンドを使用します。この例では、パスワードは docker login コマンドに直接パイプされ、そこで --password-stdin オプションによってパスワードパラメータに割り当てられます。

docs.aws.amazon.com

まとめ

僕が CircleCI を初めて触ったので手こずってしまいましたが,本書を読む進めていく中で同じ場面に遭遇した時の早期解決の参考情報となれば嬉しいです.