DevOps-Project-4
5 min readAug 28, 2023
Deploy Website on Docker with code analysis tool SonarQube using ec2 instance & Jenkins
Project GitHub Link :-
https://github.com/rutikdevops/DevOps-Project-4
Project Overview :-
- In this project I created a CICD Pipeline. In which push the code in GitHub and from the github I will push the code to Jenkins and After pulling the code from GitHub I will Test the code from SonarQube which is static code analysis tool In which we can solve bugs & vulnarities also we can add the rules regarding scanning. After scanning the code If code pass I deployed code on Docker
Project Steps :-
- 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
1. Install and Configure the Jenkins :-
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.
2. Run the code in jenkins:
- Jenkins-> New Item-> Freestyle Project-> Source Code Management-> Git
- Connect github to jenkins through webhook
- Build Now in Jenkins
3. Run sonarqube
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 version
cd /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)
4. Connect to terminal and install docker :-
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
Output :-
Final Output :-
- 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.