Jenkins CI/CD Pipeline - SonarQube, Docker, Github Webhooks on AWS

Jenkins CI/CD Pipeline - SonarQube, Docker, Github Webhooks on AWS

Launch Instances

I have created 3 t2.medium instances

  • Instance for Jenkins

  • Instance for Sonarqube

  • Instance for Docker

Install Jenkins

Jenkins Server

sudo apt update
clear

Now we have to install java first before installing Jenkins

sudo apt install openjdk-11-jre -y

Now we have to paste the Jenkins commands

curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
  /usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
  https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
  /etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update
sudo apt-get install jenkins

Now we have to allow port 8080 in the Jenkins server security group inbound settings

Now use the public URL and port 8080 to view the Jenkins server

Copy the command which was highlighted in the above picture and use the command

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

It will show the output a password for the Jenkins server

Copy the password and paste it to the Jenkins server

Install suggested plugins

You can set admin credentials

Jenkins Installation done

Creating a Pipeline in Jenkins Server

In this, I have selected Freestyle project

In the source code management, I have selected the Git option

Also select branch of your git

Adding a Webhooks

Now open the settings of your Git Repository, select webhooks

Click to Add Webhook, that time it will ask you to enter your password, once you enter your password, you're able to add Webhooks.

Click to Add Webhook

Now in Build Triggers, select the GitHub hook trigger for GITScm polling (because this function can trigger the pipeline automatically whenever we make changes to the repository.

Click to save.

Without Webhook I have clicked to Build now and it is working perfectly fine

Now time to verify Webhooks, I have clicked on the workspace in Jenkins, and here text.txt file is not present which I am going to create to test the Webhook

I have visited the Git Repository and created a new file by the name of test.txt

I committed the file

Now I am back to the Jenkins server to check #2 build auto trigger and it is working fine.

Now test.txt file is showing in the workspace

Starting a SonarQube Server

Start the Sonarqube Instance

sudo apt update
clear

We need to install Java on the server but 17 version earlier we were using 11 version

sudo apt install openjdk-17-jre -y

Search for the SonarQube website and download the community for the free version

SonarQube Download Copy the link and paste it to the terminal of the SonarQube Instance

sudo adduser sonarqube

Use the command below with wget

wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.4.0.54424.zip

sudo apt install unzip
unzip *
ls

Now the unzipped file is showing

cd sonarqube-9.4.0.54424 
ls

Go to the bin folder

cd bin
cd linux-x86-64
ls

Now start the sonar server by using the command

./sonar.sh start

And also we need to port 9000 in the inbound setting of the security group

Now it's time to check the URL http://54.88.67.190:9000/

Update the password

After updating the password we can access the Sonarqube

Now I have selected the project type Manually

After this, I have selected the CI platform, Jenkins

Selected the DevOps platform GitHub

You can just simply select the options Configure Analysis

Then select Continue

Also, click on Continue here

From here I have selected the Others

Copy the ProjectKey for now you can keep this Key in your notepad

sonar.projectKey=Sample-website

Finish the tutorial

Now click to settings, we need to create token

Now I have created the Token and copy the token for now you can keep this token in your notepad

Now back to Jenkins, Manage Jenkins and Install the plugin

Install one more plugin

Restart Jenkins after installation of plugins

Now Go to the Global Tool Configuration

  • Scrolling Down

  • Click on SonarQube Scanner

  • Give any name and rest default

After saving it, Click to Manage Jenkins again and click on Configure System

Configure System

  • Scrolling down

  • Click on the SonarQube server

  • Click to add

  • Give any name

  • Paste the Sonarqube URL

  • Save

After Saving, Click Configure of the Pipeline in Jenkins

  • Click to Build Environment

  • Select Execute SonarQube Scanner in Build Steps

Ignore everything just paste the key here rest default and saved it

Now back to Manage Jenkins, select Configure System again

Now we have to add token here

  • Select Secret text

  • ID - any name

Select Token after adding and click on save

Now go back to Pipeline to verify whether it's working or not, it's absolutely working fine

Now going to check SonarQube, it's perfectly working fine

Once our code is passed now I am going to deploy it on Docker

Installing Docker

Started Instance

Install Docker

Update the apt package index and install packages to allow apt to use a repository over HTTPS:

sudo apt-get update
sudo apt-get install \
    ca-certificates \
    curl \
    gnupg

Add Docker’s official GPG key:

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

Use the following command to set up the repository:

echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Update the apt package index:

sudo apt update

To install the latest version, run:

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Password-based Server Connectivity

Now I am going to make connection between Jenkins server and Docker server

After running all the commands go back to the Jenkins server

sudo su jenkins

Open Docker server

sudo su
nano /etc/ssh/sshd_config

Uncomment this first

And change the Password Authentication to Yes

systemctl restart sshd

Back to Jenkins Server

Now you can see it's asking for the password earlier it's showing permission denied

Now we have to change the password of the Docker Ubuntu user

Back to Docker Server

passwd ubuntu

Back to Jenkins Server

ssh ubuntu@172.31.22.109

Access done now

Now I am going to generate SSH Key in Jenkins Server

After generating keygen I entered the command

ssh-copy-id ubuntu@172.31.22.109

After running the command enter the password

ssh ubuntu@172.31.22.109

Now we don't need to password again anymore

Back to Jenkins again

  • Manage Jenkins

  • Configure system

  • Server group center

Now we have to add a server list

Now go to Pipeline

  • Configure

  • Post-build action

  • Add build step

Now I am gonna build the pipeline to verify whether it's working or not

And it seems to be working

Open Docker Server, as we can our file got created here

Open the Git Repository now

  • Create a Dockerfile

  • Commit the file

See Auto trigger started

Now returned to Pipeline, configure

  • I have deleted the Remote shell

  • Clicked to execute shell

Created a folder in the Docker server

Clicked to execute shell and fill the details, here I entered docker server IP and folder which is website

We have got the success message here and let us check our docker server

All the files copied to the Docker server

Building the Image and running the container

Back to the Docker server and need to give permission so that we can run all the commands without sudo

sudo usermod -aG docker ubuntu
newgrp docker

After giving the permission we are able to use the docker without sudo

docker ps

Now back to Jenkins

  • Click to Pipeline

  • Click to configure

  • Click to post-build actions

  • Select the Remote shell again in the build steps

  • I gave any random name

Time to check our docker container got created or not

It got created but we have to add port 8085 in the inbound setting of the security group

Now time to check the public URL of the docker Instance

http://54.227.42.50:8085/ (our code is successfully deployed on docker container)