Help us understand the problem. What is going on with this article?

AWS CodeBuildで失敗したときに確認するポイント

More than 1 year has passed since last update.

概要

AWS CodeBuildを使ってソースコードのビルドをして、ECRにプッシュしようとしたときにハマったのでメモ。

確認ポイント

  1. ECRへアクセスする権限が付与されているか
  2. S3へアクセスする権限が付与されているか
  3. buildspec.ymlのフォーマットは正しいか
    ymlファイルのパースに失敗すると、DOWNLOAD_SOURCEフェーズで失敗します。
    YAML_FILE_ERROR Message: did not find expected key at line 41とエラーログが出ていたのでbuildspec.ymlの42行目を見たところインデントのずれがありました。。。

    [Container] 2018/03/21 03:41:52 Waiting for agent ping
    [Container] 2018/03/21 03:41:52 Waiting for DOWNLOAD_SOURCE
    [Container] 2018/03/21 03:42:04 Phase is DOWNLOAD_SOURCE
    [Container] 2018/03/21 03:42:05 CODEBUILD_SRC_DIR=/codebuild/output/src586790933/src
    [Container] 2018/03/21 03:42:05 YAML location is /codebuild/output/src586790933/src/buildspec.yml
    [Container] 2018/03/21 03:42:05 Phase complete: DOWNLOAD_SOURCE Success: false
    [Container] 2018/03/21 03:42:05 Phase context status code: YAML_FILE_ERROR Message: did not find expected key at line 41

  4. 環境変数は正しいか
    buildspec.ymlもIAMの設定も完璧なはずなのに、なぜかビルドが失敗する。
    しかも、CodebuildのBUILDフェーズのしかもdocker tagコマンド実行時に失敗するという状況が発生したとき。
    環境変数を見直したら解消されました。

[Container] 2018/03/21 04:39:31 Running command echo Tagging the Docker image...
Tagging the Docker image...

[Container] 2018/03/21 04:39:31 Running command docker tag demo:latest $REPOSITORY_URI:latest
"docker tag" requires exactly 2 arguments.
See 'docker tag --help'.

Usage: docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE

[Container] 2018/03/21 04:39:31 Command did not exit successfully docker tag demo:latest $REPOSITORY_URI:latest exit status 1
[Container] 2018/03/21 04:39:31 Phase complete: BUILD Success: false
[Container] 2018/03/21 04:39:31 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: docker tag demo:latest $REPOSITORY_URI:latest. Reason: exit status 1

原因は、buildspec.ymlで定義していた変数($REPOSITORY_URI)内に半角スペースが紛れ込んでいたことでした。

buildspec.yml
version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - $(aws ecr get-login --no-include-email --region ${AWS_DEFAULT_REGION})
      - REPOSITORY_URI_SERVER=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${IMAGE_NAME}

(中略)

  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...
      - docker-compose build
      - echo Tagging the Docker image...
      - docker tag demo:latest $REPOSITORY_URI:latest

しかも、その半角スペースはCodeBuildの環境変数設定時に混入していました。
とほほ。。。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした