ADVANCE TASK OF GITHUB + JENKINS + DOCKER

ADVANCE TASK OF GITHUB + JENKINS + DOCKER

This article is a simple integration of GitHub, Jenkins and Docker to automate the process of deploying a website by creating custom Docker images using Dockerfile.

Job1: Pull the Github repo automatically when some developers push the repo to Github.

Job2: 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 HTML, then Jenkins should start the container that has HTML already installed ).

Job3: Test your SYSTEM if it is working or not.

Job4: If the SYSTEM is not working, then send an email to the developer with error messages.

Create one extra job job5 for monitoring purpose: If a container, where the SYSTEM is running, fails due to any reason then this job should automatically start the container again.

Let's start with the workflow for this task:

To create one image containing various features installed like automatically start the Jenkins, Load the system which can automatically select an interpreter for the above task. We have to manually create one Dockerfile to launch the container with our own image.

No alt text provided for this image

Now, We will create an image by using the above Dockerfile named as jenkins:v1

docker build -t jenkins:v1 .

Now, let's create the container to run the Jenkins with docker image which we have created just now. Run the below command for the same:

docker run -it --name c1 --privileged -p 9090:8080 -v /:/host jenkins:v1

privileged is used for running docker in the base OS and -p is to expose the container port to the base OS, hence giving access to the user to connect. Now the port number 9090 is mounted onto the internal Jenkins port 8080.

Now, You will find one password from the container for the UserAdmin connection. When you try to sign in or launch the Jenkins on your windows using IP:1234, You have to enter the password for further connection.

No alt text provided for this image

Now, Let's start our Jenkins by visiting local server using windows or Rhel 8.

Before using jenkins, We can install Plugins required for our task which are listed as follows:

  1. Github Plugin
  2. SSH Plugin
  3. Email Notification Plugin
  4. SMTP Plugin

JOB 1:

After doing the successful setup of Jenkins in your localhost now create a new item to start our task, In the task as mentioned at the start, We have to build the first Job which can automatically pull repo from your Github Repo just by giving some triggers.

No alt text provided for this image
No alt text provided for this image

It is clearly showing in the above images, We have to copy the HTML file which we cant to display as output bypassing to job2 for running on the webserver. You can find shell command here.

sudo cp {file name} {dir}

JOB 2:

Let's move towards the Job2 and which is the tough part of this task. Here you have to use some logic part because this job should automatically detect your code and launch the respective code interpreter and launch the container for the same.

In my case, I'm using an HTML file so it should start httpd image container to run HTML file on the webserver. We will use here httpd image here which is officially available online. You can also create your own by creating Dockerfile as shown above.

No alt text provided for this image

We check the extension of the file to launch a container production for our application based on an httpd.

JOB 3:

This is just the simple testing job that checks the other jobs that are they working fine or not.


No alt text provided for this image

Also, In Execute shell add below commands:


export status=$(curl -o /dev/null -s -w "$(http_code)" https://{IP}:1234/mymap.html
if [ status==200 ]
then
exit 0
else 
exit 1
fi

HTTP code for the successful execution of server or file execution gives status as 200 and for failure is 500, Hence we used above http_code. Also, we added exit 0 or 1 to shown failure or success output for Jenkins output.

JOB 4:

Here come one more important part and interesting one in our task, That when you test the jobs running in the system and if build failure occurs than How developer will get to know about this situation?

For this, There should be some notification system for the automation task we made. So, Here Job4 will directly send a build failure message to the developer if system failure occurs.

Now, We have to download the Email notification Plugin for running this job and do some configurations.


No alt text provided for this image

Note: Don't forget to add the port number as 465 and you can also test the mail system is working fine or not.

Now our email notification options are configured.

Here is the below pipeline script for the same !!

No alt text provided for this image

If the app is not working , then error messages will be sent to the developer via email.

JOB 5:

This job will work in monitor the system, Like if the system fails then it will automatically start a new container to don't make a failure in the project.

We will use SCM polling for this, * here represents 1min.

No alt text provided for this image

We will add some commands which will be used for launching a new container after a system failure in the execute shell.

No alt text provided for this image

Now, Lets Build pipeline for monitoring the jobs.

Jenkins is, fundamentally, an automation engine which supports a number of automation patterns. Pipeline adds a powerful set of automation tools onto Jenkins, supporting use cases that span from simple continuous integration to comprehensive CD pipelines. By modeling a series of related tasks, users can take advantage of the many features of Pipeline.

- Install Build or Delivery pipeline plugins to manage pipelines in jenkins.

Configure and set the first job as GitHub pull repo which is my job1 for this task.

Finally I created a job of chain of all the jobs via build pipeline in jenkins.


FUTURE SCOPE :-

Leveraging the trio – Github, Jenkins and Docker is proving to be very valuable for DevOps teams. By leveraging the tight integration with source code control mechanisms such as Git, Jenkins can initiate a build process each time a developer commits his code. And streamline creation of the complete application application stack to support test, staging as well as production operations.


?THANKS FOR READING THE ARTICLE :-)

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

Harbind singh的更多文章

社区洞察

其他会员也浏览了