CI/CD Pipeline Project - DevOps Engineer

Git-Server:-


Now it's time to pull the code from the GitHub repository.



we have done some changes to the index file and successfully push it to the Github repository.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Jenkins Server
Now I'm creating a second instance for the Jenkins server.
Successfully created Jenkins instance and install java 11 in the same.
Now set environment variables to the java.


Copy the above path and paste it into one hidden file that is .bash_profile

Open that .bash_profile file with vim

Java is installed successfully and setting environment variables for the same is also done. Now you can install Maven and Jenkins in the system.
Jenkins Installation


Jenkins Installation is done.
Now open Jenkins and go to the_____
Manage Jenkins --> Tools --> JDK --> Add JDK --> JDK Name (Anyname) --> JAVA_HME (Give Java path location here)


Now we will install Maven in the system to build your project.
We need to set environment variables for the Maven also like Jenkins.
Go to the .bash_profile file again and set environment variables for the Maven.


Now we will create one job where we will pull the code from GitHub and build the code with the help of Maven and it will create a .war file.


with the help of the "clean install package" command, you will get that .war file.
Now try to build the job ____

Once the build is successful you will get that war file in this Jenkins server itself.

Now the war file is created in the Jenkins server itself.
Path:-


--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Ansible-Server
Now create an instance for the Ansible server.
Successfully create an Ansible-Server instance and install Ansible software in the same.
Step1:- Create an ansible user
Step2:- Set password for ansible user

Step3:- Go to the visudo file and type the below syntax

Step4:- Go to the below file
vi /etc/ssh/sshd_config and type yes in front of PasswordAuthentication


Our Ansible Server is ready now.
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Launching Two EC2 instances for Ansible Host 1 and Ansible Host 2
we have successfully launch two ec2 instances for Ansible-Host1 and Ansible-Host2.
Step1:- Create an ansible user
Step2:- Set password for ansible user
Step3:- Go to the visudo file and type the below syntax


Step4:-Go to the below file
vi /etc/ssh/sshd_config and type yes in front of PasswordAuthentication


Once it is done just reload the service. "service sshd reload"

Now go to the Ansible-Server and try to copy ssh keys to the Ansible-Host1 and Ansible-Host2.
Type the below command in the Ansible-Server:-
"ssh-copy-id ansible@172.31.41.145"
where 172.31.41.145 = Ansible-Host1 private IP address

The same process you have to do for Ansible-Host2 also.
Type the below command in the Ansible-Server:-
"ssh-copy-id ansible@172.31.40.108"
where 172.31.40.108 = Ansible-Host2 private IP address

Now check whether you can connect to Ansible-Host1 and Ansible-Host2 without asking for a password.
You can check using the below command
ssh ansible-host1 private IP address - ssh 172.31.41.145
ssh ansible-host1 private IP address - ssh 172.31.40.108
Now in Ansible-Server create two files - 1. host file 2. ansible-config file
Why we are creating these two files because we do not download or install Ansible-Server using the Epel repository concept.
Let's try to create these two files now.


The above two are private IP addresses of Ansible-Host1 and Ansible-Host2.
Now let's try to use one command so that we can understand whether we able to do ping or not.

This says that you can connect successfully to Host1 and Host2.
Now it's time to establish a connection between Jenkins and Ansible-Server. For this, you have to install one plugin i.e., "Publish Over SSH"
Go to the Jenkins --> Manage Jenkins --> Plugins --> Install Publish Over SSH plugin
Go to the Manage Jenkins --> System --> SSH Servers --> Add

Where, Name - Random name you can choose
Hostname - Ansible Server Private IP address
Username - we have created "ansible" user

If you click on Test Configuration you will get an error like above.
Why this error is coming because we have not mentioned the password.

In the Passphrase tab, you have to give the password that you have set while creating Ansible Server.
Successfully we connect to the Ansible-Server.
Go to the Ansible home directory

Now it's time to push that war file from Jenkins to the Ansible-Server.
For this, we will create one new job. It will pull the code from the git repository, build it with the help of Maven and generate a war file and push it to the Ansible-Server.


