GCP CloudBuild を使って仕事の開発フローと同じようにビルド&イメージpush&デプロイを自動化した開発環境が欲しい

GCP CloudBuild を使ってScrewdriverのようにビルド&イメージpush&デプロイを自動化した開発環境が欲しい

使いたい

手順はこう

  1. まず、CloudRunへSpringBootアプリをサンプルでいいから入れる
  2. それをCloudBuildからデプロイできるようにする
  3. Github上のソースコードからデプロイできるようにする
  4. Github上のリポジトリ更新にHooKしてCloudBuildを実行する

1. Cloud Run に Java サービスをデプロイする

https://cloud.google.com/run/docs/quickstarts/build-and-deploy/deploy-java-service?hl=ja

言われたとおりにコード仕込んでるのにdeployコマンドが失敗しちゃう

よくわかんないからmavenに変えたけど、やっぱり無理だなあって

手元で mvn package 実行しただけなら通るんだけど ログからビルドに使ってるコマンド抜いて実行したらやっぱ失敗する

$ ./mvnw clean package --batch-mode -DskipTests -Dhttp.keepAlive=false -f=pom.xml
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< com.example:helloworld >-----------------------
[INFO] Building helloworld 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.2.0:clean (default-clean) @ helloworld ---
[INFO] Deleting /home/*******/git/study_cloudbuild/helloworld/target
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ helloworld ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ helloworld ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/*******/git/study_cloudbuild/helloworld/target/classes
[INFO] -------------------------------------------------------------
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] /home/*******/git/study_cloudbuild/helloworld/src/main/java/com/example/helloworld/HelloworldApplication.java:[3,1] class, interface, or enum expected
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.869 s
[INFO] Finished at: 2023-02-05T13:50:08+09:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.10.1:compile (default-compile) on project helloworld: Compilation failure
[ERROR] /home/*******/git/study_cloudbuild/helloworld/src/main/java/com/example/helloworld/HelloworldApplication.java:[3,1] class, interface, or enum expected
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException
[ERROR] /home/*******/git/study_cloudbuild/helloworld/src/main/java/com/example/helloworld/HelloworldApplication.java:[3,1] class, interface, or enum expected
[INFO] 1 error

と思ったらIDE使わずに書いてたからpackage宣言しくってただけだった

直したらうまくいった

Please enter numeric choice or text value (must exactly match list item):  1

To make this the default region, run `gcloud config set run/region asia-east1`.

This command is equivalent to running `gcloud builds submit --pack image=[IMAGE] /home/sh3eys/git/study_cloudbuild/helloworld` and `gcloud run deploy helloworld --image [IMAGE]`

Allow unauthenticated invocations to [helloworld] (y/N)?  y

Building using Buildpacks and deploying container to Cloud Run service [helloworld] in project [study-cloudbuild-and-gae] region [asia-east1]
✓ Building and deploying new service... Done.
  ✓ Uploading sources...
  ✓ Building Container... Logs are available at [https://console.cloud.google.com/cloud-build/builds/d******************************309c?project=7**************7].
  ✓ Creating Revision... Revision deployment finished. Checking container health.
  ✓ Routing traffic...
  ✓ Setting IAM Policy...
Done.
Service [helloworld] revision [helloworld-0*****1-b**] has been deployed and is serving 100 percent of traffic.
Service URL: https://helloworld-***************e.a.run.app

先が長いがいったんここまで

正直、このコマンドでほとんど望んでることができてる気がするが 環境をコントロールできないのでそこをどうしようか

次はこれをやっていくことでリポジトリへのマージをHookしてCloud BuildからCDしてもらう?

Cloud Build を使用した Cloud Run へのデプロイ https://cloud.google.com/build/docs/deploying-builds/deploy-cloud-run?hl=ja

そうじゃなく普通にDB連携としてこっちをやった方が実りがあるのかもしれない

Cloud runにおけるFirestore利用について https://zenn.dev/straydog/articles/c76bb7d6fb6ec3f46268