Go CI/CD Playground
This repository is a playground for experimenting with CI/CD in Go. It is a well-structured command-line application that serves a gRPC service and is an excellent example of how to structure a Go project.
It is not intended to be a complete application but a starting point for your projects.
Table of Contents
- Code of Conduct
It is all based on pushes and pull requests. Here is the list of actions triggered based on each event:
- When a tag is pushed:
securityactions are triggered.
- When a commit is pushed to the
mainbranch or a pull request is opened:
testactions are triggered.
You need to configure the following secrets in the repository settings:
DOCKERHUB_USERNAME: The username of the Docker Hub account.
DOCKERHUB_TOKEN: The token of the Docker Hub account.
GITLEAKS_NOTIFY_USER_LIST: The list of users to notify when a secret leak is found.
- The build action builds the binaries for checking the code.
- The lint action runs
golangci-linton the code and
bufon the protobuf files (example).
- The release action runs
goreleaserto build and publish the binaries (examples).
- The security action runs
gitleaksto check for secret leaks (example).
- The test action runs the unit tests.
You can use act to run GitHub Actions locally.
goreleaser is the core of the release process. It is configured in the
For multiple architectures, all Docker images are pushed to the GitHub Container Registry and Docker Hub.
The binary is built using the
ldflags flag to embed the version, commit and build time in the binary. The
version command or the
--version can print this information.
There are also a few completion scripts for the
fish shells in the released archives.
The releases are created as drafts. You need to manually publish them, allowing you to review the release notes before publishing.
The application is a simple gRPC server with a greeting service. It has a single endpoint,
Hi, which takes a
HiRequest and returns a
HiRequest contains a
name field, which is used to construct the
Running the application
You can either run the application locally or in a Docker container.
It can be configured using environment variables or a configuration file. The configuration file will only be used if the
--config-file flag is provided.
There is a sample configuration file in the
Using environment variables:
export PLAYGROUND_LOG_LEVEL=info export PLAYGROUND_GRPC_SERVER_ADDRESS=:50051 export PLAYGROUND_GRPC_SERVER_NETWORK=tcp go run ./cmd/playground/main.go
Using a configuration file:
go run ./cmd/playground/main.go --config-file ./configs/development.yaml
Running in a Docker container
The Dockerfile is located at the repository's root. It is a multi-stage build that builds the binary and then copies it to a scratch image.
First, we need to build the image:
docker build -t playground .
Then we can run the container:
docker run \ -e PLAYGROUND_LOG_LEVEL=info \ -e PLAYGROUND_GRPC_SERVER_ADDRESS=:50051 \ -e PLAYGROUND_GRPC_SERVER_NETWORK=tcp \ -p 50051:50051 \ playground
There is a
docker-compose.ymlthat you can use as well.docker-compose up -d
You can switch from
nerdctlif you use Containerd.
You will need to install a few tools before you can start developing the application.
Buf is used to lint the protobuf definitions and generate source code from them.
You can find the protobuf definitions in the
Before we continue, let us verify that everything is set up correctly:
You can run all of the configured lint rules by running this command:
Now we can generate the source code for our service:
buf generate -o pkg/proto proto/internal
Here is a list of tools used in this repository:
I might have missed some, so feel free to open a pull request if you think something is missing.
It would be best if you used pre-commit to run linting and testing on your project; this way, you can catch problems before they are committed.
--- repos: - repo: https://github.com/golangci/golangci-lint rev: v1.49.0 hooks: - id: golangci-lint - repo: https://github.com/zricethezav/gitleaks rev: v8.13.0 hooks: - id: gitleaks default_install_hook_types: [pre-commit] minimum_pre_commit_version: 2.20.0
Code of Conduct
This project adheres to the Contributor Covenant code of conduct. By participating, you are expected to uphold this code.