In this project, we will explore how to set up a Jenkins CI/CD pipeline using Github, Sonarqube, and Docker on an AWS EC2 instance.
https://medium.com/@rutikdevops/devops-project-4-31891d829c00
- In this project I will create an AWS EC2 instance and install Jenkins on it. We will then set up a Github repository and integrate it with Jenkins. Next, we will configure Sonarqube to analyze code quality in our Github repository. Finally, we will create a Docker image of our application and deploy it using Jenkins.
- Create 3 ec2 instance :
- Jenkins-Server : AWS Linux-2, t2 micro
- Docker-Server : AWS Linux-2, t2 micro
- SonarQube-Server : AWS Linux-2, t2 medium
ec2-user
sudo su
yum update -y
hostnamectl set-hostname jenkins
bash- Java installation on Jenkins server:-
yum install java* -y
java --version
alternatives --config java ## Using this command you can choose any version of Java
// select java 11 version- Jenkins installation on Jenkins server:-
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
yum install jenkins -y
systemctl enable jenkins.service
systemctl start jenkins.service
systemctl status jenkins.service- Copy public ip of jenkins server and paste it in new tab with port no.8080
- copy this path and paste in terminal with "cat" command
cat /var/lib/jenkins/secrets/initialAdminPassword- Now copy & paste this passwd in jenkins. so, jenkins is ready.
-
Jenkins-> New Item-> Freestyle Project-> Source Code Management-> Git
-
Connect github to jenkins through webhook
ec2-user
sudo su
yum update -y
hostnamectl set-hostname sonarqube
bash- Java installation on Jenkins server:-
yum install java* -y
java --version
alternatives --config java ## Using this command you can choose any version of Java
// select java 11 versioncd /opt
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.9.10.61524.zip #(chrome-downloadsonarqube-latestversion-rightclick-copypaste)
unzip sonarqube-8.9.10.61524.zip
cd sonarqube-8.9.10.61524
cd bin
cd linux-x86-64
./sonar.sh start
./sonar.sh status (in that sonarqube is not running)
cd ../..
cd logs
useradd sonaradmin
chown -R sonaradmin:sonaradmin /opt/sonarqube-8.9.10.61524
pwd
cd ..
su - sonaradmin
cd /opt/sonarqube-8.9.10.61524/
cd bin
cd linux-x86-64
./sonar.sh start
./sonar.sh status
netstat -tulpn #(command for check port)
#(paste public ip with :9000)
#(securitygroup-inboundrule-all traffic, anywhere)
#(username passwd bydefault is admin)sudo su -
yum install docker -y
systemctl start docker
systemctl enable docker
systemctl status docker
useradd dockeradmin
passwd dockeradmin
usermod -aG docker dockeradmin
vim /etc/ssh/sshd_config
( PasswordAuthentication yes )
systemctl restart sshd-
Lets Integrate Docker with Jenkins
-
Jenkins -> Manage Jenkins -> Plugins -> Available -> Search for Publish Over SSH -> Install
-
Lets configure Docker in Jenkins
-
Jenkins -> Manage Jenkins -> System -> Publish Over SSH -> SSH Servers -> Add -> Give Name -> Hostname : -> Username : dockeradmin -> advanced -> enable password based authentication -> password : <password of dockeradmin -> test configuration -> Apply -> Save
-
Successfully instegrated docker with jenkins.
-
Now goto docker server
cd /opt
mkdir docker
cd docker
chown -R dockeradmin:dockeradmin /opt/docker
vim Dockerfile
FROM tomcat:latest
RUN cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps
COPY ./*.war /usr/local/tomcat/webapps
chmod 777 /var/run/docker.sock- Now Create a Jenkins job to pull the code from Github , build with help of Maven and copy the Artifacts to Dockerhost.
- Jenkins -> Dashboard ->New project-> DevOps-Project-4-> maven project-> Source Code Management-> Post-build Actions -> select Send build artifacts over SSH -> SSH Server -> Name : give name of Added SSH Server -> Source file: webapp/target/*.war -> Remove prefix : webapp/target -> Remote directory : //opt//docker -> Exec command : enter the below commands
cd /opt/docker;
docker build -t regapp:v1 .;
docker stop registerapp;
docker rm registerapp;
docker run -d --name registerapp -p 8081:8080 regapp:v1
- Apply -> Save
- Lets access the tomcat server using public ip through port 8081
- In this section, we have successfully deployed the application to the Docker server and enabled CI/CD. If any changes occur in the source code, they will be automatically built and deployed to the server.


