Skip to content

Commit

Permalink
Merge pull request #2 from jankremlacek/dev-gprc
Browse files Browse the repository at this point in the history
gprc/protobuf
  • Loading branch information
jankremlacek authored Sep 30, 2022
2 parents 63a4c13 + 147af78 commit 9ce7615
Show file tree
Hide file tree
Showing 20 changed files with 901 additions and 89 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ jobs:
- name: Prepare the dist folder
run: |
mkdir -p "${GITHUB_WORKSPACE}/dist/"
cp "${GITHUB_WORKSPACE}/bazel-bin/services/a/service-a" "${GITHUB_WORKSPACE}/dist/"
cp "${GITHUB_WORKSPACE}/bazel-bin/services/b/service-b" "${GITHUB_WORKSPACE}/dist/"
cp "${GITHUB_WORKSPACE}/bazel-bin/services/servicea/servicea" "${GITHUB_WORKSPACE}/dist/"
cp "${GITHUB_WORKSPACE}/bazel-bin/services/serviceb/serviceb" "${GITHUB_WORKSPACE}/dist/"
- name: Compress
run: |
tar czvf ${{matrix.name}}.tar.gz --directory "${GITHUB_WORKSPACE}/dist/" \
service-a \
service-b
servicea \
serviceb
- name: Upload the build as an artifact
uses: actions/upload-artifact@v3
Expand Down
2 changes: 0 additions & 2 deletions BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,3 @@ gazelle(

# make sure that others can import either using the import or go_default_library naming conventions
# gazelle:go_naming_convention import_alias

# gazelle:proto disable_global
89 changes: 75 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,36 +1,97 @@
# go-bazel

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

## Features

- Multi microservices in the mono repo
- Multi `golang` microservices in the mono repo
- Single build pipeline
- Shared `golang` library
- Shared `grpc/protobuf` library
- Automatic release GitHub workflow action
- macOS and Linux builds

## CLI
## A hypothetical setup

```sh
bazel run //:gazelle
```
- A cli `ServiceA` calling `ServiceB` grpc method `Sum(int32, inte32)`
- A `ServiceB` serving the `Sum` method over grpc.

```sh
bazel run //:gazelle-update-repos
```
### Step 1: Install Bazel

```sh
bazel test //... --test_output=errors --nocache_test_results
```
- **macOS**: [https://bazel.build/install/os-x](https://bazel.build/install/os-x)
- **Ubuntu**: [https://bazel.build/install/ubuntu](https://bazel.build/install/ubuntu)

### Step 2: Build it

Run the complete build pipeline using:

```sh
bazel build //...
```

> **Note:** The first build will take a moment. No worries, you will see the Bazel mono repo benefits later.
### Step 3: Run to see the result

In one terminal, run the grpc serving `ServiceB`:

```sh
bazel run services/a
bazel run services/serviceb :42042
```

In another terminal, run the client `ServiceA`:

```sh
bazel run services/b
bazel run services/servicea :42042
```

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

## Protobuf/grpc caveats

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](https://github.com/bazelbuild/rules_go/issues/512).
Until solved, the workaround is:

- For every proto service:
- Run Bazel to generate the proto `golang` implementation:

```sh
bazel run //proto/[service]
```

- Manually copy generated implementation back to the `proto/[service]` directory:

```sh
cp ./bazel-bin/proto/[service]/[service]_go_proto_/github.com/kikotxyz/babykktd/proto/[service]/*.pb.go ./proto/[service]/
```

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

> **Note:** Of course, this process can be automated.
## Additonal commands

- Build `BUILD.bazel` build files using [Gazelle](https://github.com/bazelbuild/bazel-gazelle):
```sh
bazel run //:gazelle
```
- Update Gazelle `golang` dependencies using:
```sh
bazel run //:gazelle-update-repos
```
- Test whole pipeline using:
```sh
bazel test //...
```

---

## Additional resources

- [Bazel official website](https://bazel.build/)
- [Bazel at GitHub](https://github.com/bazelbuild/bazel)
- [Gazelle at GitHub](https://github.com/bazelbuild/bazel-gazelle)
13 changes: 13 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,19 @@ http_archive(
],
)

http_archive(
name = "com_google_protobuf",
sha256 = "d0f5f605d0d656007ce6c8b5a82df3037e1d8fe8b121ed42e536f569dec16113",
strip_prefix = "protobuf-3.14.0",
urls = [
"https://mirror.bazel.build/github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz",
"https://github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz",
],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies")
load("@com_google_protobuf//:protobuf_deps.bzl", "protobuf_deps")

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

gazelle_dependencies()

protobuf_deps()
Loading

0 comments on commit 9ce7615

Please sign in to comment.