Once the latest code is available on the Git repository, Jenkins is capable of fetching the updated code, initiating a build using Maven, creating Docker images, and subsequently deploying these Docker containers onto the Docker host.
In that case, we used Jenkins as a tool for building and used docker for deploying applications.
So in this case Jenkins is going to take the code from GitHub and build artifacts and copy those artifacts onto Ansible server.
Now it is Ansible’s task to create images and deploy the containers.Ansible is going to take the artifact and with the help of Docker file it creates a Docker image.
This Docker image, we can commit it into the Docker hub because Docker Hub is a repository to store Docker images.
Now whenever we execute any Ansible playbook to deploy a container, this Docker host communicates with the Docker hub and pull the image whatever we mentioned in our playbook and create a container out of it.
- Create 4 ec2 instance name as:- (AWS Linux-2, t2 micro)
- Jenkins-Server
- Ansible-Server
- Docker-Server
- Tomcat-Server
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- 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.
sudo su
cd ~
pwd
cd /opt
wget https://dlcdn.apache.org/maven/maven-3/3.9.4/binaries/apache-maven-3.9.4-bin.tar.gz
tar -xvzf apache-maven-3.9.4-bin.tar.gz
mv apache-maven-3.9.4 maven
cd maven/
cd bin/
./mvn -vcd ~
ll -a
vim .bash_profile- In the vi editor add this path :-
M2_HOME=/opt/maven
M2=/opt/maven/bin
JAVA_HOME=/usr/lib/jvm/java-11-amazon-corretto.x86_64
PATH=$PATH:$HOME/bin:$JAVA_HOME:$M2_HOME:$M2- Find java path using this command:-
find / -name java-11*- Showing maven & java path :-
source .bash_profile
echo $PATH-
Dashboard-> Manage Jenkins-> Pluins-> Available Plugins-> Maven Integration(Install this)
-
Dashboard-> Manage Jenkins-> Pluins-> Installed Plugins->github
-
GitHub Branch Source Plugin = Disable
-
GitHub plugin = Enable
- git installation on Jenkins server:-
yum install git -y- Jenkins-> New Item-> Maven Project
- In this job do this:-
- Maven Build is successful:-
ec2-user
sudo su
yum update -y
hostnamectl set-hostname jenkins
bash- add user
useradd ansadmin
passwd ansadmin ## enter passwd 2 times
visudo ## In vi editor go to end of the file = press shift+g without press i
ansadmin ALL=(ALL) NOPASSWD: ALL ## add this in editor
vi /etc/ssh/sshd_config ##Do this changes in vi editor:- ** #PermitRootLogin yes (remove #) ** PasswordAuthentication no (replace no to yes)- Now run this command:-
systemctl restart sshd- Now switch to created user:-
sudo su - ansadminyum install ansible -y
amazon-linux-extras install ansible2 -y
ansible --version- Go to ansible server and type command:-
ssh-keygen ##(and press enter 2 to 3 times)- Jenkins-> Manage Jenkins-> System-> Publish over SSH
- Go to Ansible
cd /opt
ls
mkdir docker
chown ansadmin:ansadmin docker ## Using this ansadmin is owner of docker directory
ls
cd docker/
ls-
Jenkins-> New Project-> Do this mentioned in Img-> Build Now

-
Now, webapp.war file is involved in docker directory
yum install docker -yid ansadmin
usermod -aG docker ansadmin ## provide all the access to ansadmin on the dockerservice docker start
service docker statussu - ansadmin
cd /opt/docker/
vi dockerfile ## add below commande in dockerfile
FROM tomcat:latest
RUN cp -R /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps
COPY ./*.war /usr/local/tomcat/webapps- In Ansivle Open host file
vi /etc/ansible/hosts
[Ansible] ## type this in editor
(paste public Ip here)ssh-copy-id (paste here ansible private ip)
## enter passwd 2 times- Create Ansible playbook to create the docker image and push image to dockerhub
vi regapp.yml ## paste below cmnds in editor
---
- hosts: ansible
tasks:
- name: create docker image
command: docker build -t regapp:latest .
args:
chdir: /opt/docker
- name: create tag to push image onto dockerhub
command: docker tag regapp:latest rutikdevops/regapp:latest
- name: push docker image
command: docker push rutikdevops/regapp:latest- check indentation of playbook
ansible-playbook regapp.yml --checkdocker images
docker login- Jenkins-> Project-> Configure
- Now, your image is pushed in DockerHub
- Do passwordless authentication between Ansible & Docker
- Install apache on docker server
yum install httpd -y
systemctl enable httpd
systemctl start httpd
systemctl status httpd- In Ansible Open host file
vi /etc/ansible/hosts
[docker] ## type this in editor
(paste public Ip here)ssh-copy-id (paste here docker private ip)
## enter passwd 2 times- Create Ansible playbook to deploy webapp on server:-
vi deploy_regapp.yml ## paste below cmnds in editor
---
- hosts: docker
tasks:
- name: stop existing container
command: docker stop regapp-server
ignore_errors: yes
- name: remove the container
command: docker rm regapp-server
ignore_errors: yes
- name: remove image
command: docker rmi rutikdevops/regapp:latest
ignore_errors: yes
- name: create container
command: docker run -d --name regapp-server -p 8082:8080 rutikdevops/regapp:latest- Now, regapp sussessfully host on server
- Virtual Techbox, YouTube:- https://youtu.be/5_s7EmZWz78




