🚀 Comprehensive Guide to Setting Up SonarQube, Jenkins, Docker, and CI/CD Integration

Written by Sivaranjan

🚀 Comprehensive Guide to Setting Up SonarQube, Jenkins, Docker, and CI/CD Integration

Output

Servers

SonarQube Setup 🛠️

SonarQube Server Installation

  1. Install JDK:

     sudo apt install openjdk-17-jre
    
  2. Download Community Edition:

     wget <sonarqube file link>
     ls
    
  3. Install SonarQube:

     sudo apt install unzip
     unzip sonar.zip
     cd sonarqube/bin/linux
     ./sonar.sh console
    
  4. Add Inbound Rule:

    • Open port 9000 for SonarQube.

SonarQube Configuration

  1. Access SonarQube GUI:

    • Visit http://<your_ip>:9000 in your browser.

    • Default credentials: admin/admin

  2. Set New Password.

  3. Create Project:

    • Fill in details: Name, Key, Branch (main).
  4. Setup Project:

    • CI: Jenkins

    • Platform: GitHub

    • Build Description: Other

    • Copy Project Key

  5. Generate Token:

    • Navigate to Admin User -> Security -> Generate Token

Jenkins SonarQube Integration 🔗

Jenkins Server Setup

  1. Install Java & Jenkins:

  2. Create Free-Style Job:

    • Source: Git URL

    • Branch: */main

  3. Build Trigger:

    • GitHub webhook
  4. Manage Plugins:

    • Install SonarQube Scanner

    • Install SSH2 Easy

  5. Global Tool Configuration:

    • Go to SonarScanner

    • Name: Add and set to auto-install (version 4.8.0)

  6. System Configuration:

    • Go to SonarQube Server settings

    • Name: Add SonarQube Server

    • URL: Add URL

    • Authenticate using Secret Text

  7. Build Steps:

    • Execute SonarQube Scanner

    • Add Project Key in Analysis Properties

Verify SonarQube Scan:

  • Build the Jenkins job to check SonarQube scan results.


Docker Setup 🐳

Docker Server on Ubuntu

  1. Install Docker:

  2. Switch to Root:

     sudo su
    
  3. Configure SSH:

     vi /etc/ssh/sshd_config
    
    • Enable PubkeyAuthentication and PasswordAuthentication
    systemctl restart sshd
    passwd ubuntu # Set new password
    mkdir website # Create deployment path
  1. Add User to Docker Group:

     sudo usermod -aG docker ubuntu
     newgrp docker
    

Integrate Jenkins with Docker 🤝

Jenkins to Docker Server Integration

  1. Switch User to Jenkins:

     su jenkins
    
  2. SSH to Docker Server:

     ssh ubuntu@docker_ip
    
  3. Generate SSH Key:

     ssh-keygen
    
  4. Copy SSH Key to Docker:

     ssh-copy-id ubuntu@docker_ip
    

GitHub Setup 🐙

Create Webhook URL

  • Create a webhook in your GitHub repository to trigger Jenkins builds.


Jenkins Job for Docker Deployment 📦

Jenkins GUI Configuration

  1. Add Docker Server Group:

    • Name: Docker server

    • SSH Port: 22

    • Username: ubuntu

    • Password: your password

  2. Add Server List:

    • Server Group: Docker server

    • Server Name: docker

    • Server IP: docker_ip

  3. Post-Build Action:

    • Select Remote Shell

    • Target Server: Docker

    • Shell Command: touch hello.txt

Verify Deployment:

  • Check if hello.txt is created in Docker server:

      ls
    

Docker Deployment via Jenkins 🏗️

GitHub Configuration

  1. Create Dockerfile in GitHub:

     FROM nginx
     COPY . /usr/share/nginx/html
    

Jenkins GUI Configuration

  1. Post-Build Action:

    • Remove Remote Shell

    • Execute SCP Command:

        scp -r ./* ubuntu@docker_ip:/home/ubuntu/website
      
  2. Add Remote Shell Action:

    • Target Server: Docker server

    • Shell Commands:

        cd /home/ubuntu/website
        docker build -t mywebsite .
        docker run -itd -p 8085:80 --name onix mywebsite
      

Verify Deployment:

  • Build the Jenkins job and check the deployment.

By following these steps, you can set up a comprehensive CI/CD pipeline integrating SonarQube, Jenkins, Docker, and GitHub. This guide ensures a streamlined process from code analysis to deployment, enhancing code quality and efficiency. Happy coding! 🚀