Github+Jenkins+Docker Adv Task

Github+Jenkins+Docker Adv Task

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 jenos:v1

No alt text provided for this image
docker build -t jenos:v1 .

?Note: You should have patience while creating images as it takes 10-15 mins.

No alt text provided for this image

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 1234:8080 -v /:/host jenos: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 1234 is mounted onto the internal Jenkins port 8080.

No alt text provided for this image

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
  • To know, How to find IP and start docker and many more basic things kindly refer my previous blogs.
Blog1, Blog2

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

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

Here, 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
No alt text provided for this image

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.

docker pull httpd
systemctl start httpd
No alt text provided for this image
No alt text provided for this image
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.

No alt text provided for this image

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

No alt text provided for this image
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.

No alt text provided for this image

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

No alt text provided for this image

Now, Lets Build pipeline for monitoring the jobs it can be done as follows:

- Install Build or Delivery pipeline from manage pipelines.

No alt text provided for this image

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

So, Here is the beautiful output for the above task.

No alt text provided for this image

Future Scope:

  1. This is just simple task where tried to load some basic HTML code but we can also add big python or ML models inside this container and directly deploy to make it run in just one-click and there will be no manual work of data scientist to keep changing values and train model again and again this task will be done by docker or Jenkins.
  2. Also, To launch many containers in one go, We can use Kubernetes in place of dockers.
Github
LinkedIn
Thanks for reading...

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

社区洞察

其他会员也浏览了