Automation Using jenkins & docker

Arifiya Khan
4 min readJun 30, 2020

Hello !!

So here I did another task,where I did automation using jenkins,Docker,and Git.

The task is as follows:-

  1. Create container image that 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.If app is not working , then send email to developer with error messages.

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.

STEP-BY-STEP IMPLEMENTATION

As per the first step,We need to create an image using dockerfile.For this,I have created a new folder in docker and then created an image that launches jenkins and the jenkins service should automatically gets started.

Dockerfile

So now I have created my dockerfile.

To launch this image use the command,

docker build -t <image_name> /folder_name/
installing image

After this I will check if my jenkins service is running or not.

jenkins service running

So my jenkins service is running perfectly.Now the next thing I have to do is to push the files in github using git bash.

So for this I have made a new repo on my github account and will push the files on that repo.

adding files to my github repo

Now after this process is done.I will create jobs in jenkins

Job1

The first job copies the file received from the GitHub repository to the specified project folder.

As you can see that after running the first job,all the files from github gets downloaded by jenkins and copied in the folder given.

As soon as the first job is built stably , it triggers our next job that is our job 2 whose task is to launch the container from the respective image which has the required software installed. For this purpose we have constructed the python code which triggers the dockerfile and builds the image .

After this is built,our job2 launches the new container

Job2

job2

Let us see the output from the console output of job2,

output

job3

Now a new job is created which tests whether the application is running successfully or not and if the application is not running it sends an email to the developer regarding the issue.

To receive the mail change the security settings for a while,because gmail will block this account and hence you will not receive the mail.So I am changing the security and allow the less secure app access.

Now ,as you can see that I am receiving the mail

Job4

Job 4 just monitors the containers and if it is not present then launches it again.

So,let us take a view of my all the jobs running.

Also,I will build a pipeline view to give it a good and understandable look.

Hope my this blog would help.

If you will still got stuck somewhere,feel free to ask your queries.Also,you may suggest something more that I can do in it.

Thank you and Happy learning!!

--

--