Gin Serverless Container GCP Deployment Template
🚀 This is a template for web apps using Golang, Gin, Docker and Google Cloud Run
Quick Start
To make your app into production, first create a GCP project and set a billing account for it. And then run the following commands:
Build and push the container to GCP container image registry:
$ gcloud builds submit
Apply the Terraform infra to GCP:
$ cd terraform/
$ terraform init
$ terraform apply
You will be asked for the project id and the Docker image URL.
Building
By far, the easiest way to get everything running is with docker-compose
. See the docker-compose section. First, set up the environment variables for your project. You can use the .env.example
:
$ source .env.example
Binary
To build the binary, run the following:
$ go build -o ${APP_NAME}
To run the binary, run the following:
$ ./${APP_NAME}
Or simply:
$ go run main.go
Makefile
To run the through the Makefile, run the following
$ source .env
$ make run
Docker
Make sure you have Docker installed on your machine.
To build the Docker image, run the following:
$ docker build . -t ${APP_NAME}
To run the Docker image, run:
$ docker run -p ${PORT}:${PORT} ${APP_NAME}
docker-compose
To run the docker-compose:
$ docker-compose up
Deploying
To deploy your container to Google Cloud Run, follow these steps:
Push Docker Image
First, push your Docker image to GCP's registry with the Google Cloud Build. We've setted up the pushing instructions on cloudbuild.yaml
file:
$ gcloud build submit
Take note of your Docker image url.
Deploy Cloud Run
First you need a Google Cloud project set up and a billing account. Take a look into this docs on how to create a project if you don't have one, and your billing account is setted for it (run: $ open "https://console.cloud.google.com/billing/linkedaccount?project=$PROJECT_ID"
). Now let's get our application into production with Terraform. Cd to terraform/
directory and init it:
$ cd terraform/
$ terraform init
Now apply the infra into your Google Cloud project:
$ terraform apply
You will be asked for your PROJECT_ID
and the DOCKER_IMAGE_URL
of your application.
Testing
The unit testes are written with the default testing tool of Golang.
Unit Tests
To run the unit tests, do the following:
$ go test -v ./...
To run the tests with coverage, do the following:
$ go test -v -covermode=count ./...
Or use the Makefile
:
$ make test
Continuous Integration
We use GitHub Actions for our CI tool. Right now we have four workflows, and you can check they state here:
- Building the binary status.
- Unit tests status.
- Building the Docker image status.
- Terraform plan