Search code examples
cachinggoogle-cloud-platformbazelgoogle-cloud-build

Google Cloud Build - How to Cache Bazel?


I recently started using Cloud Build with Bazel. So I have a basic cloudbuild.yaml

steps:
  - id: 'run unit tests'
    name: gcr.io/cloud-builders/bazel
    args: ['test', '//...']

which runs all tests of my Bazel project.

But as you can see from this screenshot, every build takes around 4 minutes, although I haven't touched any code which would affect my tests.

build duration

Locally running the tests for the first time takes about 1 minute. But running the tests a second time, with the help of Bazels cache, it takes only a few seconds.

So my goal is to use the Bazel cache with Google Cloud Build

Update

As suggested by Thierry Falvo I'v looked into those recommendations. An thus I tried to the add the following to my cloudbuild.yaml:

steps:
  - name: gcr.io/cloud-builders/gsutil
    args: ['cp', 'gs://cents-ideas-build-cache/bazel-bin', 'bazel-bin']

  - id: 'run unit tests'
    name: gcr.io/cloud-builders/bazel
    args: ['test', '//...']

  - name: gcr.io/cloud-builders/gsutil
    args: ['cp', 'bazel-bin', 'gs://cents-ideas-build-cache/bazel-bin']

Although I created the bucket and folder, I get this error:

CommandException: No URLs matched

cloud build error


Solution

  • I think that rather than cache discrete results (artifacts), you want to use GCS (cloud storage) as a bazel remote cache.

    - name: gcr.io/cloud-builders/bazel
      args: ['test', '--remote_cache=https://storage.googleapis.com/<bucketname>', '--google_default_credentials', '--test_output=errors', '//...']