如何在Google Cloud Platform上加速bazel构建

时间:2017-09-27 21:02:11

标签: docker google-cloud-platform bazel google-container-builder

我正在使用gcr.io/cloud-builders/bazel在Google容器构建中构建我的图片。

从日志中看,它似乎花费大部分时间为bazel设置工作区。此工作空间不会从构建更改为构建,因此我认为可以预先计算并存储在新图像中。

如何在Google Cloud Platform上加速这些bazel构建?

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

编辑:添加cloudbuild文件

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}']

2 个答案:

答案 0 :(得分:4)

Container Builder目前不提供内置功能,但你可以自己动手。

  1. 将一个或多个volumes添加到构建步骤,该步骤输出要在以后的构建中还原的信息。 (见documentation on volumes。)
  2. 如有必要,请修改这些构建步骤,以将要保留的数据输出到卷提供的路径中。
  3. 在后续构建步骤中,使用gsutil build step将卷中的所有数据保存到GCS存储桶。 (如果您愿意,您当然可以使用其他一些实用工具将其保存到其他地方。)
  4. 在以后的版本中,使用gsutil将数据恢复到所需的卷,然后将卷装入后续的构建步骤。

答案 1 :(得分:0)

现在有一种更简单的方法可以使用 Bazel Remote Caching

Create a Cloud Storage bucket,然后在您的 .bazelrc 中设置以下内容:

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

Bazel 会自动将操作缓存在您的 Cloud Storage 存储桶中,并且它们会在构建之间持续存在。您可以set automatic lifecycle management在您的存储桶上在一定天数后删除操作。