Cloudbuild and Dockerfile Example

An example of using cloud build and a dockerfile

One of the things this illustrates is passing through the commit id into an environment variable in the image

import git
repo = git.Repo("..")
latest_commit = repo.commit("origin/main").hexsha
print("Latest commit in hydrated repo is: ", latest_commit)
Latest commit in hydrated repo is:  86cb448a5faba352291d77dd1e770a3ce03a6937
import subprocess
def build_image():
    subprocess.check_call([
        "gcloud", "builds", "submit", "--project", "chat-lewi", "--async", "--config", "cloudbuild.yaml",
        "--substitutions=COMMIT_SHA=local-" + latest_commit,
        "--format=yaml"
    ])

build_image()            
Creating temporary tarball archive of 3 file(s) totalling 5.3 KiB before compression.
Uploading tarball of [.] to [gs://chat-lewi_cloudbuild/source/1688568359.217966-2cf3b986df0349fba522b0a748a9c81e.tgz]
createTime: '2023-07-05T14:46:00.812622789Z'
id: cb41ff2b-9103-41d2-b273-99f93e23dafe
logUrl: https://console.cloud.google.com/cloud-build/builds/cb41ff2b-9103-41d2-b273-99f93e23dafe?project=197678265256
logsBucket: gs://197678265256.cloudbuild-logs.googleusercontent.com
name: projects/197678265256/locations/global/builds/cb41ff2b-9103-41d2-b273-99f93e23dafe
options:
  logging: GCS_ONLY
  pool: {}
projectId: chat-lewi
queueTtl: 3600s
source:
  storageSource:
    bucket: chat-lewi_cloudbuild
    generation: '1688568360180330'
    object: source/1688568359.217966-2cf3b986df0349fba522b0a748a9c81e.tgz
sourceProvenance:
  resolvedStorageSource:
    bucket: chat-lewi_cloudbuild
    generation: '1688568360180330'
    object: source/1688568359.217966-2cf3b986df0349fba522b0a748a9c81e.tgz
status: QUEUED
steps:
- args:
  - --destination=us-west1-docker.pkg.dev/chat-lewi/random/cloudbuild:cb41ff2b-9103-41d2-b273-99f93e23dafe
  - --destination=us-west1-docker.pkg.dev/chat-lewi/random/cloudbuild:local-86cb448a5faba352291d77dd1e770a3ce03a6937
  - --destination=us-west1-docker.pkg.dev/chat-lewi/random/cloudbuild:latest
  - --dockerfile=Dockerfile
  - --cache=true
  - --build-arg=BUILD_COMMIT=local-86cb448a5faba352291d77dd1e770a3ce03a6937
  - --build-arg=BUILD_ID=cb41ff2b-9103-41d2-b273-99f93e23dafe
  - --label=BUILD_ID=cb41ff2b-9103-41d2-b273-99f93e23dafe
  - --label=COMMIT_SHA=local-86cb448a5faba352291d77dd1e770a3ce03a6937
  name: gcr.io/kaniko-project/executor:latest
substitutions:
  COMMIT_SHA: local-86cb448a5faba352291d77dd1e770a3ce03a6937
tags:
- dev
timeout: 3600s
Created [https://cloudbuild.googleapis.com/v1/projects/chat-lewi/locations/global/builds/cb41ff2b-9103-41d2-b273-99f93e23dafe].
Logs are available at [ https://console.cloud.google.com/cloud-build/builds/cb41ff2b-9103-41d2-b273-99f93e23dafe?project=197678265256 ].