CI/CD for a Go gRPC Service with GitHub Actions
Generate stubs, test, and build a static binary for your Go gRPC service.
A Go gRPC service compiles protobufs into Go stubs and serves over HTTP/2. This recipe generates and lints the protos with buf, runs tests, builds a static binary, and ships a container.
What the pipeline does
- set up Go and buf
- lint and generate stubs with buf
- vet and test with go vet and go test
- build a static binary with CGO_ENABLED=0
- push a container image on main
The workflow
buf generate produces the Go and gRPC stubs from your protos; the build then compiles a static binary you copy into a minimal container.
name: CI
on:
push:
branches: [main]
pull_request:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: '1.22'
cache: true
- uses: bufbuild/buf-action@v1
with:
setup_only: true
- run: buf lint
- run: buf generate
- run: go vet ./...
- run: go test ./...
- run: CGO_ENABLED=0 go build -o server ./cmd/server
- uses: actions/upload-artifact@v4
with:
name: server
path: serverCaching and speed
setup-go with cache: true restores the build and module cache keyed on go.sum. gRPC services build fast, so most CI time is tests; cheaper managed runners such as Latchkey keep frequent runs inexpensive and auto-retry transient module-proxy failures.
Deploying
Copy the static binary into a distroless container, push to GHCR or ECR, and deploy to Kubernetes or ECS. Expose the gRPC port and add a gRPC health check; put an L7 proxy (Envoy) in front if you need gRPC-Web for browsers.
Key takeaways
- buf lint and buf generate keep protos and stubs in sync.
- Build with CGO_ENABLED=0 for a portable static binary.
- Add a gRPC health check for orchestrator probes.