Auto-generating gRPC files with GitHub Actions

Joel Renk
3 min readMay 15, 2023

Welcome to our latest blog post, where we explore the fascinating world of gRPC and GitHub Actions! In recent years, gRPC has emerged as a popular framework for building high-performance APIs, thanks to its ability to generate efficient, type-safe code in a range of programming languages. However, manually generating gRPC files for each language can be a time-consuming and error-prone process. That’s where GitHub Actions come in — these powerful automation tools allow you to streamline your workflow and automate repetitive tasks, including the generation of gRPC files. In this article, we’ll show you how to use GitHub Actions to auto-generate gRPC files for multiple languages, saving you time and effort in the process. So whether you’re a seasoned gRPC developer or just starting out, read on to discover how to take your workflow to the next level with GitHub Actions!

To ensure optimal organization, it is advisable to create a single repository that houses all proto files, as well as a repository for each desired output language.

The proto repository should contain a “proto” directory that includes subdirectories for each service, where respective proto files can be stored.

|
-- proto
|
-- exampleService
|
-- exampleService.proto

It is highly recommended to add comments for each service, function, and field in your proto files, as this can improve code clarity and facilitate maintenance.

syntax = "proto3";

package exampleService;

option go_package = "github.com/{yourOrganization}/grpc-go/exampleService";

// ExampleService exposes the functionality to do Foo
service ExampleService {
// FooFunction will do stuff to return baz
rpc FooFunction(FooRequest) returns (FooResponse) {}
}

// FooRequest will be the given data on FooFunction
message FooRequest {
// bar contains the identifier of the data
string bar = 1;
}

// FooResponse will be returned on FooFunction
message FooResponse {
// baz is the value of the data
string baz = 1;
}

To automate the gRPC file generation process a GitHub Actions pipeline file should be added. The repository structure should be arranged to accommodate this pipeline, as demonstrated below.

|
-- .github
|
-- workflows
|
-- proto.yaml
|
-- proto

By leveraging GitHub Actions, you can generate gRPC files for multiple output languages and automatically push them to their designated repositories. While the example below employs Golang as the output language, you can easily incorporate other languages as needed.

name: Build and push gRPC files

on:
push:
branches:
- main

jobs:
setup-build-push-deploy:
name: Setup, Build, and Push
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Install Protoc
uses: arduino/setup-protoc@v1
with:
version: '3.x'
include-pre-releases: false

- name: Install Protoc Plugins
run: |-
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

# The following steps would be needed for each language
- name: Build Go
run: |-
mkdir out
protoc --proto_path=./proto --go_out=out --go-grpc_out=out --go-grpc_opt=paths=source_relative --go_opt=paths=source_relative $(find ./proto -iname "*.proto")

- name: Push Go
uses: cpina/github-action-push-to-another-repository@main
env:
SSH_DEPLOY_KEY: ${{ secrets.SSH_DEPLOY_KEY }}
with:
source-directory: 'out'
destination-github-username: '{yourOrganization}'
destination-repository-name: 'grpc-go'
target-branch: main

To enable pushing into an external repository, you must follow the steps outlined here.

Your go.mod file can now reference the repository github.com/{yourOrganization}/grpc-go.

At Teratis Solutions GmbH, we emphasize the importance of staying up-to-date with the latest technologies and trends in software development. By leveraging powerful tools like gRPC and GitHub Actions, we can streamline our workflows, automate repetitive tasks, and ultimately deliver better results for our clients. Whether you need custom software solutions, cloud infrastructure support, or expert consulting services, we’re here to help. Visit us at https://teratis.de today to learn more about how we can help your business thrive in the digital age!

--

--