Build Quality Microservice with gRPC and GitHub Workflows: Hands on Guide (Part 2)
The path to an amazing software supply chain. It only takes few minutes
Everything mentionned in this article is available on this repository https://github.com/chaisarfati/UserMicroservice
In the first part of this gRPC guide, we set up a repository containing a Python application that implements a gRPC service definition. We created a GitHub workflow (pipeline) that generates interface code from the protobuf, compiles the code, tests it, and commits the generated code to the repository.
In this second part, we’ll define a Dockerfile and add it to our repository. Then, we’ll add a job to our GitHub workflow that builds an image from this Dockerfile and pushes it to GitHub’s Container Registry.
Step 1: Defining the Dockerfile
In this Dockerfile:
- We start with a lightweight Python 3.9 image.
- We copy our service files into the container.
- We install all dependencies (including gRPC-related dependencies).
- We expose the gRPC port (50051 in this case) so that other services can communicate with it.
- Finally, we run our Python gRPC service using the command
python server.py
.
Step 2: Adding a Docker Build Job to the GitHub Workflow
Next, we’ll add a new job to our GitHub workflow that will build and push our Docker image to GitHub’s Container Registry. Here’s the updated workflow:
Breaking down the job:
- Checkout the Code: The first step checks out the code from the current branch, which is determined by the branch name we defined in the
init
job. - Set Up Docker Buildx: This action sets up Docker Buildx, a tool for building Docker images that supports multi-platform builds.
- Log in to GitHub Container Registry: This step logs in to GitHub’s Container Registry using the secret stored in
GHPR_TOKEN
, allowing us to push the image to GitHub. - Sanitize Repository and Branch Names: Here, we make sure the repository and branch names are lowercase and properly formatted to create valid Docker tags.
- Build and Push the Docker Image: Finally, we build the Docker image using the Dockerfile we defined earlier, tag it using the branch name and short commit SHA, and push it to GitHub’s Container Registry.
Step 3: Verifying the Build
Once this pipeline is complete, you’ll see your Docker image in the GitHub Container Registry, tagged with the branch name and commit SHA such as ghcr.io/<repository_owner>/<repository_name>:<branch_name>.<commit_sha>.docker
If we go to the repository ‘Packages’ tag and we clear the filters, we should see this:
That’s all for this part. In the next article, we’ll move on to packaging the microservice for deployment using Helm, an increasingly popular tool for Kubernetes package management.
Stay tuned!