How Jenkins can actually help you , build your code with every commit

Dipto Chakrabarty
5 min readApr 12, 2020

If you have been using GitHub for sometime now you would have found it tedious to build your project every time you push a new change.

Let’s say your team is building a web app , you containerize your application and build a docker image out of it and now one of your team mates makes a significant change in the code base and pushes it to your GitHub repository.

Now there are two most common scenarios which may arise here , the first one being you overlook the or ignore the new commit in the repository (especially when the repository has multiple branches and almost hundreds of commits) in which case your team would still be using an older version of your product for testing and deployment.

The second case is that every time there is a new commit you build your project manually from beginning , this becomes tiring and repetitive when your product grows.

In both the cases it would be much easier and faster if every time there was a new commit your code would build itself up on its won and if there were any errors it would notify you at that moment only and the error would not be passed on further.

To do this task we can use a tool which is very popular known as Jenkins.

We will be building a simple flask web app and every time we push a new commit Jenkins will automatically build a new version of docker image and push it to docker hub.

For this you need Jenkins installed , if you don’t have it installed you can install it using ansible using this repository https://github.com/DiptoChakrabarty/Jenkins.

Once you have Jenkins installed setup a remote repository , in my case I am going to use this repository https://github.com/DiptoChakrabarty/pipeline.

Lets first build the flask app , I will build a simple hello world app .

Simple Hello World Application

and now the home.html page

The home.html page

Add a pom.xml file to your repository and now it is time to setup Jenkins.

Login to Jenkins and create a new job , give it any name you want

Once you are done with that the main thing is configuring the new job , click on configure to , once you are done that add your repository link in Github project under general , your screen should something familiar to this , make sure to select free style project.

When you create a new job
Configuration of the job

If you are not getting the option of Github repository , most probably your git and github plugins have not been installed and git has not been configured.

You can check this link to find out how you can configure these.

Further down under build triggers add a cron Job , which means after every commit start building , Poll SCM is nothing but checking if there is new code committed in your repository and build based on that.

After each commit to github it runs install goal.

Now if you need a successful build , you need to ensure that every time you push your code into the repository there is a new docker image built and pushed to docker hub.

To do that we need to install a few plugins required for docker.

Go to Manage Jenkins -> Manage Plugins

Install the following plugins , search for docker and install them , since I have them installed I have it under installed , if you are doing it for the first time you should do it under available and make sure you select install without restart.

Plugins required for docker hub and docker

Once done we can now push to Docker Hub by adding the name you want the docker image to be made , the format should be <your username>/<image name>.

Also add your docker hub credentials , for that we will add our credentials in Jenkins which is shown below .

Docker Hub setup

To add credentials go to Credentials page where click on the admin user.

The Credentials page

Once there on admin page click add credentials and go ahead and add your docker hub credentials.

Add your credentials

Your credentials should be visible similar to the docker hub setup image above.

Once done click on save and the job will be configured.

Okay now push to your repository and check whether build is successful or not.

You can see that after every build Jenkins is building my docker image.

To view your build click on your job and press view console output.

You can notice the commit message present there each time you commit.

if your build is unsuccessful your output will be as such.

Unsuccessful build

Every time your build is successful the image is pushed to docker hub.

Successful build

You can see this video of a small demonstration I made https://www.youtube.com/watch?v=d00IZPctdCU which can help clear things up on how it works.

Now even if multiple people commit together every time your new docker image will be built and if the build is successful it will be uploaded on docker hub.

If the build is unsuccessful you can setup mailing each time such a thing occurs then this helps ensure that your team is not pushing bad code.

--

--

Dipto Chakrabarty

Site Reliability Engineer , I talk about Devops Backend and AI. Tech Doctor making sure to diagnose and make your apps run smoothly in production.