Automation of a Pipeline via Docker and Jenkins.

B.V.Rohan Bharadwaj
4 min readJun 30, 2020

--

Task Objectives:

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. 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.

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

Procedure:

1.

Create a container’s image which has Jenkins in it .For that we can simply start of with a Dockerfile.

Docker will understand the listed commands from the Dockerfile , and installs the listed requirements.

Since Jenkins was built on Java, we need to have a JDK in our image. And Jenkins runs stably on openjdk (version of java-8 or java-11 is recommended).

As for our image we installed “ java-11-openjdk.x86_64 ” to run Jenkins .

Since we are working remotely on the container , it is crucial to have either SSH or PuTTY on it. we are using openssh-server .

Now we can build our image using the command:

docker build -t <image_name>:<tag> .

2.

Now we have to launch the image for hosting Jenkins and for this ,we can use the command:

docker run -it -p 90:8080 -v /root/task2/storage:/root/.jenkins --name <name> <image_name>:<tag>

After this , Jenkins will store the data in a directory ( /root/.jenkins ) this way we are just mounting a older into that specific directory inorder to access those files as the host. In this we also use PAT so that our traffic goes from 90 to the 8080 port of the container we launched.

Now a new Jenkins is launched

You may proceed through the set-up process as usual

And after that install the required plugins and we are all set to work with the Jenkins we launched from a container!

3. Creating the Jobs:

Job1:

This job is meant to download the content from the listed GitHub repo whenever a Dev updates the repo.

The reason we are using “Execute shell script on remote host using ssh” is to execute the cmds as a host , and also provide the username and <ip>:<port to jenkins>

Once that starts running , it’ll download the repo into our work space.

Job2:

This runs only once Job1 is successful, and its purpose is to check which interpreter to use.

Job3:

This job is meant to test the status of our site , if it works or not. If it’s not running the Dev will get a notification to his email.

Here we have options for sending a notification email to our Dev , like using SMTP plugin and so on~

To make it a tad bit more interesting, I used a python script which will send the email to the Dev when it’s triggered/executed.

Job4:

This Job’s sole purpose is for monitoring and restarting the container . So it’s built periodically to monitor from time to time.

With this , our process of automating a pipeline to monitor a repo using Git via Jenkins inside a Docker container is complete.

Thank you for the time.

--

--