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.
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
This will check continuously if any code is pushed.
We are done with Job1 now lets move on to Job2.
Job2(j2):Identify the type of file and assign container accordingly.
It builds only if Job1 is successfully built.
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.
It builds only if Job2 is successfully built.
Using curl command it checks for status code. If it returns 200, that means the code is error free otherwise not.
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.
Continuously checks if the container is running. If it fails then jenkins will restart.
Build Pipeline Vizualization:
We can see that job3(j3) has failed therefore it sens email.
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).
Cloud Developer 2 at Hewlett Packard Enterprise
4yIt was really very helpful and a well written article 👍🏻
Technology Consultant at EY || ServiceNow Developer || 6x CIS || CSA || CAD
4yWell done!