A Bazel scaffold template for golang / protobuf+grpc mono repository.
- Multi
golangmicroservices in the mono repo - Single build pipeline
- Shared
golanglibrary - Shared
grpc/protobuflibrary - Automatic release GitHub workflow action
- macOS and Linux builds
- A cli
ServiceAcallingServiceBgrpc methodSum(int32, inte32) - A
ServiceBserving theSummethod over grpc.
Run the complete build pipeline using:
bazel build //...Note: The first build will take a moment. No worries, you will see the Bazel mono repo benefits later.
In one terminal, run the grpc serving ServiceA:
bazel run services/servicea :42042In another terminal, run the client ServiceB:
bazel run services/serviceb :42042You should see the result. Protobuf + grpc built, services binaries built as well.
The built binaries are in the /bazel-bin directory in their respective sub
directories.
There is a little caveat with proto when using Bazel. The golang generated proto
files are in the bazel-bin build folder (as they are result of the build, not a
source). So they are not accessible to your IDE. There is an official issue.
Until solved, the workaround is:
-
For every proto service:
-
Run Bazel to generate the proto
golangimplementation:bazel run //proto/[service]
-
Manually copy generated implementation back to the
proto/[service]directory:cp ./bazel-bin/proto/[service]/[service]_go_proto_/github.com/jankremlacek/go-bazel/proto/[service]/*.pb.go ./proto/[service]/ -
Also, you have to exclude the copied file from the Bazel build. Create file
/proto/[service]/.bazelignoreand put there all generated[filename].pb.gofiles.
Note: Of course, this process can be automated.
- Build
BUILD.bazelbuild files using Gazelle:bazel run //:gazelle
- Update Gazelle
golangdependencies using:bazel run //:gazelle-update-repos
- Test whole pipeline using:
bazel test //...