13. Use Bazel’s cloud features#

Caution

This section is optional. Feel free to skip to the next one if you’re impatient to try the air quality app!

One of Bazel’s defining features is that it’s a cloud build system. Team members can easily share artifacts (including test logs), builds can be run in parallel on hundreds of machines in the cloud, and build artifacts that were built once (by anyone) don’t need to be rebuilt from scratch.

This section gives you a taste of these features of Bazel using BuildBuddy.

BuildBuddy setup#

To use cloud features, you need to get set up with some cloud provider.

Note

Googlers: see additional instructions at go/pw-sense-googlers.

  1. Go to https://app.buildbuddy.io/ and log in via Google or GitHub.

  2. Click Quickstart Guide.

  3. In 1. Configure your .bazelrc enable the following options:

    • API Key

    • Enable cache

    • Full cache

    Caution

    b/364781685: Sense does not support remote execution yet, so don’t enable that option.

  4. Copy the provided snippet to your .bazelrc.

Review and share logs#

Let’s go back to what we learned in 5. Run host tests and run a test.

  1. Open //modules/blinky/blinky_test.cc.

  2. Make the Toggle test fail by changing one of the expected values. Example:

    TEST_F(BlinkyTest, Toggle) {
      // ...
      auto event = FirstActive();
      ASSERT_NE(event, monochrome_led_.events().end());
      EXPECT_EQ(event->state, State::kInactive);   // add this line
      // EXPECT_EQ(event->state, State::kActive);  // comment out this line
      EXPECT_GE(ToMs(event->timestamp - start), kIntervalMs * 0);
      start = event->timestamp;
      // ...
    }
    

    Caution

    Remember to save your changes!

    In Bazel Build Targets expand //modules/blinky, then right-click :blinky_test (cc_test), then select Test target.

    Selecting Test target

    Starting blinky_test#

    A task launches a terminal. You should see blinky_test fail, and a BuildBuddy invocation link printed:

    //modules/blinky:blinky_test         FAILED in 0.4s
    INFO: Streaming build results to: https://app.buildbuddy.io/invocation/f8bc4845-a38d-4c62-b939-14238168ba46
    

    Run the following command. You should see output similar to what’s shown after the command. The key line starts with INFO: Streaming build results to:. It contains the BuildBuddy invocation link.

    $ bazelisk test //modules/blinky:blinky_test
    # ...
    //modules/blinky:blinky_test         FAILED in 0.4s
      /home/kayce/.cache/bazel/_bazel_kayce/27fcdd448f61589ce2692618b3237728/execroot/showcase-rp2/bazel-out/k8-fastbuild/testlogs/modules/blinky/blinky_test/test.log
    
    Executed 1 out of 1 test: 1 fails locally.
    INFO: Streaming build results to: https://app.buildbuddy.io/invocation/f8bc4845-a38d-4c62-b939-14238168ba46
    
  3. Click the provided link. Some highlights to notice:

    • The full log of the failed test.

    • The command line invocation used to generate it.

    • A timing profile showing how long Bazel took to build and execute the test.

  4. Click “Share” (in the top-right corner) and broaden the permissions to make the invocation link shareable. Then show it off to your friends and coworkers!

Remote caching#

Bazel supports remote caching: if you (or someone else in your organization) already built an artifact, you can simply retrieve it from the cache instead of building it from scratch. Let’s test it out.

  1. Remove all local Bazel build results.

    $ bazelisk clean
    
  2. Run blinky_test again. It should be quite fast, even though you discarded all the build artifacts. Bazel simply downloads them from the remote cache that your previous invocation populated!

    You can click the CACHE tab in the BuildBuddy invocation UI to see more details on cache performance (how many hits there were, how much data was uploaded and downloaded, etc).

Summary#

In this section, you’ve gotten a taste of the cloud features of Bazel: generating easily shareable URLs for build and test invocations, and speeding up your builds by leveraging remote caching.

Next, head over to 14. Run the air quality monitor app to try out the air quality monitoring application.