[A-00182] Cloud Buildを使ってみる

google cloudが提供するbuildツールであるcloud buildを使ってみたいと思います。

・Cloud Functionsをbuild・deployしてみる

cloudbuild.yamlを使ってcloud functionsをビルド・デプロイしてみます。

使用するプロジェクトは他の記事で使ったものを使用します。下記を参照してください。

ディレクトリ構成は下記の通りとなっております。cloudbuild.yamlの配置場所に注目してください。

上のyamlファイルを配置して中身は下記のように記述します。

steps:
  # clean install
  - name: gcr.io/cloud-builders/mvn
    entrypoint: mvn
    args:
      - "clean"
      - "install"
      - "-DskipTests=true"
  # test
  - name: gcr.io/cloud-builders/mvn
    entrypoint: mvn
    args: ['test']
  # cloudfunction deploy
  - name: gcr.io/google.com/cloudsdktool/cloud-sdk
    args:
      - gcloud
      - functions
      - deploy
      - feed-fmt-conv-fn
      - --gen2
      - --runtime=java17
      - --region=asia-northeast1
      - --service-account=437530287615-compute@developer.gserviceaccount.com
      - --source=.
      - --entry-point=com.myapp.convfl.function.FeedFmtConverterFn
      - --memory=512MB
      - --trigger-event-filters=type=google.cloud.storage.object.v1.finalized
      - --trigger-event-filters=bucket=testcfneventjp20240129
      - --set-env-vars=UPLOADBKT=testuploadcfneventjp20240130
      - --set-env-vars=PROJECTID=dataflowtest002

[name]属性に各アクションが記載されています。

上記の例では[mvn clean install]->[mvn test]->[gcloud functions deploy]としています。

cloudbuild実行コマンドは下記の通りとなります。

gcloud builds submit --config cloudbuild.yaml .

上記のコマンドを実行すると下記のようにログが出力されます。

