1
1

More than 3 years have passed since last update.

CodeBuildを使ってみよう -本編-

Last updated at Posted at 2021-05-31

構成

image.png

ビルド対象のディレクトリ構造(SpringBoot)

SpringBootプロジェクトをGradleでビルドし、イメージ化してECRにプッシュします。

ディレクトリ構造
.
├── Dockerfile
├── build
├── build.gradle
├── gradle
├── gradlew
├── settings.gradle
└── src

ビルドプロジェクト作成

image.png

ビルドプロジェクトの主な設定は以下の通りです。Dockerを使用する為、特権付与の項目にチェックを入れます。

設定名
ソースプロバイダ CodeCommit
サービスロール 管理者権限ロール
オペレーティングシステム Amazon Linux2
ランタイム Standard
イメージ aws/codebuild/amazonlinux2-x86_64-standard:3.0
イメージのバージョン aws/codebuild/amazonlinux2-x86_64-standard:3.0-21.04.23
特権付与 あり

Buildspec

ルートディレクトリ内にBuildspecファイルを入れる方法もありますが、コンソールから編集します。
image.png
以下がコードです。

version: 0.2
phases:
  install:
    runtime-versions:
      java: corretto8
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws ecr get-login-password --region ap-northeast-1 | docker login --username AWS --password-stdin accont_id.dkr.ecr.ap-northeast-1.amazonaws.com
  build:
    commands:
      - ./gradlew build
      - docker build -t portforio_backend .
      - docker tag portforio_backend:latest accont_id.dkr.ecr.ap-northeast-1.amazonaws.com/portforio_backend:latest
  post_build:
    commands:
       - docker push accont_id.dkr.ecr.ap-northeast-1.amazonaws.com/portforio_backend:latest

<成功>
image.png

<ECRにイメージがプッシュされている事を確認>
image.png

トラブルシューティング

ECRへのログインに失敗する

→サービスロールにECRにアクセスできるポリシーがアタッチされている事を確認.

./gradlew build permission denied

→chmod +x ./gradlew で実行権限を与えたファイルをプッシュします。

You have reached your pull rate limit.

こればかりは運のようです。もう一度ビルドを実行します。
(docker loginで回避できるようです。)

コンテナ内でdockerイメージをビルドする

CodeBuildと同じように、コンテナ内でイメージをビルドしてECRにプッシュできる事をローカル環境で確認しています。

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