Auto merge Git branches upon Build

If you have a scenario where different people are working on different branches, and they depend on each branch as a series, each build from one branch will needed to be pushed to the next branch like a pipeline. Say developers do changes to a dev branch and if the builds are OK, you merge the dev changes with the master branch.

You can achieve this with a simple Jenkins build

Lets assume you have 2 branches Master and Test


Create a Jenkins job with a name of your preference.

In Source code management, state the git repository you are using.

Then point out the branch name that needs to build(assume test branch)


Jenkins will get the “test” branch and build it. Next what we need to do is run some test to verify that the build is successful. You can add a script to run some UAT tests on the build created upon the test branch in the Git repo.

Go to Build process -> Add build step -> Execute shell


Then upon success, we need to have the test branch merged automatically with the master branch which we usually consider as the trunk of the repository.

We can use the Git Plugin for this. 

Go to Post build Actions -> Git publisher

Then do the configurations as follows to merge the above branch (test) automatically upon a successful build.

Branch to push = the branch that test branch is needed to be merged with(master in out case)
Target remote   = the remote repository name (git origin in our case)

This job can be triggered automatically with a simple git polling in Build Triggers section. (See my previous Jenkins job configuration posts) . This will then automatically build the test branch and then if the build is successful and the tests pass, will merge the branch with the master branch. Else the auto merging will not happen.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s