CI/CD Pipeline Using GitHub, Jenkins And Docker

CI/CD Pipeline Using GitHub, Jenkins And Docker

Jenkins is an open source automation server written in Java. It is used to continuously build and test software projects, enabling developers to set up a CI/CD environment.

CI/CD is Continuous Integration and Continuous Deployment.

Moving each day step towards Automation. Herewith another mini-project showing automating CI/CD pipeline through Git Docker and Jenkins.

Docker another tool designed to make it easier to create, deploy, and run applications by using containers in a loosely isolated environment. The isolation and security allows to run many containers simultaneously on a given host. It launches a container within seconds!

Task Description -

1. Create container image that’s has Jenkins installed using dockerfile.

2. When we launch this image, it should automatically starts Jenkins service in the container.

3. Create a job chain of job1, job2, job3 and job4 using build pipeline plugin in Jenkins.

4. Job 1 : Pull the Github repo automatically when some developers push repo to Github.

5. Job 2: By looking at the code or program file, Jenkins should automatically start the respective language interpreter install image container to deploy code ( eg. If code is of PHP, then Jenkins should start the container that has PHP already installed ).

6. Job 3 : Test your app if it is working or not. If app is not working , then send email to developer with error messages.

7. Job 4 : If container where app is running. fails due to any reson then this job should automatically start the container again

Pre-requisite :

  • OS: Base OS is Windows 10. Server OS is RedHat Enterprise Linux 8 (RHEL8) in Virtual Box.
  • In RHEL8 some of the softwares needed are Docker (also need the php image downloaded in it), Jenkins (also github, build pipeline and email extension plugin should be installed in it).
  • In Windows we need git bash software.
  • At the starting stop the firewalld in RHEL8 and start the docker and jenkins services.

Let's see step by step how to achieve this :

1. Creating Dockerfile and Building Personal Image using it :

  • Here I create one Dockerfile and run the build command. Remember one thing you have to run this command particularly on that folder where your  Dockerfile exits.
  • Here we have three important noticeable points.
  1. FROM : the image to be used for container
  2. RUN : commands to be executed while building the modified container
  3. CMD : the command used here will keep the Jenkins live till the container is on and will start on container boot.
  • We will be installing wget for downloading the files from url, and before moving further we will set up the yum repository for Jenkins from URLs using wget command. Then we will first install java as it is a prerequisite for the smooth functioning of Jenkins.
  • We will make a Jenkins entry in the sudoers file to give it root powers without the requirement for any password. We will also require Git installed to be used in Jenkins as a plugin.

Here is the Dockerfile :

No alt text provided for this image
  • Here is the command to build. To build the image we will use: ** . is to be replaced with the path of Dockerfile

docker build -t myjenkins:v1 .

No alt text provided for this image

2. Running the container and collecting needed informations :

  • Here is the command of docker run : 

docker run -it --privilaged -P -v /:/host --name first_os myjenkins:v1

  • Here we have three important noticeable points.
  1. "-P" means it will go inside the image's configuration and will look for which port is exposed. It will then allocate one random port and link that with that exposed port.
  2. "-v /:/host " this command will attach our base os absolute directory to the container.
  3. " -- privilaged " this will allow our docker to go inside base os and from inside container we will be able to run any commands in base os.
No alt text provided for this image

Now observe this two below mentioned images :

Here you will find the initial password of jenkins. This is needed to unlock Jenkins for the 1st time.

No alt text provided for this image

Run these two commands "docker ps" (this is to find the allocated port number) and "ifconfig enp0s8 " (this command will tell you the ip address of you base os ).

No alt text provided for this image

Now type your ip address along with port number and then copy paste the initial password in the starting page of Jenkins.

No alt text provided for this image

3. Jenkins Job1 :

Follow the below mentioned picture and build the 1st job in Jenkins.

  • Go to the 'configure' of your job and fill these data. First give the URL of your project repository. Next in SCM select git and fill the repo URL and select the branch to master .
No alt text provided for this image
  • And in build trigger just select Build Trigger Remotely and give a desired token name. This will help us to trigger jenkins as soon as we push our code to github remotely.
No alt text provided for this image
  • Next this is the code which gonna help to copy our files from Jenkins Workspace to our desired folder.
No alt text provided for this image

4. Jenkins Job2 :

Follow the below mentioned picture and build the 2nd job in Jenkins.

  • In this job we are deploying our code. Here we select one different trigger. Means as soon as job1 will complete job2 will start automatically. Also this job at first check if in our folder have any php or html code exist or not. If yes then it will start the container accordingly.
