DevOps Project

Piyush Davda
6 min readMay 17, 2020

Task Description

  • 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. If app is not working , then send email to developer with error messages.
  • 7. Job4 : If container where app is running. fails due to any reason then this job should automatically start the container again.

Environment

  • Base OS- Windows 10
  • Server OS :-RedHat Enterprise Linux 8 (RHEL8) in Virtual Box
  • Docker installed in RHEL8 and firewall stopped.

Lets get into it

Step 1 — Created personal docker image using dockerfile and run it

  • then build that image using this command

docker build -t task2:v1 .

  • after build we have to run container using this command

“docker run -it — privileged -p 20101:8080 -v /:/host — name myos1 task2:v1”

  • Here we have three important noticeable points.
  1. "-P" means it will go inside the image's configuration and will look for which port is exposed. It will then allocate one random port and link that with that exposed port.
  2. "-v /:/host" this command will attach our base os absolute directory to the container.
  3. " --privileged" this will allow our docker to go inside base os and from inside container we will be able to run any commands in base os.
  • Now observe this two below mentioned images : Here you will find the initial password of jenkins. This is needed to unlock Jenkins for the 1st time.
  • Run these two commands "docker ps -a" (this is to find the allocated port number) and "ifconfig enp0s3" (this command will tell you the ip address of you base os.
  • Now type your ip address along with port number and then copy paste the initial password in the starting page of Jenkins.

Step 2: Jenkins Job1

Follow the below mentioned picture and build the 1st job in Jenkins.

  • Go to the ‘configure’ of your job and fill these data. First give the URL of your project repository. Next in SCM select git and fill the repo URL and select the branch to dev1.
  • And in build trigger just select Build Trigger Remotely and give a desired token name. This will help us to trigger jenkins as soon as we push our code to github remotely.
  • Next this is the code which will copy your files from Jenkins Workspace to your desired folder.

Step 3: Jenkins Job2

Follow the below mentioned picture and build the 2nd job in Jenkins.

  • In this job we are deploying our code. Here we select one different trigger. Means as soon as job1 will complete job2 will start automatically. Also this job at first check if in our folder any php code exist or not. If yes then it will start the php container.

Step 4 : Jenkins Email Configuration

Follow the below mentioned picture and configure your email address with Jenkins to get notifications :

  • Goto The Manage Jenkins , then Configuration and scroll down and fill up the email notification portion.

Step 5: Jenkins Job3

Follow the below mentioned picture and build the 3rd job in Jenkins.

  • Here again use the previous job for trigger.
  • This code will check if your php code is working fine or not.
  • If our php code has any error then it will send notification to the developer. Follow the below picture.

Step 6 : Jenkins Job4

Follow the below mentioned picture and build the 3rd job in Jenkins.

  • This job will keep on checking each minutes if my docker container is running or not.These 5 stars means each minute this job will automatically starts it self in minute.

Step 7: Creating Build Pipeline View

  • Just simply create one new My view in Jenkins and select Build Pipeline. Then at the end only mention job1 and you are done.

So, the work is done , Now let’s see how to test the setup :

Follow the Below pics:

  • At first pull the repo from github and go to the dev branch of it. Next configure the post-commit. Go inside the folder .git/hooks and create the post-commit. Follow the below picture and do the steps.
  • so this will push my changes automatically when I commit.
  • I set github webhooks also to trigger jenkins job.
  • For this I used ngrok to make my private server to public like this below attached photoes for that.
  • Now when I commit from dev1 branch It will automatically run job1 like this and then job2 and job3.
  • And my website is working on port 8085 which I have given in job1.
  • Next you can see in below picture I made one mistake in the php code. So, I pushes the wrong code. Now you will see that automatically Your Job3 will fail, that means it detects the problem and my website is not working on same port.
  • So when my job3 fails it send mail to developer like this.

Finally one last thing let’s see what our Job4 is doing :

  • If you see job4 build history you can in the below picture that it keeps on an eye on our container and if anyhow container stops it gonna start that again.

Future Posibilities

  • We can use kubernetes for managing containers and scaling.
  • We can also use this type of sysyem in cloud.

Thanks for reading my article.

Visit my website.

--

--