A Beginner’s Guide to Setting Up a CI/CD Pipeline with Jenkins, SonarQube, and Docker on AWS

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

  1. AWS EC2 Instances:

    • Jenkins Instance: For orchestrating the pipeline.

    • SonarQube Instance: For static code analysis.

    • Docker Instance: For hosting and running your website.

  2. GitHub Repository: Store your website’s code. (You can use a free HTML/CSS template for practice!)

  3. Basic Linux Knowledge: Familiarity with SSH and terminal commands.

  4. AWS Security Groups: Properly configured inbound rules to allow required traffic (explained below).


Step 1: Prepare Your GitHub Repository

1.1 Create the Repository

  1. Go to GitHub and log in.

  2. Create a new repository for your website.

  3. Clone the repository to your local machine:

 git clone https://github.com/your-username/your-repository.git
  1. Download a free template from Free CSS and add the template files to your project.

  2. 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

  1. Create an EC2 instance (Ubuntu 20.04 recommended) for Jenkins.

  2. In the Security Group, allow inbound traffic on port 8080.

    Inbound Rule:

    • Protocol: TCP

    • Port Range: 8080

    • Source: Anywhere (0.0.0.0/0)

  3. Connect to the instance via SSH:

     ssh -i <your-key.pem> ubuntu@<jenkins-ip>
    
  4. Set the hostname:

     sudo hostnamectl set-hostname jenkins
    

2.2 Install Jenkins

  1. Install OpenJDK 17:

     sudo apt update
     sudo apt install openjdk-17-jre -y
    
  2. 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
    
  3. Start Jenkins:

     sudo systemctl start jenkins
    
  4. 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

  1. Go to Jenkins Dashboard > New Item > Freestyle Project.

  2. Under Source Code Management, select Git and provide your repository URL.

  3. Add a GitHub Webhook:

    • In GitHub, go to Settings > Webhooks > Add Webhook.

    • Set the payload URL as <jenkins-ip>/github-webhook/.

  4. 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

  1. Launch another EC2 instance for SonarQube.

  2. Allow inbound traffic on port 9000 in the Security Group.

    Inbound Rule:

    • Protocol: TCP

    • Port Range: 9000

    • Source: Anywhere (0.0.0.0/0)

  3. 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

  1. Install OpenJDK 17:

     sudo apt update
     sudo apt install openjdk-17-jre -y
    
  2. 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
    
  3. Access SonarQube at <sonarqube-ip>:9000. Use default credentials (admin/admin) and change the password.


3.3 Integrate SonarQube with Jenkins

  1. Install the SonarQube Scanner Plugin in Jenkins.

  2. 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.

  3. 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

  1. Launch an EC2 instance for Docker.

  2. Allow inbound traffic on port 8085.

    Inbound Rule:

    • Protocol: TCP

    • Port Range: 8085

    • Source: Anywhere (0.0.0.0/0)

  3. 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

  1. Install Docker:

     sudo apt update
     sudo apt install docker.io -y
     sudo usermod -aG docker ubuntu
    
  2. Verify Docker installation:

     docker --version
    

4.3 Enable Passwordless SSH

  1. Generate an SSH key on the Jenkins instance:

     ssh-keygen -t rsa -b 4096
    
  2. Copy the public key to the Docker instance:

     ssh-copy-id -i ~/.ssh/id_rsa.pub ubuntu@<docker-ip>
    
  3. Test passwordless SSH:

     ssh ubuntu@<docker-ip>
    

4.4 Deploy the Website Using Docker

  1. Add a Dockerfile to your repository:

     FROM nginx
     COPY . /usr/share/nginx/html
    
  2. 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"
      
  3. 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

  1. Jenkins Documentation – Learn how to use Jenkins effectively.

  2. SonarQube Documentation – Understand SonarQube integration for code quality.

  3. Docker Documentation – Dive into Docker’s containerization tools.

  4. AWS EC2 Documentation – Guide to configuring EC2 instances.

  5. Free CSS Templates – Find free templates to jumpstart your frontend development.