Skip to content

verma-kunal/ci-cd-gitlab-argo

Repository files navigation

CI/CD with GitLab CI and Argo CD

📍 NOTE:

This project has originally been implemented on GitLab, as it's specifically focused towards using the GitLab CI. GitLab repo link

Introduction

This project is an implementation of Continuous Integration and Continuous Deployment (CI/CD) on a simple Go-REST-API application. It provides a comprehensive example of building, testing, and deploying a Go application using GitLab CI and Argo CD.

Features

  • 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

Prerequisites

Before getting started with this project, ensure you have the following dependencies installed:

Getting Started

Setup the Project

Please follow the instructions given in the README, to set up the GO-REST-API application correctly.

Overview of CI/CD Workflow

GitLab CI - Continuous Integration

Refer the .gitlab-ci.yml for the full configuration.

Prerequisites

  • Setup the following environment variables:
    • DOCKERHUB_USER - DockerHub Username
    • DOCKERHUB_PASS - DockerHub Password or Access Token
    • SONAR_TOKEN - SonarQube Token
    • SSH_KNOWN_HOSTS - SSH known hosts for GitLab (with ssh-keyscan)
    • SSH_PRIVATE_KEY - SSH private key

The CI pipeline consists of 5 stages:

  1. build - Builds the Go binaries for Darwin, Linux and Windows
  2. test - Run simple unit tests using Go testing library
  3. code_quality - Static code analysis using SonarQube
  4. image_build - Builds a new docker image and push to DockerHub
  5. 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:

  1. Trigger the CI, if the commit message ends with "-ci" keyword > Example: "update:readme-ci"

  2. Do not trigger the CI, if the commit message ends with "-draft" or "-test"

Steps to run SonarQube Locally (Linux)
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 http:https://<ip-address>:9000

Steps to run SonarQube on Docker
docker run -d --name sonarqube -e SONAR_ES_BOOTSTRAP_CHECKS_DISABLE=true -p 9000:9000 sonarqube:latest

Refer the documentation for more information.

Connecting GitLab and SonarQube Server

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.

Argo CD - Continuous Deployment

Refer the app.yml for full application configuration for Argo CD.

A few important points for configuring Argo CD:

  1. 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
    
  2. Argo CD will automatically sync the application and deploy it based on the configuration defined:

    syncPolicy:
        automated:
          selfHeal: true
    
  3. 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.

Contributing

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.

License

This project is licensed under the MIT License. Feel free to use and modify the code according to your needs.

Acknowledgements

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!