AWS-Jenkins-Pipeline-SonarQube-Docker-Deployment

JEnkins Cicd Pipeline

AWS Create 3 EC2 instances Step 1 - We will push the code the github Step 2 - Pull the code through jenkins Step 3 - Test the code via sonarqube - scans Step 4 - Deploy in Docker

Open AWS console

image

Create an EC2 Instance for Jenkins with a key pair

Create an EC2 Instance for SonarQube and use the same key pair generated

Create an EC2 Instance for Docker and use the same key pair generated

image

Part 1 - Jenkins - Github Setup

SSH this cmd to get access to the Jenkins server(Use the instance public Ip as per intance generated)

ssh -i SSH-Key-Jenkins.pem ubuntu@52.23.182.224

image

sudo apt update

Then follow jenkins installation for Debian Ubuntu

sudo apt install openjdk-11-jre

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 -y

sudo apt-get install jenkins -y

sudo systemctl enable jenkins

sudo systemctl start jenkins

sudo systemctl status jenkins

Now expose the port 8080 on jenkins ec2 instance -> Go to Security and edit inbound rules image

Copy the admin token image

Now open the public ip with port - http://52.23.182.224:8080/ - and enter the token copied image

Install the suggested plugins image

Create a User image

image

Create a Pipeline image

Add the Git repo URL in the Configuration image

Also select GITScm to automatically triger the pipeline whenver we make changes in the repository image

Add the Github Webhook in the github rep settings - Add the Jenkins url ip and port (http://34.224.39.140:8080/) and select ind events - pull reqs and pushes image

File updated in workspace image

Also latest build log updated image

Part 1 Summary

We have automated the process till the Jenkins whenever we change or add the code in github repository it will trigger the jenkins automatically and Jenkins will pull automatically from the github


Part 2 - SonarQube Setup

Log into the ssh for the SonarQube Instance

SSH this cmd to get access to the SonarQube server(Use the instance public Ip as per intance generated) - PublicIP - 3.84.220.33

ssh -i SSH-Key-Jenkins.pem ubuntu@54.221.137.114

image

sudo apt update

sudo apt install openjdk-17-jre

Download SonarQube using wget method and paste the download link

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

sudo apt install unzip

unzip sonarqube-10.1.0.73491.zip

image

Open the sonarqube instance with its public ip and its port- Use admin and admin for username and pass image

image

Create a project image

image

Create a global token image

Go to Jenkins manage Plugins and install SonarQube Scanner and SSH2 Easy plugin image image

Now in the configure tools add the SonarQube Scanner image

Go to configure systems and add the sonarqube server and paste the sonarqube url - http://54.221.137.114:9000/ image

Configure the project key in the sonarqube build steps image

Add the token in the secret text dropdown under secret image

Then select the created token image

Build and check the sonarqube report image

image

It is passed image


Part 3 - Deploy on Docker

Log into the docker ec2 instance image

Install the docker from documention and add the credentials created and add the Docker group in jenkins image

Add the docker Server in jenkins image

Under the build steps chose the remote shell build step - and add a text file just to verify image

Now build the pipeline and verify if the text file is created in the docker server image

Create a dockerfile with Nginx image and store in repository also create a folder in docker

Add a build step execute shell - copy the contents to a location created in docker folder (scp ./* ubuntu@54.81.5.217:~/website/) image

Make sure the docker is given permissions to run the cmds image

Now in Jenkins in the build step add the remote shell content

image

Check the container working image

Configure the ports 8085 and website is deployed with docker container - http://54.81.5.217:8085/ image