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:
Jenkins: Installed and configured.
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.
Docker: Installed on the host machine.
GitHub Repository: Contains your application code.
GitHub Webhook: Configured to trigger Jenkins builds on new commits.
Pipeline Overview
The pipeline performs the following steps:
Pulls the latest code from the GitHub repository.
Scans the code using SonarQube for quality analysis.
Builds a Docker image for the web application.
Runs the Docker container, exposing the application to the web.
Jenkins Pipeline Configuration
Step 1: Create a New Jenkins Job
Log in to Jenkins and click New Item.
Choose Freestyle Project and provide a name for your job.
Under the Source Code Management section, select Git and provide the repository URL.
Step 2: Configure GitHub Webhook
Go to your GitHub repository.
Navigate to Settings > Webhooks > Add Webhook.
Enter your Jenkins server’s URL followed by
/github-webhook/
(e.g.,http://your-jenkins-server/github-webhook/
).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
GitHub Webhook triggers Jenkins when new code is pushed.
Jenkins pulls the latest code from the repository.
SonarQube Scanner analyzes the code and reports any issues.
The script builds a Docker image for the web application.
Jenkins runs the application in a Docker container, exposing it on port 8081.
Best Practices
Secure Your Credentials: Use environment variables or Jenkins credentials for sensitive information like SonarQube tokens.
Optimize Docker Builds: Use multi-stage builds to keep images lightweight.
Monitor Container Health: Use tools like Prometheus or Grafana to monitor container performance.
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!