Select "Send build artifacts over SSH"

Our job is created successfully.
The build is done successfully

Once you executed this job one folder got created in /var/lib/jenkins/workspace and the war file push to the ansible home directory.


Tomcat Server Installation
we want to access our application in the web browser right? Now we will run our application into the Tomcat Server. For this, we need to download the Tomcat server on host1 and host2.



Now we have to start the service
Take Host1 and Host2 public IP address and try to open in web browser

We will get this type of error because we are not starting the service of Tomcat. We need to start the service of Tomcat.
For this, we need to go to the tomcat/bin folder. In this folder, you will see all the binary executable files.


If you have a question that why we are getting the above error. The above error we are getting because we do not install Java in Host1 and Host2. To run the Tomcat server, we need Java and that's why we getting this error.
Let's try to install Java in Host1 and Host2.



Click on Manager App

Why straight away it gives an error that Access Denied?
Because our Host1 and Host2 run on the Linux Operating system in the Mumbai region and you try to access them in your personal systems web browser. That is why we are getting this error as Access Denied.
To resolve this error we need to modify context.xml configuration file.

We need to modify these two files:- Perform these steps in Host1 and Host2
/root/tomcat/webapps/host-manager/META-INF/context.xml /root/tomcat/webapps/manager/META-INF/context.xml
Once you open these files you will see the Valve block there. Just comment it out as I showed you the below picture.


You have modified the context.xml file in both the Host1 and Host2 machines.
Now just stop and start the Tomcat Service.
To stop the Tomcat Service:-

To start the Tomcat Service:-

Now if you select Manager App in Tomcat Server it will ask for Username and Password but we don't have any username and Password for that Manager App.

So for this, we need to generate Username and password.
Let's do it.
Go to the tomcat folder --> conf --> tomcat-users.xml


Type the below lines in that tomcat-users.xml file. You can type these lines anywhere in the tomcat-users.xml file

My username and password for the Tomcat Manager App is nilesh.
Now again you have to stop the Tomcat service and start the Tomcat Service just like above we did.

Now we have to push that war file from the Ansible server to Host1 and Host2.
If you have a question that how to push a war file from Ansible Server to Host1 and Host2 machines.
For this, we need to write a Playbook to push the file.
Where we have to write this Playbook - Ansible Server machine
Let's do it.


Now move your host's file from /etc/ansible/hosts to your /home/ansible folder.

Now execute the playbook that we have created



Now you can see that web app folder in the Tomcat server.
All the Steps and Configurations are done successfully. Now it's time to access our application on the web.

Successfully I can access my application on the browser.
What is the manual work we are doing here is executing playbook. I dont want to execute that playbook manually, I want to automate the execution of playbook.
Let's do it.
How can we automate this task. Go to the Jenkins Server and create one new job to automate the execution of playbook.

Go to the Post Build Action --> Exec Command and try to give full path where your playbook file present.

If we build this job, it will pull the code from the GitHub and build the code using Maven and create a war file in Jenkins Server. Then Jenkins serveer will push the code to the Ansible-Server and then Ansible server push the code to the Host1 and Host2.
Here we are doing deployment war file on to the Host1 and Host2.


Now I want to execute my Jenkins job automatically whenever there is some changes in code which is present in the GitHub repository.
For this, I need to do Poll SCM.
Go to your third job --> Configure --> Poll SCM

It will go to the Github repo and check whether there is some changes done are not.
Let's do some changes in your code.



Job is successfully executed.

This is an amazing project guys. Please try to build it from your end and if you facing any issues please reach out to me. Its my pleasure to help you.
Conclusion:-
In conclusion, implementing a complete CI/CD pipeline using Jenkins can provide significant benefits to organizations looking to streamline their software delivery processes. By automating the different stages of the pipeline will speed up cooperation, decrease the likelihood of errors, and cut down on the time and effort needed to deploy software.
The proposed solution for the implementation of the CI/CD pipeline using Jenkins is a comprehensive approach that includes several stages, from source code management to testing and deployment.
