This project is a simple Go-based RESTful API built with the Gin Web Framework.
This project also has an implementation that demonstrates Continuous Integration and Continuous Deployment (CI/CD). It provides a comprehensive example of building, testing, and deploying a Go application using GitLab CI and Argo CD.
- RESTful API built with Go and Gin
- Demonstrates all CRUD operations
- Includes simple test suite for unit testing
- A configured CI/CD pipeline for automated building, testing, and deployment
- Static Code Analysis using SonarQube
- GitOps enabled - Utilizes Argo CD for Kubernetes-based deployment
- Dockerized application for easy deployment and scalability
Before getting started with this project, ensure you have the following dependencies installed:
- Go (version 1.16+)
- Docker
- Kubernetes Cluster (either local or on Cloud)
- SonarQube server up and running
- Refer the documentation to configure SonarQube
- Ngrok (if you wish to run SonarQube in Docker)
- Argo CD installed
To get started with this project, follow these steps:
-
Clone the repository:
git clone https://gitlab.com/devops-projects6943118/ci-cd/go-rest-api.git
-
Change into the project directory:
cd go-rest-api
-
Install the dependencies:
go mod download
-
Run the application:
-
Option 1: Run locally
go run main.go
-
Option 2: Run with Docker
-
Build the image locally:
docker build -t go-app .
-
Run the docker container
docker run -dp 3000:3000 go-app
-
-
- You can now access the app at: https://localhost:3000/albums
-
To add am item to the list:
curl https://localhost:3000/albums \ --include \ --header "Content-Type: application/json" \ --request "POST" \ --data '{"id": "5","title": "The Modern Sound of Betty Carter","artist": "Betty Carter","price": 49.99}'
Refer the .gitlab-ci.yml
for the full configuration.
- Setup the following environment variables:
DOCKERHUB_USER
- DockerHub UsernameDOCKERHUB_PASS
- DockerHub Password or Access TokenSONAR_TOKEN
- SonarQube TokenSSH_KNOWN_HOSTS
- SSH known hosts for GitLab (withssh-keyscan
)SSH_PRIVATE_KEY
- SSH private key
The CI pipeline consists of 5 stages:
- build - Builds the Go binaries for Darwin, Linux and Windows
- test - Run simple unit tests using Go testing library
- code_quality - Static code analysis using SonarQube
- image_build - Builds a new docker image and push to DockerHub
- update_k8s_manifest - Updates the latest image tag in Kubernetes manifests
📍NOTE:
The CI pipeline is configured to be triggered "on push" to the project. There are two conditions defined:
Trigger the CI, if the commit message ends with "-ci" keyword > Example: "update:readme-ci"
Do not trigger the CI, if the commit message ends with "-draft" or "-test"
apt install unzip
adduser sonarqube
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.4.0.54424.zip
unzip *
chmod -R 755 /home/sonarqube/sonarqube-9.4.0.54424
chown -R sonarqube:sonarqube /home/sonarqube/sonarqube-9.4.0.54424
cd sonarqube-9.4.0.54424/bin/linux-x86-64/
./sonar.sh start
You can access the SonarQube Server on https://<ip-address>:9000
docker run -d --name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9000:9000 sonarqube:latest
Refer the documentation for more information.
If you are using SonarQube in Docker, you would need to expose the Sonar Server using a tool like Ngrok, for GitLab to communicate with it.
Refer the guide to know more about using Ngrok and Docker together.
Refer the app.yml
for full application configuration for Argo CD.
A few important points for configuring Argo CD:
-
Replace the namespace & server cluster URL with the target namespace & cluster URL for your Kubernetes cluster:
destination: server: https://kubernetes.docker.internal:6443 namespace: go-app
-
Argo CD will automatically sync the application and deploy it based on the configuration defined:
syncPolicy: automated: selfHeal: true
-
Monitor the deployment status and access the application once it's successfully deployed.
📍 To learn more about Argo CD and configure it in your cluster, refer the documentation.
Contributions are welcome! If you encounter any issues or have suggestions for improvements, please feel free to open an issue or submit a pull request.
This project is licensed under the MIT License. Feel free to use and modify the code according to your needs.
The RESTful API has been built with the help of the official Go tutorial - Tutorial: Developing a RESTful API with Go and Gin.
The CI/CD workflow is inspired from Abhishek Veeramalla!