• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1.. _showcase-sense-tutorial-bazel_cloud:
2
3==============================
413. Use Bazel's cloud features
5==============================
6One of Bazel's defining features is that it's a cloud build system. Team
7members can easily `share artifacts
8<https://app.buildbuddy.io/invocation/f8bc4845-a38d-4c62-b939-14238168ba46>`__
9(including test logs), builds can be run in parallel on hundreds of machines in
10the cloud, and build artifacts that were built once (by anyone) don't need to
11be rebuilt from scratch.
12
13This section gives you a taste of these features of Bazel using `BuildBuddy
14<https://www.buildbuddy.io/>`_.
15
16----------------
17BuildBuddy setup
18----------------
19To use cloud features, you need to get set up with some cloud provider.
20
21.. note::
22
23   Googlers: see additional instructions at `go/pw-sense-googlers
24   <http://go/pw-sense-googlers#buildbuddy-integration>`_.
25
26#. Go to https://app.buildbuddy.io/ and log in via Google or GitHub.
27#. Click `Quickstart Guide <https://app.buildbuddy.io/docs/setup/>`__.
28#. In step 1 (**Configure your .bazelrc**) enable the following options:
29
30   * **API Key**
31   * **Enable cache**
32   * **Full cache**
33
34   .. caution::
35
36      :bug:`364781685`: Sense does not support remote execution yet, so don't
37      enable that option.
38
39#. Copy the provided snippet to ``//.bazelrc`` (the ``.bazelrc`` file in the root
40   directory of your Sense repository).
41
42   .. figure:: https://storage.googleapis.com/pigweed-media/sense/buildbuddy_bazelrc_v1.png
43
44#. Try :ref:`building the blinky app <showcase-sense-tutorial-build>` again to verify your
45   BuildBuddy integration. While the build runs you should see an information log indicating
46   that the build is streaming to BuildBuddy, like this:
47
48   .. code-block:: text
49
50      INFO: Streaming build results to: https://app.buildbuddy.io/invocation/6d467374-ffad-44be-a6be-e4f7b53129dd
51
52---------------------
53Review and share logs
54---------------------
55Let's go back to what we learned in :ref:`showcase-sense-tutorial-hosttests` and run a test.
56
57#. Open ``//modules/blinky/blinky_test.cc``.
58
59#. Make the ``Toggle`` test fail by changing one of the expected values.
60   Example:
61
62   .. code-block:: c++
63
64      TEST_F(BlinkyTest, Toggle) {
65        // ...
66        auto event = FirstActive();
67        ASSERT_NE(event, monochrome_led_.events().end());
68        EXPECT_EQ(event->state, State::kInactive);   // add this line
69        // EXPECT_EQ(event->state, State::kActive);  // comment out this line
70        EXPECT_GE(ToMs(event->timestamp - start), kIntervalMs * 0);
71        start = event->timestamp;
72        // ...
73      }
74
75   .. caution:: Remember to save your changes!
76   .. tab-set::
77
78      .. tab-item:: VS Code
79         :sync: vsc
80
81         In **Bazel Build Targets** expand **//modules/blinky**, then right-click
82         **:blinky_test (cc_test)**, then select **Test target**.
83
84         .. figure:: https://storage.googleapis.com/pigweed-media/sense/20240802/test_target_v2.png
85            :alt: Selecting Test target
86
87            Starting ``blinky_test``
88
89         A task launches a terminal. You should see ``blinky_test`` fail, and a
90         BuildBuddy invocation link printed:
91
92         .. code-block:: console
93
94            //modules/blinky:blinky_test         FAILED in 0.4s
95            INFO: Streaming build results to: https://app.buildbuddy.io/invocation/f8bc4845-a38d-4c62-b939-14238168ba46
96
97      .. tab-item:: CLI
98         :sync: cli
99
100         Run the following command. You should see output similar to what's
101         shown after the command. The key line starts with ``INFO: Streaming
102         build results to:``. It contains the BuildBuddy invocation link.
103
104         .. code-block:: console
105
106            $ bazelisk test //modules/blinky:blinky_test
107            # ...
108            //modules/blinky:blinky_test         FAILED in 0.4s
109              /home/kayce/.cache/bazel/_bazel_kayce/27fcdd448f61589ce2692618b3237728/execroot/showcase-rp2/bazel-out/k8-fastbuild/testlogs/modules/blinky/blinky_test/test.log
110
111            Executed 1 out of 1 test: 1 fails locally.
112            INFO: Streaming build results to: https://app.buildbuddy.io/invocation/f8bc4845-a38d-4c62-b939-14238168ba46
113
114#. Click the provided BuildBuddy link. Some highlights to notice:
115
116   * The **LOGS** tab provides a full log of the failed test.
117   * The **DETAILS** tab shows the exact command line invocation for reproducing this result.
118   * The **TIMING** tab shows a granular breakdown of how long Bazel took to build and execute the test.
119
120#. Click **Share** (top-right corner) and broaden the permissions to make
121   the invocation link shareable. Then show it off to your friends and
122   coworkers!
123
124--------------
125Remote caching
126--------------
127Bazel supports remote caching: if you (or someone else in your organization)
128already built an artifact, you can simply retrieve it from the cache instead of
129building it from scratch. Let's test it out.
130
131
132#. Remove all local Bazel build results.
133
134   .. code-block:: console
135
136      $ bazelisk clean
137
138#. Run ``blinky_test`` again. It should be quite fast, even though you
139   discarded all the build artifacts. Bazel simply downloads them from the
140   remote cache that your previous invocation populated!
141
142   You can click the **CACHE** tab in the BuildBuddy invocation UI to see more
143   details on cache performance: how many hits there were, how much data was
144   uploaded and downloaded, etc.
145
146-------
147Summary
148-------
149In this section, you've gotten a taste of the cloud features of Bazel:
150generating easily shareable URLs for build and test invocations, and speeding
151up your builds by leveraging remote caching.
152
153Next, head over to :ref:`showcase-sense-tutorial-prod` to try
154out the ``production`` app that exercises more of the Enviro+ Pack hardware.
155