Table of contents
In this project, I am creating a pipeline which can automate the software lifecycle of the company or project.
Automating the deployment process from code to production is a best practice in modern software development. By implementing this process, developers can ensure that the code is properly tested before being released to end-users, reducing the risk of errors and downtime.
In this project, I have implemented an automated deployment process where code changes are pushed to a testing server first. The testing server runs a suite of automated tests to verify the stability and functionality of the code changes. If the tests pass, the code is automatically deployed to the production server, where it is made available to end users.
By automating this process, we ensure that our software is always stable and functioning correctly. This reduces the risk of downtime and errors and helps us to deliver a better user experience to our customers. This automated deployment process saves time and effort, allowing developers to focus on writing code and delivering new features, rather than worrying about deployment
Creating a Master branch for managing tasks
Installing Ansible
Setup an AWS EC2 Instance
We need 3 EC2 Instances (Master and Test)
Log in to an AWS account using a user with admin privileges and ensure your region is set to us-east-1
N. Virginia.
Move to the EC2 console. Click Launch Instance.
For name
use Jenkins-master
For name
use Jenkins-Test
For name
use Jenkins-Prod
Select AMIs as Ubuntu
and select Instance Type as t2.medium
. Select my keypair and select the default security group
Jenkins-master Instance
sudo nano ansible_install.sh
I am creating a bash for installing Ansible, the below code is contained by ansible_install.sh file
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible -y
I am executing the above shell script by using the command
bash ansible_install.sh
I added private IPs of my Test and Prod server into my hosts of the Master Ansible
cd /etc/ansible/
sudo nano hosts
Updation of the packages in both machines is required, I am updating my Test and Prod machine by using the command
sudo apt update
Now we need to add the public key of the Master in our both Test and Prod server. Generating a public key
sudo ssh-keygen
we need to copy this key and paste it to our Test and Prod server
After adding this key in our server we will be able to ping our server in the master server
ansible -m ping all
Installing Jenkins and Docker
Creating 2 files
jenkins_install.sh
For now, I am going to create a jenkins_install.sh file to execute all the code at once in the playbook
This file contains
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
sudo apt-get install jenkins -y
sudo apt update
sudo apt install openjdk-11-jdk -y
sudo apt install docker.io -y
I have created the dependencies.sh file for installing Java and Docker in my Test and Prod server
sudo apt update
sudo apt install openjdk-11-jdk -y
sudo apt install docker.io -y
Creating a Playbook
---
- hosts: localhost
become: true
name: Install Jenkins, Java and Docker
tasks:
- name: master task
script: jenkins_install.sh
- hosts: Test
become: true
name: Install Java and Docker
tasks:
- name: Test task
script: dependencies.sh
- hosts: Prod
become: true
name: Install Java and Docker
tasks:
- name: Prod task
script: dependencies.sh
To execute the playbook, use the command
ansible-playbook (file name)
Master Server, I am going to add port 8080 to launch Jenkins
Copy the pink highlighted code line and paste it to the master server to get the password for Jenkins
Install suggested plugins
Setup basic details in Jenkins
After setup all the details it will look a like
Adding Nodes in Jenkins
Select Launch agent via SSH in Launch Method
Add credentials (SSH Username with Private Key)
Adding Private Key directly, paste Private pem key here
Host key verification would be Non Verifying and then save it
My Test Node is added now in my Jenkins
In my Test Server, Jenkins is now showing but we don't have workspace directory here
The same way I'll do this for Prod Server and all the steps are the same
In Prod Server, the Jenkins folder is available without a workspace
Creating Master Testing Job in Jenkins
I am creating my freestyle first job by the name of Master_Testing82, in this 82 means port no. 82
In Master Server, I am cloning the git repo in my server by using the command
git clone https://github.com/BroDevOps/website.git
I am creating Dockerfile by using the command
sudo nano Dockerfile
Inside Dockerfile I am writing this code, I am installing apache2 because for launching any website we need a web server.
ADD ./var/www/html I am using to save html page get save into this path
-D It is a detached mode it helps to run the website even if the machine is closed.
FROM ubuntu
RUN apt update
RUN apt install apache2 -y
ADD . /var/www/html
ENTRYPOINT apachectl -D FOREGROUND
Now we have 3 things here
After adding Dockerfile, we need to commit it and push it to the GitHub
git add Dockerfile
git commit -m "first commit"
Adding Webhooks
I Opened Jenkins and select Github hook triggers but I will not save this first
Then I went to GitHub, opened the setting of the website repository and clicked on webhooks
After entering the Jenkins URL, I simply clicked Let me select individual events so I selected Pull request and Pushes
First I ran a build now to test whether it was working or not
Now I am pushing my Dockerfile to GitHub by using the command
git push origin master
After entering this will ask you to enter a username and token for pushing the commit code to the GitHub server.
In Jenkins Server
The build will start automatically
In Test Server
Now we do have a workspace, only because we had to push our Dockerfile
I got all three files here
In Jenkins, I am creating the automation where it builds the Dockerfile and the container got created automatically without manual intervention.
In the Build step, clicked to Execute shell
sudo docker rm -f $(sudo docker ps -a -q)
sudo docker build /home/ubuntu/jenkins/workspace/Master_Testing_82 -t masterimage
sudo docker run -itd -p 82:80 --name c1 masterimage
Basically after the docker build I entered the path and after -t I gave the name to the image
After saving this, and Clicked to Build Now, I was able to run perfectly after 14 builds
It's perfectly deployed on my Test server under the Docker container
Creating Testing Develop Job in Jenkins
I am creating another job by filling required details
The branch should be develop this time to perform the task
Now need to configure a webhook trigger in the Jenkins configure
Rest things default, just clicked to apply and save, Now I need to create develop branch in my Master server
First clicked on to git branch, and then I added the git develop branch here
git branch develop
git push origin develop
I pushed all the code to the git develop branch
Now I have the same code and file in develop branch
I created the pushed activity so my Jenkins server created a build automatically
Testing through Develop Branch
I clicked on the Testing_Developer_83 job and configure Execute shell in build actions. Before that we need a path so went to the Test server because we need to select the right job
Build was successful
Now I am checking by using the Public IP of my Test server with Port no 83 this time and It is perfectly working fine
Now the testing part is done moving forward to the last job
Creating Final Release Prod Job in Jenkins
It will only run if the very first job was successful
This time we are not going to select Git webhook and Branches
Prod Server
In the Jenkins server I need to configure build steps again for my Prod Job
Now for executing this, I need to configure post-build action in my Master_Testing_82 and this will only run if the previous build is successful
Finally, I made a build in Master_Testing_82 and it was successful my Final_Release was also successful which is available directly in Downstream Projects
Thanks