|
1 | 1 | # go-bazel |
2 | 2 |
|
3 | | -A sample scaffold golang + Bazel mono repository. |
| 3 | +A Bazel scaffold template for `golang` / `protobuf+grpc` mono repository. |
4 | 4 |
|
5 | 5 | ## Features |
6 | 6 |
|
7 | | -- Multi microservices in the mono repo |
| 7 | +- Multi `golang` microservices in the mono repo |
8 | 8 | - Single build pipeline |
| 9 | +- Shared `golang` library |
| 10 | +- Shared `grpc/protobuf` library |
9 | 11 | - Automatic release GitHub workflow action |
10 | 12 | - macOS and Linux builds |
11 | 13 |
|
12 | | -## CLI |
| 14 | +## A hypothetical setup |
13 | 15 |
|
14 | | -```sh |
15 | | -bazel run //:gazelle |
16 | | -``` |
| 16 | +- A cli `ServiceA` calling `ServiceB` grpc method `Sum(int32, inte32)` |
| 17 | +- A `ServiceB` serving the `Sum` method over grpc. |
17 | 18 |
|
18 | | -```sh |
19 | | -bazel run //:gazelle-update-repos |
20 | | -``` |
| 19 | +### Step 1: Install Bazel |
21 | 20 |
|
22 | | -```sh |
23 | | -bazel test //... --test_output=errors --nocache_test_results |
24 | | -``` |
| 21 | +- **macOS**: [https://bazel.build/install/os-x](https://bazel.build/install/os-x) |
| 22 | +- **Ubuntu**: [https://bazel.build/install/ubuntu](https://bazel.build/install/ubuntu) |
| 23 | + |
| 24 | +### Step 2: Build it |
| 25 | + |
| 26 | +Run the complete build pipeline using: |
25 | 27 |
|
26 | 28 | ```sh |
27 | 29 | bazel build //... |
28 | 30 | ``` |
29 | 31 |
|
| 32 | +> **Note:** The first build will take a moment. No worries, you will see the Bazel mono repo benefits later. |
| 33 | +
|
| 34 | +### Step 3: Run to see the result |
| 35 | + |
| 36 | +In one terminal, run the grpc serving `ServiceB`: |
| 37 | + |
30 | 38 | ```sh |
31 | | -bazel run services/a |
| 39 | +bazel run services/serviceb :42042 |
32 | 40 | ``` |
33 | 41 |
|
| 42 | +In another terminal, run the client `ServiceA`: |
| 43 | + |
34 | 44 | ```sh |
35 | | -bazel run services/b |
| 45 | +bazel run services/servicea :42042 |
36 | 46 | ``` |
| 47 | + |
| 48 | +You should see the result. Protobuf + grpc built, services binaries built as well. |
| 49 | +The built binaries are in the `/bazel-bin` directory in their respective sub |
| 50 | +directories. |
| 51 | + |
| 52 | +## Protobuf/grpc caveats |
| 53 | + |
| 54 | +There is a little caveat with proto when using Bazel. The `golang` generated proto |
| 55 | +files are in the `bazel-bin` build folder (as they are result of the build, not a |
| 56 | +source). So they are not accessible to your IDE. [There is an official issue](https://github.com/bazelbuild/rules_go/issues/512). |
| 57 | +Until solved, the workaround is: |
| 58 | + |
| 59 | +- For every proto service: |
| 60 | +- Run Bazel to generate the proto `golang` implementation: |
| 61 | + |
| 62 | + ```sh |
| 63 | + bazel run //proto/[service] |
| 64 | + ``` |
| 65 | + |
| 66 | +- Manually copy generated implementation back to the `proto/[service]` directory: |
| 67 | + |
| 68 | + ```sh |
| 69 | + cp ./bazel-bin/proto/[service]/[service]_go_proto_/github.com/kikotxyz/babykktd/proto/[service]/*.pb.go ./proto/[service]/ |
| 70 | + ``` |
| 71 | + |
| 72 | +- Also, you have to exclude the copied file from the Bazel build. Create file `/proto/[service]/.bazelignore` and put there all generated `[filename].pb.go` files. |
| 73 | + |
| 74 | +> **Note:** Of course, this process can be automated. |
| 75 | +
|
| 76 | +## Additonal commands |
| 77 | + |
| 78 | +- Build `BUILD.bazel` build files using [Gazelle](https://github.com/bazelbuild/bazel-gazelle): |
| 79 | + ```sh |
| 80 | + bazel run //:gazelle |
| 81 | + ``` |
| 82 | +- Update Gazelle `golang` dependencies using: |
| 83 | + ```sh |
| 84 | + bazel run //:gazelle-update-repos |
| 85 | + ``` |
| 86 | +- Test whole pipeline using: |
| 87 | + ```sh |
| 88 | + bazel test //... |
| 89 | + ``` |
| 90 | + |
| 91 | +--- |
| 92 | + |
| 93 | +## Additional resources |
| 94 | + |
| 95 | +- [Bazel official website](https://bazel.build/) |
| 96 | +- [Bazel at GitHub](https://github.com/bazelbuild/bazel) |
| 97 | +- [Gazelle at GitHub](https://github.com/bazelbuild/bazel-gazelle) |
0 commit comments