本ページはプロモーションが含まれています。

スポンサーリンク

Build-Deploy-Pipeline デプロイ(CD/CI)

AWS CodeBuild/CodeDeploy/CodePipelineの基礎知識

投稿日:2022年3月4日 更新日:

概要

AWS CodeBuildビルドマシンの貸し出しサービス。
SpringBootのソース群から、jarファイルを作成する。
buildspec.yml
AWS CodeDeployビルド成果物を自動デプロイできるサービス。
Code Buildで作成したjarファイルをEC2/ECSにデプロイする。
appspec.yml
AWS CodePipelineGithub(or AWS CodeCommit)-CodeBuild-CodeDeployを連携してCD/CIを実現するサービス。

AWS CodeBuildの基礎知識

AWS CodeBuiildはビルドマシンの貸し出しサービスです。
ビルドの手順を「buildspec.yml」に記述することで、
ビルドを実行し、SpringBootプロジェクトの.jarを作成できます。

buildspec.yml

buildspec.yml はプロジェクトルートディレクトリ直下に配置します。
SpringBootプロジェクトをビルドするための最低限のbuildspec.ymlを示します。

version: 0.2

phases:
  install:
    runtime-versions:
      java: corretto11
  build:
    commands:
      - ./gradlew bootJar
artifacts:
  files:
    - build/libs/okozukai-system-0.0.1-SNAPSHOT.jar

phases: install で、javaのバージョンを指定します。
phases: build で、gradlew bootJarを実行してビルドします。
artifacts: files で、buildされたjarの出力パスを教えます。

そうすることで、javaがインストールされ、gradlew bootJarが実行され、ビルド成果物がS3など指定の場所へ連携されます。

runtime-versionsで使用可能なJavaのバージョン

java: openjdk8Ubuntu のみ
java: openjdk11Ubuntuのみ
java: corretto8Ubuntu ,Amazon Linux2
java: corretto11Ubuntu ,Amazon Linux2
使用可能なランタイム より

buildspecの仕様は「CodeBuild のビルド仕様に関するリファレンス」を参照。

AWS CodeBuildのエラー

YAML_FILE_ERROR Message: YAML file does not exist

[Container] 2022/03/04 12:18:38 Phase is DOWNLOAD_SOURCE
[Container] 2022/03/04 12:18:38 CODEBUILD_SRC_DIR=/codebuild/output/src110234391/src/github.com/noricgeographic/okozukai-system
[Container] 2022/03/04 12:18:41 Phase complete: DOWNLOAD_SOURCE State: FAILED
[Container] 2022/03/04 12:18:41 Phase context status code: YAML_FILE_ERROR Message: YAML file does not exist

原因:buildspec.ymlを使う設定になってるが、builidspec.ymlが存在しない。
対策:builidspec.ymlをプロジェクトルート直下に置く。
   コミットできてるか確認する。

YAML_FILE_ERROR Message: Unknown runtime version named 'openjdk11' of java. This build image has the following versions:

[Container] 2022/03/05 01:41:56 Waiting for agent ping
[Container] 2022/03/05 01:41:57 Waiting for DOWNLOAD_SOURCE
[Container] 2022/03/05 01:41:59 Phase is DOWNLOAD_SOURCE
[Container] 2022/03/05 01:41:59 CODEBUILD_SRC_DIR=/codebuild/output/src682313074/src/github.com/noricgeographic/okozukai-system
[Container] 2022/03/05 01:41:59 YAML location is /codebuild/output/src682313074/src/github.com/noricgeographic/okozukai-system/buildspec.yml
[Container] 2022/03/05 01:41:59 No commands found for phase name: install
[Container] 2022/03/05 01:41:59 Processing environment variables
[Container] 2022/03/05 01:41:59 Selecting 'java' runtime version 'openjdk11' based on manual selections...
[Container] 2022/03/05 01:42:01 Phase complete: DOWNLOAD_SOURCE State: FAILED
[Container] 2022/03/05 01:42:01 Phase context status code: YAML_FILE_ERROR Message: Unknown runtime version named 'openjdk11' of java. This build image has the following versions: corretto11, corretto8

