Skip to content

Commit 9ce7615

Browse files
authored
Merge pull request #2 from jankremlacek/dev-gprc
gprc/protobuf
2 parents 63a4c13 + 147af78 commit 9ce7615

File tree

20 files changed

+901
-89
lines changed

20 files changed

+901
-89
lines changed

.github/workflows/release.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ jobs:
6767
- name: Prepare the dist folder
6868
run: |
6969
mkdir -p "${GITHUB_WORKSPACE}/dist/"
70-
cp "${GITHUB_WORKSPACE}/bazel-bin/services/a/service-a" "${GITHUB_WORKSPACE}/dist/"
71-
cp "${GITHUB_WORKSPACE}/bazel-bin/services/b/service-b" "${GITHUB_WORKSPACE}/dist/"
70+
cp "${GITHUB_WORKSPACE}/bazel-bin/services/servicea/servicea" "${GITHUB_WORKSPACE}/dist/"
71+
cp "${GITHUB_WORKSPACE}/bazel-bin/services/serviceb/serviceb" "${GITHUB_WORKSPACE}/dist/"
7272
7373
- name: Compress
7474
run: |
7575
tar czvf ${{matrix.name}}.tar.gz --directory "${GITHUB_WORKSPACE}/dist/" \
76-
service-a \
77-
service-b
76+
servicea \
77+
serviceb
7878
7979
- name: Upload the build as an artifact
8080
uses: actions/upload-artifact@v3

BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,3 @@ gazelle(
1616

1717
# make sure that others can import either using the import or go_default_library naming conventions
1818
# gazelle:go_naming_convention import_alias
19-
20-
# gazelle:proto disable_global

README.md

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,97 @@
11
# go-bazel
22

3-
A sample scaffold golang + Bazel mono repository.
3+
A Bazel scaffold template for `golang` / `protobuf+grpc` mono repository.
44

55
## Features
66

7-
- Multi microservices in the mono repo
7+
- Multi `golang` microservices in the mono repo
88
- Single build pipeline
9+
- Shared `golang` library
10+
- Shared `grpc/protobuf` library
911
- Automatic release GitHub workflow action
1012
- macOS and Linux builds
1113

12-
## CLI
14+
## A hypothetical setup
1315

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.
1718

18-
```sh
19-
bazel run //:gazelle-update-repos
20-
```
19+
### Step 1: Install Bazel
2120

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:
2527

2628
```sh
2729
bazel build //...
2830
```
2931

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+
3038
```sh
31-
bazel run services/a
39+
bazel run services/serviceb :42042
3240
```
3341

42+
In another terminal, run the client `ServiceA`:
43+
3444
```sh
35-
bazel run services/b
45+
bazel run services/servicea :42042
3646
```
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)

WORKSPACE

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,19 @@ http_archive(
2020
],
2121
)
2222

23+
http_archive(
24+
name = "com_google_protobuf",
25+
sha256 = "d0f5f605d0d656007ce6c8b5a82df3037e1d8fe8b121ed42e536f569dec16113",
26+
strip_prefix = "protobuf-3.14.0",
27+
urls = [
28+
"https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz",
29+
"https://github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz",
30+
],
31+
)
32+
2333
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
2434
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
35+
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")
2536

2637
############################################################
2738
# Define your own dependencies here using go_repository.
@@ -41,3 +52,5 @@ go_rules_dependencies()
4152
go_register_toolchains(version = "1.19.1")
4253

4354
gazelle_dependencies()
55+
56+
protobuf_deps()

0 commit comments

Comments
 (0)