Speed up builds with cache images

You can take advantage of buildpacks cache images to reuse results from a previous build. This reduces the overall duration of time when you build images with Google Cloud's buildpacks, including when you build images in ephemeral environments such as Cloud Build.

The following steps in this guide focus on remote builds with Cloud Build. For local builds, refer to both the cache image instructions and pack command reference page.

Using cache images with Cloud Build

You must create a build configuration file, for example cloudbuild.yaml, to enable buildpacks cache images in Cloud Build. Your build configuration file instructs the pack CLI to publish a build image to a specified remote repository. This build image then gets used as your "cache image" for subsequent builds.

Note that the first build that you run with your new build configuration file uses the pack command to create and then push the initial build image cache to your repository. Your build performance improvements are seen only after the initial cache image is available to your builds.

Creating a build configuration file

To define a Cloud Build configuration file you must write a YAML file with a build step that uses the pack CLI create your image.

In the following example cloudbuild.yaml, the build creates an image for your application or function using buildpacks, creates the initial build cache image, and pushes it to a repository:

options:
  logging: CLOUD_LOGGING_ONLY
  pool: {}
projectId: PROJECT_ID
steps:
- name: gcr.io/k8s-skaffold/pack
  entrypoint: pack
  args:
  - build
  - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/IMAGE_NAME
  - --builder
  - gcr.io/buildpacks/builder:latest
  - --cache-image
  - LOCATION-docker.pkg.dev/PROJECT_ID/REPO_NAME/CACHE_IMAGE_NAME:latest
  - --publish 

Replace:

  • LOCATION with the region name of your container repository. Example: us-west2
  • PROJECT_ID with the ID of your Google Cloud project.
  • REPO_NAME with the name of your Docker repository.
  • IMAGE_NAME with the name of your application or function container image.
  • CACHE_IMAGE_NAME with the name of your build cache image.

Also see the other supported aurgements that you can define in your build configuration file in the pack build command reference.

Building remotely with cache images

To run a build, you include the --config command flag and specify your configuration file. For example, to use a file named cloudbuild.yaml you might run:

gcloud builds submit --config cloudbuild.yaml --pack image=example-docker.pkg.dev/my-project-id/repo/example-image

For more information about remote builds, see Build an application or Build a function.