LoginSignup
26
14

More than 5 years have passed since last update.

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

Posted at

概要

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の環境変数設定時に混入していました。
とほほ。。。

26
14
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
26
14