A Beginner’s Guide to Setting Up a CI/CD Pipeline with Jenkins, SonarQube, and Docker on AWS
Imagine pushing code with confidence, knowing it will be automatically tested, analyzed, and deployed seamlessly. Sounds like a dream? Not anymore! In this blog, I’ll walk you through building a powerful CI/CD pipeline using Jenkins, SonarQube, and Docker, each running on its own AWS EC2 instance.
By the end, you’ll have a streamlined system that boosts productivity, ensures code quality, and accelerates deployments. Ready to transform your development workflow? Let’s get started! 🚀
What’s a CI/CD Pipeline?
A CI/CD pipeline automates repetitive development tasks like testing, building, and deploying applications. Here’s why it’s valuable:
Continuous Integration (CI): Automatically tests and integrates code changes to ensure compatibility.
Continuous Deployment (CD): Deploys the latest code changes seamlessly, reducing manual work.
In this guide, we’ll integrate Jenkins for automation, SonarQube for code quality checks, and Docker for deployment, running them on three AWS EC2 instances.
What You’ll Need
AWS EC2 Instances:
Jenkins Instance: For orchestrating the pipeline.
SonarQube Instance: For static code analysis.
Docker Instance: For hosting and running your website.
GitHub Repository: Store your website’s code. (You can use a free HTML/CSS template for practice!)
Basic Linux Knowledge: Familiarity with SSH and terminal commands.
AWS Security Groups: Properly configured inbound rules to allow required traffic (explained below).
Step 1: Prepare Your GitHub Repository
1.1 Create the Repository
Go to GitHub and log in.
Create a new repository for your website.
Clone the repository to your local machine:
git clone https://github.com/your-username/your-repository.git
Download a free template from Free CSS and add the template files to your project.
Add your website files to the repository:
cd your-repository
git init
git add .
git commit -m "Initial Commit"
git push -u origin main
Step 2: Set Up Jenkins
2.1 Launch the Jenkins EC2 Instance
Create an EC2 instance (Ubuntu 20.04 recommended) for Jenkins.
In the Security Group, allow inbound traffic on port 8080.
Inbound Rule:
Protocol: TCP
Port Range: 8080
Source: Anywhere (0.0.0.0/0)
Connect to the instance via SSH:
ssh -i <your-key.pem> ubuntu@<jenkins-ip>
Set the hostname:
sudo hostnamectl set-hostname jenkins
2.2 Install Jenkins
Install OpenJDK 17:
sudo apt update sudo apt install openjdk-17-jre -y
Add the Jenkins repository and install Jenkins:
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add - sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list' sudo apt update sudo apt install jenkins -y
Start Jenkins:
sudo systemctl start jenkins
Access Jenkins at
<jenkins-ip>:8080
and complete the setup using the token from:sudo cat /var/lib/jenkins/secrets/initialAdminPassword
2.3 Configure a Jenkins Freestyle Project
Go to Jenkins Dashboard > New Item > Freestyle Project.
Under Source Code Management, select Git and provide your repository URL.
Add a GitHub Webhook:
In GitHub, go to Settings > Webhooks > Add Webhook.
Set the payload URL as
<jenkins-ip>/github-webhook/
.
Test the webhook by pushing a change to the repository. Jenkins should trigger a build.
Step 3: Set Up SonarQube
3.1 Launch the SonarQube EC2 Instance
Launch another EC2 instance for SonarQube.
Allow inbound traffic on port 9000 in the Security Group.
Inbound Rule:
Protocol: TCP
Port Range: 9000
Source: Anywhere (0.0.0.0/0)
SSH into the instance and set the hostname:
ssh -i <your-key.pem> ubuntu@<sonarqube-ip> sudo hostnamectl set-hostname sonarqube
3.2 Install SonarQube
Install OpenJDK 17:
sudo apt update sudo apt install openjdk-17-jre -y
Download and configure SonarQube:
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.9.0.zip sudo apt install unzip -y unzip sonarqube-*.zip cd sonarqube-*/bin/linux-x86-64 ./sonar.sh start
Access SonarQube at
<sonarqube-ip>:9000
. Use default credentials (admin/admin) and change the password.
3.3 Integrate SonarQube with Jenkins
Install the SonarQube Scanner Plugin in Jenkins.
Go to Manage Jenkins > Configure System, and add the SonarQube server:
Name: SonarQube
URL:
<sonarqube-ip>:9000
Token: Generate a token in SonarQube under My Account > Security > Generate Tokens.
Add a Docker Group
In the Server Group Center, click Add Group.
From the dropdown, select Docker Server.
Provide a name for the group (e.g.,
DockerServerGroup
).After the group is created, click Add Server inside the Docker group.
Fill in the following details for the Docker server:
Name: A unique name (e.g.,
DockerServer1
).Docker Host URI: Use the format
<docker-ip>
.
Step 4: Set Up Docker
4.1 Launch the Docker EC2 Instance
Launch an EC2 instance for Docker.
Allow inbound traffic on port 8085.
Inbound Rule:
Protocol: TCP
Port Range: 8085
Source: Anywhere (0.0.0.0/0)
SSH into the instance and set the hostname:
ssh -i <your-key.pem> ubuntu@<docker-ip> sudo hostnamectl set-hostname docker
4.2 Install Docker
Install Docker:
sudo apt update sudo apt install docker.io -y sudo usermod -aG docker ubuntu
Verify Docker installation:
docker --version
4.3 Enable Passwordless SSH
Generate an SSH key on the Jenkins instance:
ssh-keygen -t rsa -b 4096
Copy the public key to the Docker instance:
ssh-copy-id -i ~/.ssh/id_rsa.pub ubuntu@<docker-ip>
Test passwordless SSH:
ssh ubuntu@<docker-ip>
4.4 Deploy the Website Using Docker
Add a Dockerfile to your repository:
FROM nginx COPY . /usr/share/nginx/html
Configure Jenkins to deploy the website:
Add a shell build step:
scp -r ./* ubuntu@<docker-ip>:~/website ssh ubuntu@<docker-ip> "cd ~/website && docker build -t mywebsite . && docker run -d -p 8085:80 mywebsite"
Push a change to your repository and verify the website is live at
<docker-ip>:8085
.
Conclusion
🎉 You've successfully set up a CI/CD pipeline with Jenkins, SonarQube, and Docker. Here's how it works:
Jenkins automates your pipeline.
SonarQube ensures code quality.
Docker simplifies deployment.
This setup gives you essential DevOps skills to handle real-world projects. Keep experimenting, learning, and sharing your journey!
Resources
Jenkins Documentation – Learn how to use Jenkins effectively.
SonarQube Documentation – Understand SonarQube integration for code quality.
Docker Documentation – Dive into Docker’s containerization tools.
AWS EC2 Documentation – Guide to configuring EC2 instances.
Free CSS Templates – Find free templates to jumpstart your frontend development.