Mastering CI/CD: Create a Strong Pipeline with Jenkins, SonarQube, and Docker

Mastering CI/CD: Create a Strong Pipeline with Jenkins, SonarQube, and Docker

In this article, I’ll walk you through the process of creating a complete CI/CD pipeline using Jenkins, SonarQube, Docker, and GitHub webhooks. This pipeline automates the process of pulling code from a repository, performing a code quality scan, building a Docker image, and deploying a web application. Let’s dive in!


Prerequisites

Before we start, ensure you have the following set up:

  1. Jenkins: Installed and configured.

  2. SonarQube: Installed and running on a server.

    • Tip: Use a t2.medium or t3.medium AWS instance for SonarQube. It may not work properly on t2.micro or t3.micro instances due to insufficient resources.
  3. Docker: Installed on the host machine.

  4. GitHub Repository: Contains your application code.

  5. GitHub Webhook: Configured to trigger Jenkins builds on new commits.


Pipeline Overview

The pipeline performs the following steps:

  1. Pulls the latest code from the GitHub repository.

  2. Scans the code using SonarQube for quality analysis.

  3. Builds a Docker image for the web application.

  4. Runs the Docker container, exposing the application to the web.


Jenkins Pipeline Configuration

Step 1: Create a New Jenkins Job

  1. Log in to Jenkins and click New Item.

  2. Choose Freestyle Project and provide a name for your job.

  3. Under the Source Code Management section, select Git and provide the repository URL.

Step 2: Configure GitHub Webhook

  1. Go to your GitHub repository.

  2. Navigate to Settings > Webhooks > Add Webhook.

  3. Enter your Jenkins server’s URL followed by /github-webhook/ (e.g., http://your-jenkins-server/github-webhook/).

  4. Select the event trigger for push.

Step 3: Add Build Steps

Pull the Latest Code

Jenkins automatically fetches the latest code from GitHub as configured in the Source Code Management section.

Perform Code Quality Analysis

Use SonarQube for static code analysis:

sonar-scanner \
  -Dsonar.projectKey=your_project_key \
  -Dsonar.sources=. \
  -Dsonar.host.url=http://your-sonarqube-server:9000 \
  -Dsonar.login=your_authentication_token

Ensure the SonarQube scanner is installed and configured on the Jenkins server.

Build and Deploy the Application

Add an Execute Shell build step with the following script:

if ! cp -r /var/lib/jenkins/workspace/demo/* /home/ubuntu/web; then
    echo "Error: Failed to copy files."
    exit 1
fi

# Navigate to the application directory
cd /home/ubuntu/web || { echo "Directory not found!"; exit 1; }

# Stop and remove all containers if any exist
if [ "$(docker ps -qa)" ]; then
    docker rm -v -f $(docker ps -qa)
else
    echo "No containers to remove."
fi
# Build the Docker image
docker build -t web .

# Run the container
docker run -t -p 8081:80 --name website web

How It Works

  1. GitHub Webhook triggers Jenkins when new code is pushed.

  2. Jenkins pulls the latest code from the repository.

  3. SonarQube Scanner analyzes the code and reports any issues.

  4. The script builds a Docker image for the web application.

  5. Jenkins runs the application in a Docker container, exposing it on port 8081.


Best Practices

  1. Secure Your Credentials: Use environment variables or Jenkins credentials for sensitive information like SonarQube tokens.

  2. Optimize Docker Builds: Use multi-stage builds to keep images lightweight.

  3. Monitor Container Health: Use tools like Prometheus or Grafana to monitor container performance.

  4. Clean Up: Add steps to prune unused Docker images and containers to save disk space:

     docker image prune -f
    

Conclusion

By integrating Jenkins, SonarQube, Docker, and GitHub webhooks, you’ve created a robust CI/CD pipeline that automates code analysis, building, and deployment. This pipeline ensures faster delivery with improved code quality and reduced manual intervention.

Let me know if you found this guide helpful or have any questions in the comments below. Happy coding!