Automatic code fetch from GitHub using Jenkins and Docker but with a twist

Automatic code fetch from GitHub using Jenkins and Docker but with a twist

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 using build pipeline plugin in Jenkins 

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

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

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

8. Create One extra job job4 for monitor : If container where app is running. fails due to any reson then this job should automatically start the container again.

So lets start:

Step1: Create Dockerfile and start jenkins.

No alt text provided for this image

 We use centos for installing jenkins. For jenkins to run we need java therefore we have to install jdk package. For various commands we need sudo, therefore we RUN sudo to install it. And now the challenge is that when we launch this container it should start Jenkins automatically therefore we use CMD with the path.

Then after creating the Dockerfile we need to build and we launch the image to check if it is working fine:

docker build -t jen:v1 .
docker run -d -i -t -P --name jenos jen:v1

Step2: For jenkins to be able to do the jobs we need certain plugins. They are:

  • GitHub Plugin
  • Build Pipeline Plugin
  • Email Notification
  • Email Extension Template Plugin

Now lets move on to the jobs:

Job1(j1): Pull files from GitHub

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

This will check continuously if any code is pushed.

No alt text provided for this image

We are done with Job1 now lets move on to Job2.

Job2(j2):Identify the type of file and assign container accordingly.

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

It builds only if Job1 is successfully built.

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

Now it checks if it is html or php file and runs the containers accordingly. We are done with Job2 now.

Job3(j3): Checks status code and notifies accordingly.

No alt text provided for this image

It builds only if Job2 is successfully built.

No alt text provided for this image

Using curl command it checks for status code. If it returns 200, that means the code is error free otherwise not.

No alt text provided for this image

If the code has error then it will send an email to the Developer. For Jenkins to send email successfully you have to disable 2 step verification and allow access to less secure apps . If you do not want to do this, then you can write a program accordingly to do so.

Job4(j4): Monitor if the containers are working fine.

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

Continuously checks if the container is running. If it fails then jenkins will restart.

Build Pipeline Vizualization:

No alt text provided for this image

We can see that job3(j3) has failed therefore it sens email.

No alt text provided for this image

Now we are done with the task of automating the process.

If you have any doubts you can email me at shirshadatta2000@gmail.com.

Thankyou. Keep connecting to know how we manage the process more intelligently using Kubernetes(K8S).



Ayush Sinha

Cloud Developer 2 at Hewlett Packard Enterprise

4y

It was really very helpful and a well written article 👍🏻

Like
Reply
Manan Jaiswal

Technology Consultant at EY || ServiceNow Developer || 6x CIS || CSA || CAD

4y

Well done!

Like
Reply

To view or add a comment, sign in

More articles by Shirsha Datta

Insights from the community

Others also viewed

Explore topics