This content is part of the Essential Guide: Master Git basics and branch into DVCS

Essential Guide

Browse Sections
Manage Learn to apply best practices and optimize your operations.

How to use the Jenkins Git Plugin: Tips and tricks

The Jenkins Git Plugin is the cornerstone utility for connecting to both local and remote repositories, but the add-on provides much more than just a connection to source code repositories stored on GitHub, GitLab, Bitbucket or even the local file system. Here are five cool continuous integration functions, from the Jenkins Git push to the ability to perform a sparse checkout, which you probably didn't realize the Jenkins Git Plugin could do.

1. Poll the Git repository for changes

Once the Jenkins Git Plugin is installed and configured, your build jobs can poll any local or remote repositories for new commits. The Jenkins environment uses a cron job scheduling syntax, so polling the repository hourly, daily, monthly or any combination thereof is possible. If new commits are detected, the Jenkins Git Plugin will kick off the build.

It should be noted that the Jenkins Git Plugin will be looking for commits on only specific branches, so the topic branches developers use to create features and bug fixes won't likely trigger a build. But if someone merges a new feature into the master branch, the Jenkins build job will run.

Jenkins build trigger
This is how you configure Jenkins and Git build triggers.

2. Build multiple branches

Jenkins beginners often get the false impression that a build job can only be associated with a single Git branch, but nothing could be further from the truth. Does your Git environment have a feature, fix and master branch? Are there separate user story branches or branches that map to long-lasting Agile epics? You can have the Jenkins Git Plugin build against each of these branches every time it is invoked.

build multiple branches
Jenkins Git Plugin enables you to build multiple branches in a single job.

3. Merge before build fix and feature branches

Jenkins merge before build
Configure your Jenkins merge before build with an octopus merge strategy.

Are your developers supposed to merge their branches into the main development trunk line every night? Do the developers always leave the office before doing so? Using the Merge before build feature, Jenkins can be configured to locally merge any number of development branches and then perform the build on the consolidated code base. If merges occur without conflicts, your developers won't need to perform daily merges anymore.

4. The Jenkins Git push

Jenkins Git tipsJenkins Git Plugin has many
helpful tricks.

The Jenkins Git push is one of my favorite hidden features of the plug-in.

When Jenkins jobs are configured to merge branches together before running a build, the commit is local, and there won't automatically be a push of the commit back to the repo from which Git pulls. But you can configure the Jenkins plug-in to perform a Git push back to the remote repo, which then incorporates the merge's commit. The Jenkins Git push feature ensures that the commit becomes part of the distributed version control system's global history.

Jenkins Git push
Perform a Jenkins Git push as a post-build action.

5. Issue a Git sparse checkout

sparse git checkout
Perform a sparse checkout with Jenkins Git Plugin.

Repositories can get huge, and pulling down every file and every delta can consume both network resources and disk space. But since version 1.7 of Git, it has been possible to perform a sparse checkout, where only certain folders and subfolders are pulled back from the source code repository. By asking the Jenkins Git Plugin to perform a sparse checkout, your builds will be more focused, run faster and consume fewer resources than a full Git fetch or clone.

Once installed, Jenkins Git Plugin enables your build jobs to perform plenty of other interesting functions, but these are five of the most useful. Integrate these features into your continuous integration builds, and get the most out of your Jenkins CI tool.

Learn Git fast

Interested in learning more about Git? Check out these Git tutorials and resources:

View All Videos
App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close