原因:buildspec.ymlで指定しているruntime-versionsは、選択しているOSでは適用できない。
上記の例では、openjdk11を指定したが、corretto11, corretto8しか使えない。

選択:build機のOSを変更する or 指定するruntime-versions を変更する。

COMMAND_EXECUTION_ERROR Message: Error while executing command: gradlew build. Reason: exit status 127

[Container] 2022/03/05 01:35:59 Running command gradlew build
/codebuild/output/tmp/script.sh: 4: gradlew: not found

[Container] 2022/03/05 01:35:59 Command did not exit successfully gradlew build exit status 127
[Container] 2022/03/05 01:35:59 Phase complete: BUILD State: FAILED
[Container] 2022/03/05 01:35:59 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: gradlew build. Reason: exit status 127
[Container] 2022/03/05 01:35:59 Entering phase POST_BUILD
[Container] 2022/03/05 01:35:59 Running command echo Build completed on `date`
Build completed on Sat Mar 5 01:35:59 UTC 2022

原因:gradlewが見つからない。
対策:./gradlew とcommandsに記述する。

Phase context status code: CLIENT_ERROR Message: no matching artifact paths found

[Container] 2022/03/05 02:04:42 Expanding base directory path: .
[Container] 2022/03/05 02:04:42 Assembling file list
[Container] 2022/03/05 02:04:42 Expanding .
[Container] 2022/03/05 02:04:42 Expanding file paths for base directory .
[Container] 2022/03/05 02:04:42 Assembling file list
[Container] 2022/03/05 02:04:42 Expanding target/okozukaiapp-1.0.jar
[Container] 2022/03/05 02:04:42 Skipping invalid file path target/okozukaiapp-1.0.jar
[Container] 2022/03/05 02:04:42 Phase complete: UPLOAD_ARTIFACTS State: FAILED
[Container] 2022/03/05 02:04:42 Phase context status code: CLIENT_ERROR Message: no matching artifact paths found

原因:buildspec.ymlで指定しているfilesが見つからない。
対策:buildspec.ymlのfilesのパスを実際に出力されるパスに合わせる。

上記の例では、target/okozukaiapp-1.0.jarを探しに行ってるが、
実際は build/libs/okozukai-system-0.0.1-SNAPSHOT.jar に出力されるので、filesにはそちらを記述する。

AWS CodeDeploy

AWS CodeDeployは、
・EC2
・AWS Lambda
・Amazon ECS
ビルド成果物を自動デプロイできるサービスです。

デプロイ手順をappspec.ymlに記述します。

AWS CodePipeline

AWS CodePipelineのエラー

無効なアクション設定 The source artifact bucket 'okozukai-build-bucket' is not versioned.

原因:S3バケットのバージョニングが無効になっている。
対策:S3バケットのバージョニングを有効にする。

Error: there is no Procfile and no .jar file at root level of your source bundle

Deployment completed, but with errors: During an aborted deployment, some instances may have deployed the new application version. To ensure all instances are running the same version, re-deploy the appropriate application version. Failed to deploy application. Unsuccessful command execution on instance id(s) 'i-0aed9e89703cf131d'. Aborting the operation. [Instance: i-0aed9e89703cf131d] Command failed on instance. Return code: 1 Output: Engine execution has encountered an error.. Instance deployment failed. For details, see 'eb-engine.log'.
2022/03/06 00:41:18.927724 [ERROR] An error occurred during execution of command [app-deploy] - [CheckProcfileForJavaApplication]. Stop running the command. Error: there is no Procfile and no .jar file at root level of your source bundle 

原因:Build Artifactsから.jarが見つからない。
対策:buildspec.ymlのartifacts:discard-paths: yesを追記する。

本ページはプロモーションが含まれています。

Udemyのハンズオン動画講座でSpringBootのスキルを磨く!

スポンサーリンク

-Build-Deploy-Pipeline, デプロイ(CD/CI)

Copyright© 【Spring Hack】 , 2024 All Rights Reserved Powered by AFFINGER5.