MacBook-Pro:conv-file$ gcloud builds submit --config cloudbuild.yaml .
Creating temporary tarball archive of 21 file(s) totalling 95.7 KiB before compression.
Uploading tarball of [.] to [gs://dataflowtest002_cloudbuild/source/1708261259.353705-100991e46a6746519e72f6f06829ca81.tgz]
Created [https://cloudbuild.googleapis.com/v1/projects/dataflowtest002/locations/global/builds/6e81652d-0002-4fb3-a608-1a73338cc4ea].
Logs are available at [ https://console.cloud.google.com/cloud-build/builds/6e81652d-0002-4fb3-a608-1a73338cc4ea?project=437530287615 ].
------------------------------------------------------------------ REMOTE BUILD OUTPUT ------------------------------------------------------------------
starting build "6e81652d-0002-4fb3-a608-1a73338cc4ea"

FETCHSOURCE
Fetching storage object: gs://dataflowtest002_cloudbuild/source/1708261259.353705-100991e46a6746519e72f6f06829ca81.tgz#1708261260781098
Copying gs://dataflowtest002_cloudbuild/source/1708261259.353705-100991e46a6746519e72f6f06829ca81.tgz#1708261260781098...
/ [1 files][ 64.0 KiB/ 64.0 KiB]                                                
Operation completed over 1 objects/64.0 KiB.
BUILD
Starting Step #0
Step #0: Already have image (with digest): gcr.io/cloud-builders/mvn
Step #0: [INFO] Scanning for projects...
......

Step #2: 
Step #2: buildConfig:
Step #2:   build: projects/437530287615/locations/asia-northeast1/builds/4cadc8a9-7e26-4942-95f9-09af847a9ed1
Step #2:   dockerRegistry: ARTIFACT_REGISTRY
Step #2:   dockerRepository: projects/dataflowtest002/locations/asia-northeast1/repositories/gcf-artifacts
Step #2:   entryPoint: com.myapp.convfl.function.FeedFmtConverterFn
Step #2:   runtime: java17
Step #2:   source:
Step #2:     storageSource:
Step #2:       bucket: gcf-v2-sources-437530287615-asia-northeast1
Step #2:       generation: '1708261414140114'
Step #2:       object: feed-fmt-conv-fn/function-source.zip
Step #2:   sourceProvenance:
Step #2:     resolvedStorageSource:
Step #2:       bucket: gcf-v2-sources-437530287615-asia-northeast1
Step #2:       generation: '1708261414140114'
Step #2:       object: feed-fmt-conv-fn/function-source.zip
Step #2: createTime: '2024-02-07T18:39:39.115966695Z'
Step #2: environment: GEN_2
Step #2: eventTrigger:
Step #2:   eventFilters:
Step #2:   - attribute: bucket
Step #2:     value: testcfneventjp20240129
Step #2:   eventType: google.cloud.storage.object.v1.finalized
Step #2:   pubsubTopic: projects/dataflowtest002/topics/eventarc-asia-northeast1-feed-fmt-conv-fn-623950-133
Step #2:   retryPolicy: RETRY_POLICY_DO_NOT_RETRY
Step #2:   serviceAccountEmail: 437530287615-compute@developer.gserviceaccount.com
Step #2:   trigger: projects/dataflowtest002/locations/asia-northeast1/triggers/feed-fmt-conv-fn-623950
Step #2:   triggerRegion: asia-northeast1
Step #2: labels:
Step #2:   deployment-tool: cli-gcloud
Step #2: name: projects/dataflowtest002/locations/asia-northeast1/functions/feed-fmt-conv-fn
Step #2: serviceConfig:
Step #2:   allTrafficOnLatestRevision: true
Step #2:   availableCpu: '0.3333'
Step #2:   availableMemory: 512M
Step #2:   environmentVariables:
Step #2:     PROJECTID: dataflowtest002
Step #2:     UPLOADBKT: testuploadcfneventjp20240130
Step #2:   ingressSettings: ALLOW_ALL
Step #2:   maxInstanceCount: 100
Step #2:   maxInstanceRequestConcurrency: 1
Step #2:   revision: feed-fmt-conv-fn-00005-pux
Step #2:   service: projects/dataflowtest002/locations/asia-northeast1/services/feed-fmt-conv-fn
Step #2:   serviceAccountEmail: 437530287615-compute@developer.gserviceaccount.com
Step #2:   timeoutSeconds: 60
Step #2:   uri: https://feed-fmt-conv-fn-77dm3ivb7q-an.a.run.app
Step #2: state: ACTIVE
Step #2: updateTime: '2024-02-18T13:04:45.529005144Z'
Step #2: url: https://asia-northeast1-dataflowtest002.cloudfunctions.net/feed-fmt-conv-fn
Finished Step #2
PUSH
DONE
---------------------------------------------------------------------------------------------------------------------------------------------------------
ID                                    CREATE_TIME                DURATION  SOURCE                                                                                         IMAGES  STATUS
6e81652d-0002-4fb3-a608-1a73338cc4ea  2024-02-18T13:01:02+00:00  3M47S     gs://dataflowtest002_cloudbuild/source/1708261259.353705-100991e46a6746519e72f6f06829ca81.tgz  -       SUCCESS


Updates are available for some Google Cloud CLI components.  To install them,
please run:
  $ gcloud components update



To take a quick anonymous survey, run:
  $ gcloud survey

GCPコンソールからもビルド履歴を確認できます。

・Appendix

公式ドキュメントはこちら

https://cloud.google.com/functions/docs/troubleshooting?hl=ja#cloud-sdk_2

https://cloud.google.com/build/docs/securing-builds/configure-access-for-cloud-build-service-account?hl=ja

https://cloud.google.com/build/docs/securing-builds/configure-access-for-cloud-build-service-account?hl=ja&_ga=2.78370388.-133574885.1688480522

https://cloud.google.com/build/docs/deploying-builds/deploy-cloud-run?hl=ja

https://cloud.google.com/sdk/gcloud/reference/builds/submit

https://cloud.google.com/build/docs/building/build-java?hl=ja

https://cloud.google.com/functions/docs/create-deploy-http-java?hl=ja

参考文献はこちら

https://zenn.dev/nananaoto/articles/c56bc2b38834b32c8fd7

https://stackoverflow.com/questions/54315104/error-gcloud-app-deploy-error-response-9-cloud-build-xxxxxxxxxxxx-status

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

*