Jenkins configuration with Maven and GitHub

Git is the most widely used modern version control system in the world today, which allows multiple persons to safely work on the same project without hampering other team members. As a part of a team using Git, You and your team members will clone working copy of a local repository from Git server. You/team will add and commit the test scripts that are developed locally and push your changes to the Git.

We can make Jenkins to pull the project’s source code from the remote Git Server, by selecting the option in Source code management and specify the path/url can find the source code of the project.

In the below example, we will configure Git plugin and invoke testng.xml using maven from jenkins. You can have your selenium scripts/tests added to testng.xml. You can also parameterize your selenium tests using Maven-Testng. View previous article on how to configure simple maven project in jenkins

Configure Git Plugin in Jenkins

Step 1:- Manage Plugins -> Filter list of plug-ins availble with 'Git Plugin'. Find more details about Git Plug-in

Step 2:- Check the Git Plug-in and click on the button ‘Install without restart’

Step 3:- After the installations are done , Please restart Jenkins by using the command in the browser. http://localhost:8080/jenkins/restart

Once Jenkins is restarted, Git option should be available under source code management when configuring job.

Install Git in jenkins

Step 4:- From Manage Jenkins > Configure System, please provide the right Path to Git executable.

Git executable path in jenkins

Step 5:- Under Git Plug-in, Set the global git user.name and user.email to match your global config options

Git config values in jenkins

Now let us create a Maven Project and build a job from Git project

Step 1:- Click New Items -> Enter Project Name -> Select Maven Project -> Click OK

Step 2:- Provide the job description

Step 3:- In Source Code Management, Select 'Git' option. (This should be visible once you have successfully installed Git Plugin).

Git repo url in jenkins

Step 4:- From Build Triggers, If you want Jenkins to monitor the repository and start a build whenever any changes have been committed, We can choose to pick the Poll SCM option and enter syntax of cron.

The other options include Build periodically (for example, once a day, ), Build whenever a SNAPSHOT dependency is built etc.

Here "SNAPSHOT" means build is is still under active development. Ex;- If it is easy-1.0-SNAPSHOT.jar library, Maven will know that this version is not stable and is subject to changes. Before 1.0 release (or any other release) is done, there exists a 1.0-SNAPSHOT version.

The difference between a "real" easy-1.0 version and a easy-1.0-snapshot version is that snapshots might get updates. That means that downloading 1.0-SNAPSHOT might give you a different code than downloading it yesterday or tomorrow.

Builds Periodically will trigger builds as per the schedule (If we specify H/5 * * * *, every 5 minutes) even if you haven't changed anything. Poll SCM will check for changes before triggering any build, if there are changes to the previous version then only the build will be triggered.

Both these fields follows below syntax of cron which consists of 5 fields separated by TAB or white space: -

MINUTE (0-59), HOUR (0-23), DAY (1-31), MONTH (1-12), DAY OF THE WEEK (0-7)

MINUTE - Minutes within the hour (0–59)
HOUR - The hour of the day (0–23)
DOM - The day of the month (1–31)
MONTH - The month (1–12)
DAY OF THE WEEK - The day of the week (0–7) where 0 and 7 are Sunday.

Example:-
# If you want to run job for every fifteen minutes, we should specify cron syntax as below :
H/5 * * * *

#If you want to trigger for, every 2 hours
H */2 * * * to distribute load evenly throughout the hour

In addition to the above, Jenkins also support convenient aliases as @yearly, @annually, @monthly, @weekly, @daily, @midnight, and @hourly.

Git build trigger in jenkins

[Optional]

Step 5:- In Build Settings, to send an email notifications, you can check 'Email Notification' and add Recipients address separated by comma.

Step 6:- In Post Build Actions, You can chose steps such as Archive Artifacts, Publish Results etc.

Step 7:- Click on Apply and Save.

Now as per configuration above, the build will trigger for every 5 minutes.

Once when the build is triggered, you can check for the console output, which will show you something like below image :-

Git repo triggered in jenkins

And also when you view the build information, it will show you something like the below image which has information like who started and from where the build is triggered:-

Git repo triggered in jenkins

As we are triggering build for every 5 mins, you can see the jobs running every 5 minutes (shows in the below image).

Git repo triggered in jenkins

Hope this article helps you. Please let me know if you have any suggestions / issues.

Build Tools: 

Comments

Hello Mukesh,
Can you please explain, what a snapshot dependency is?

Dear Moderators,
This website has a tremendous collection of information and is helping continuously to gain knowledge. But, the content is overlapping with the right side of the advertisements, Tags Cloud section and popular posts section. Kindly manage the formatting, so that the information is complete and easy to understand.

Thank you!

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.