No alt text provided for this image

chroot command in Linux system is used to change the root directory. Every process/command in Linux like systems has a current working directory called root directory. It changes the root directory for currently running processes as well as its child processes.

A process/command that runs in such a modified environment cannot access files outside the root directory. This modified environment is known as “chroot jail” or jailed directory. Some root user and privileged process are allowed to use chroot command.

chroot” command can be very useful:

  • To create a test environment.
  • To recover the system or password.
  • To reinstall the bootloader.

So firstly I have created new environment using chroot command . Then checked whether the file is of html or php  then accordingly lauched the container .

No alt text provided for this image

5. Jenkins Email Configuration :

Follow the below mentioned picture and configure your email address with Jenkins to get notifications :

  • Goto The Manage Jenkins , then Configuration and scroll down and fill up the email notification portion.
No alt text provided for this image

6. Jenkins Job3 :

Follow the below mentioned picture and build the 3rd job in Jenkins.

  • Here again use the previous trigger. This code will check if our php or html code is working fine or not.
No alt text provided for this image

So firstly I have stored the status code of my webpage in status variable . Generally status code 200 represents that the website is running fine . So If status == 200 then webpage is running absolutely fine and job will terminate with exit status 0 (Success) , else it will terminate with exit status 1 ( Fail ) .

No alt text provided for this image
  • If our php or html code has any error then it will send notification to the developer. Follow the below picture.
No alt text provided for this image

7. Jenkins Job4 :

Follow the below mentioned picture and build the 3rd job in Jenkins.

  • This job will keep on checking each minutes if my docker container is running or not.These 5 start means each minute this job will automatically triggers itself.
No alt text provided for this image
No alt text provided for this image

8. Creating Build Pipeline View :

  • Just simply create one new My view in Jenkins and select Build Pipeline. Then at the end only mention job1 and you are done.

So, the work is done , Now let's see how to test the setup :

Follow the Below pics:

  • At first pull the repo from github and go to the master branch of it. Next configure the post-commit. Go inside the folder .git/hooks/ and create the post-commit
No alt text provided for this image
  • Go to the website and type your webaddress of your server and you will see the page working fine
No alt text provided for this image
  • Next you can see in below picture I made one mistake in the php code. So, I pushes the wrong code. Now you will see that automatically Your Job3 will fail, that means it detects the problem.
No alt text provided for this image
  • If you go to the page you will see that it's not working.
No alt text provided for this image
  • Here you can see that it sends the email notification as soon as job3 fails.
No alt text provided for this image

Finally one last thing let's see what our Job4 is doing :

  • If you see job4 build history you can in the below picture that it keeps on an eye on our container and if anyhow container stops it gonna start that again.
No alt text provided for this image





Priyanshi Garg

MTS @athenaheath

4 年

Well done NIDHI JAIN ??

回复
Rupali Gurjar

Software Engineer II @Cvent | Tech Blogger | Aws-CSA | MLOPS | DEVOPS | Hybrid Multi Cloud | GCP | Ansible | Docker | Terraform

4 年

Good Job Nidhi !

回复

要查看或添加评论,请登录

NIDHI JAIN的更多文章

  • JENKINS AND GROOVY

    JENKINS AND GROOVY

    Jenkins is an incredibly powerful tool for DevOps that allows us to execute clean and consistent builds and deployments…

  • Integrating Prometheus & Grafana using Kubernetes

    Integrating Prometheus & Grafana using Kubernetes

    What is Prometheus Prometheus is an open-source systems monitoring and alerting toolkit originally built at SoundCloud.…

  • CI/CD Pipeline with Dynamic Distributed Clusters

    CI/CD Pipeline with Dynamic Distributed Clusters

    A CI/CD Pipeline implementation, or Continuous Integration/Continuous Deployment, is the backbone of the modern DevOps…

    2 条评论
  • CI/CD pipeline using Jenkins and Kubernetes

    CI/CD pipeline using Jenkins and Kubernetes

    Use Kubernetes resources like Pods, ReplicaSet, Deployment, PVC, and Service. Task Description step1.

  • Integration of Github with Docker and Jenkins

    Integration of Github with Docker and Jenkins

    DevOps - It is a set of practices that combines software development (Dev) and IT operations (Ops). It aims to shorten…

    2 条评论

社区洞察

其他会员也浏览了