Pipeline : Creating Testing Environment, Automated Testing, Merging And Deploying to Production Environment with Jenkins, Git&GitHub, Docker and Linux

Aman Pasi
6 min readMay 9, 2020

Step 1: Create GitHub Repository and get its link

As you can see I have already created a git repo with simple HTML and CSS to deploy.

Step 2: Create Testing Environment using Jenkins and Docker.

Start Jenkins using following Command:
$ systemctl start jenkins
Goto the Jenkins URL:
In my case I have set up it to http://127.0.0.0:8080
In your case it may be http://<your laptop/desktop ip>:8080
Use $ ifconfig command get your IP address.

Click on New Item to Create Job1(DevEnv) which will be responsible for creating Testing Environment.
Give a name to job ( I have given DevEnv) and choose freestyle project.

Click on GitHub Project
Put your GitHub repository link in it.

In Source Code Management, choose Git and Enter your GitHub repository URL.
Since this job will create Testing Environment, set the branch you want to test, merge and deploy (as you can see above in my case its dev1 branch).

I am going to trigger this job and build only when there is a PUSH commit to my dev1 branch.
So I have chosen GitHub Hook which will be triggered by Github-WebHook.
We will set up GitHub WebHook in later part.

This is the code to deploy our Testing Environment using docker.
Code:
if sudo mkdir /TestingEnv/
then
echo “Directory Created”
else
echo “Directory Already Exists”
fi
sudo cp -rvf * /TestingEnv/
if sudo docker ps -a | grep testEnv
then
echo “Stoping and Removing Any Previous Testing Enviorment”
sudo docker stop testEnv
sudo docker rm testEnv
echo “Deleted Previous Enviorment”
fi
sudo docker run -itd -v /TestingEnv/:/var/www/html/ -p 8081:80 — name testEnv vimal13/apache-webserver-php

As you can see first is else checks if there is existing directory or not and if not then creates the directory and copies the files from GitHub repo(dev1 branch) to a directory TestingEnv.
In next if else, we check if the container is running or not.
If the container is running, it is good to create a fresh environment for Testing. If not then it creates the container for us.
We create container with volume TentingEnv attached to the container and expose the port 80 of container to port 8081 of the servers/os.
We can save this Job

Step 3: Creating Testing Job

Go to Jenkins Home Page and Click on New Item to create a new Job.
Give the Job a name (I have given name “Testing”).

This is Job is for testing purpose, so it will run only if Testing Enviorment has been created. So we chose <Build after other projects are built> option and select DevEnv which will create Testing Enviorment.

For testing I have written a small script to check the status of the website.
You can put your own script for testing.
Testing Job is complete and we just have to save it now.

Step 4: Creating a Job which will merge the branch dev1 and master(production branch) if testing will be passed.

Go to Home Page and click New Item to create the Job and name it (In my case I have given name “Merge”)

Select GitHub Project and put you GitHub URL.

In SCM, choose Git and put your GitHub repo URL.
Add credentials for GitHub since you will need them to push the code after merging. Don’t forget to set your branch(in my case dev1) which you have to merge.

In Additional Behaviours, choose Merge before build option(scroll in options if you don’t find at first). Set the details as I have done (Enter whichever branch you have to merge to).

Since this Job will be triggered by Testing Job, in Build Triggers
we will choose <Build after other projects are built> option and add Testing in it.

You have completed all the necessary Jobs for Testing Phase.
Only thing left is to make Production Environment.
So lets finish it too…

Setp 5: Create Job to deploy Production Environment using Docker

Go to Home Page, Click New Item and give a name to the job.
In my case I have given name “ProdEnv”

Set the project as GitHub Project and enter your GitHub URL.

In SCM, choose Git and Enter the GitHub repo URL.
Also specify the branch that you want to build i,e your production(master) branch in this case.

This Job will be an independent job and will be built once there the been any PUSH events to the master branch. PUSH information will be sent by GitHub using Github Webhooks. So choose GitHub Hook trigger.

The following script will deploy our Production Environment using docker.
First it will copy master branch files to “ProductionEnv” directory and then it will be mounted on the container. Port 80 has been exposed to our Linux System and Hence we can see it at our localhost:80.

Step 6: Set Up Ngrok

We are almost there. Just need to set up our WebHook in Github.
For this I am going to use ngrok to expose port 80, 8081 and 8080 to internet.
You will need to download ngrok and run the following command :
$ ./ngrok http 80 (to expose port 80)
I have already created an free account and added auth to ngrok configuration files so that I can use it for a bit longer (since without account it only give 7 to 8 hour to expose ports to internet) and also added some more information to configuration file(/root/.ngrok2/ngrok.yml) to expose port 80,8081 and 8080. It looks like this

after this I run ngrok using following command :
$ ./ngrok start — all (to run from configuration)

Step 7: Create GitHub WebHook

Go to your GitHub repository Setting →Webhooks → Add Webhook

Put you Jenkins link i.e the link which exposes port 8080 and add “/github-webhook/”.
GitHub will send all PUSH notification to this URL and Jenkins will do its job.

Congrats!!! You have Completed PipeLine.

--

--