4

I am using gcr.io/cloud-builders/bazel to build my images on google container build.

From the logs, it seems it spends most of the time setting up the workspace for bazel. This workspace does not change from build to build so I think could be pre-computed and stored in a new image.

How can I speed up these bazel builds on Google Cloud Platform?

Already have image (with digest): gcr.io/cloud-builders/bazel
Extracting Bazel installation...
........................
Loading: 
Loading: 0 packages loaded
Loading: 0 packages loaded
Loading: 0 packages loaded
Loading: 0 packages loaded
Loading: 0 packages loaded
WARNING: /builder/home/.cache/bazel/_bazel_root/eab0d61a99b6696edb3d2aff87b585e8/external/io_bazel_rules_go/go/def.bzl:137:3: DEPRECATED: com_github_golang_protobuf : new_go_repository is deprecated. Please migrate to go_repository soon.
WARNING: /builder/home/.cache/bazel/_bazel_root/eab0d61a99b6696edb3d2aff87b585e8/external/io_bazel_rules_go/go/def.bzl:137:3: DEPRECATED: com_github_golang_glog : new_go_repository is deprecated. Please migrate to go_repository soon.
WARNING: /builder/home/.cache/bazel/_bazel_root/eab0d61a99b6696edb3d2aff87b585e8/external/io_bazel_rules_go/go/def.bzl:137:3: DEPRECATED: org_golang_google_grpc : new_go_repository is deprecated. Please migrate to go_repository soon.
WARNING: /builder/home/.cache/bazel/_bazel_root/eab0d61a99b6696edb3d2aff87b585e8/external/io_bazel_rules_go/go/def.bzl:137:3: DEPRECATED: org_golang_x_net : new_go_repository is deprecated. Please migrate to go_repository soon.
WARNING: /builder/home/.cache/bazel/_bazel_root/eab0d61a99b6696edb3d2aff87b585e8/external/io_bazel_rules_go/go/def.bzl:137:3: DEPRECATED: com_github_gogo_protobuf : new_go_repository is deprecated. Please migrate to go_repository soon.
WARNING: /builder/home/.cache/bazel/_bazel_root/eab0d61a99b6696edb3d2aff87b585e8/external/io_bazel_rules_go/go/def.bzl:137:3: DEPRECATED: com_github_grpc_ecosystem_grpc_gateway : new_go_repository is deprecated. Please migrate to go_repository soon.
WARNING: /builder/home/.cache/bazel/_bazel_root/eab0d61a99b6696edb3d2aff87b585e8/external/io_bazel_rules_go/go/def.bzl:137:3: DEPRECATED: org_golang_google_genproto : new_go_repository is deprecated. Please migrate to go_repository soon.
Analyzing: target //:docker (4 packages loaded)
Analyzing: target //:docker (5 packages loaded)
Analyzing: target //:docker (6 packages loaded)
Analyzing: target //:docker (76 packages loaded)
Analyzing: target //:docker (78 packages loaded)
Analyzing: target //:docker (78 packages loaded)
Analyzing: target //:docker (79 packages loaded)
Analyzing: target //:docker (79 packages loaded)
Analyzing: target //:docker (80 packages loaded)
Analyzing: target //:docker (160 packages loaded)
Analyzing: target //:docker (160 packages loaded)
Analyzing: target //:docker (160 packages loaded)
Analyzing: target //:docker (160 packages loaded)
Analyzing: target //:docker (174 packages loaded)
Analyzing: target //:docker (174 packages loaded)
Analyzing: target //:docker (211 packages loaded)
INFO: Analysed target //:docker (286 packages loaded).
INFO: Found 1 target...
[0 / 1] BazelWorkspaceStatusAction stable-status.txt
[17 / 19] SHA256 external/golang/image/002.tar.gz.nogz.sha256
[42 / 281] no action running
[51 / 290] no action running
[63 / 302] no action running
[80 / 319] no action running
[101 / 340] no action running
[118 / 358] no action running
[138 / 378] no action running
[168 / 407] Compiling external/com_github_google_protobuf/src/google/protobuf/compiler/csharp/csharp_message_field.cc [for host]; 0s linux-sandbox
[200 / 439] Compiling external/com_github_google_protobuf/src/google/protobuf/extension_set.cc [for host]; 1s linux-sandbox
INFO: From ProtoCompile assets/assets/assets.pb.gw.go:
assets/assets/assets.proto: warning: Import recipes.proto but not used.
INFO: From ProtoCompile assets/assets/assets.swagger.json:
assets/assets/assets.proto: warning: Import recipes.proto but not used.
INFO: From ProtoCompile assets/assets/assets.pb.go:
assets/assets/assets.proto: warning: Import recipes.proto but not used.
[522 / 697] GoCompile vendor/github.com/gorilla/mux/~lib~/go_default_library.o; 0s linux-sandbox
Target //:docker up-to-date:
bazel-bin/docker-layer.tar
INFO: Elapsed time: 331.343s, Critical Path: 13.73s
INFO: Build completed successfully, 737 total actions

EDIT: add cloudbuild file

cloudbuild.yaml:

steps:
- name: gcr.io/cloud-builders/bazel
  args: ['run', '//:docker']
- name: gcr.io/cloud-builders/docker
  args: ['tag', 'bazel:docker', '${_IMAGE_TAG}']
images: ['${_IMAGE_TAG}']
David Bendory
  • 1,258
  • 8
  • 14
fabrizioM
  • 46,639
  • 15
  • 102
  • 119

2 Answers2

5

Container Builder doesn't offer this as a built-in at this time, but you can do it yourself.

  1. Add one or more volumes to the build step(s) that output information that you want to restore in a later build. (See documentation on volumes.)
  2. If necessary, modify those build steps to output the data you want to preserve into the path provided by the volume.
  3. In a subsequent build step, use the gsutil build step to save all the data in the volume to a GCS bucket. (You can of course use some other utility to save it to some other place if you wish.)

In a later build, use gsutil to restore the data to the desired volume, and mount the volume into a subsequent build step.

David Bendory
  • 1,258
  • 8
  • 14
  • David - Can you share an example cloudbuild.yaml file that does this? – Eric Hauser Aug 28 '18 at 17:24
  • 2
    Cool idea--I'd be very interested to see the results. But, it might not offer much benefit. Bazel does a lot when it bootstraps, not all of which is saved to disk. You can find bottlenecks by [profiling](https://docs.bazel.build/versions/master/skylark/performance.html#performance-profiling). Are you already using a [remote cache](https://docs.bazel.build/versions/master/remote-caching.html)? If so, most of the relevant disk contents will be distributed. Beyond that, an easy thing to try is [bigger Cloud Build VMs](https://cloud.google.com/cloud-build/docs/speeding-up-builds). – davidstanke Aug 29 '18 at 23:50
  • How do you handle workspace sharing between runs? We can run multiple bazel jobs for different users and different branches. We can even run the same commit in parallel by the same user. – Skyblade Apr 22 '20 at 12:25
2

There's an even easier way available now using Bazel Remote Caching.

Create a Cloud Storage bucket, then set the following in your .bazelrc:

build --remote_cache=https://storage.googleapis.com/<your bucket name>
build --google_default_credentials

Bazel will automatically cache actions in your Cloud Storage bucket, and they persist between builds. You can set automatic lifecycle management on your storage bucket to delete actions after a certain number of days.

  • This worked for me to cache the portions where Bazel actual starts building the code, however for some reason the Bazel step takes a long time to setup and even get to that part. – Christian Gossain Oct 09 '22 at 16:24