In this article, we will look at triggering the build automatically through Jenkins job when GIT push happens.

Below are the steps

Step 1:

Install the required prerequisites.

  • Install Jenkins
  • Install Java JDK (Above version 8)
  • Install Apache Maven 3.8.6
  • Post installation of Jenkins, install ‘Maven Integration’ plugin
  • Install ngrok
  • Assuming you have GitHub Repository codebase

Step 2:

Configure Jenkins

  • Under Manage Jenkins>>Global tool configuration

.  Maven ->Add MAVEN_HOME variable value (i.e. path of the maven file on your system)

. Java -> Add JAVA_HOME variable value

Step 3:

Configure GitHub webhook

            A webhook is an HTTP callback, that occurs when something happens through event notification via HTTP_POST. A GitHub webhook in Jenkins triggers when developer commits some code in GIT to master branch.

               As I have installed Jenkins locally, we need to get the payload URL which connects to the internet so let’s use ngrok for this. Post installation of ngrok, get to the directory and give below command

ngrok http 8080

You will get the screen like below with the payload URL

Under Settings of GITHub Repository, click on ‘Settings

Choose ‘webhook’ -> ‘Add webhook’ -> Provide ‘Payload URL’ and choose ‘Content type’ as ‘ Application/json’ and save it.

Step 4:

Setup new maven build job in Jenkins

  • Choose ‘create new job’ and enter the ‘name’ and choose maven project
  • Configure GitHub repository that you want to trigger build automatically when commit happens
  • Under Build Triggers section, choose ‘ GitHub hook trigger for GITScm polling
  • Under Build section, provide POM.xml path in ‘ Root POM’ and choose ‘Run regardless of build results’ in ‘Post Steps

This is it. Job setup is complete.

Step 5:

Commit the code with changes

Step 6:

Verify whether the build is triggered when git commit happens

We can see that build is triggered and its highlighted in blue highlighted box.

Hope this article will be helpful.