03 – Git integration, working with branches

03 – Git integration, working with branches

In this part you learn how to prepare a git repository and a simple Jenkins build triggered by your repository change (push) using SCM schduled polling. In the last part I show you how Jenkins works with git branches.

High-level process overview

Developer performs push to GitLab repository <= Jenkins scans the target repository for chenges every 2 minutes => If change was detected, jenkins builds the project.



Empty git repository

Create new git repository J03-git-integration and perform initial commit.


cd /tmp
git clone http://ubuntu-tutorial:9093/better-coding-tutorials/J03-git-integration.git
cd J03-git-integration
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master


Jenkins base job

For the purposes of this tutorial create a simple jenkins job that execute test.sh file as build step. The job build successflly if file exists and script execute with no error. Otherwise build failed.

To do that, just create freestyle jenkins project JT03-Base, add execute shell script as build step with the following command:

chmod u+x ./test.sh


Jenkins with git integration

Git plugin

Install Git plugin(1-6). Go to Manage Jenkins->Plugin Manager->Available. Find the Git plugin and install it.



Before you start working with git repositories, you need add git repositories credentials to Jenkins credential store.

Go to Credentials->System->Add domain(1,2,3) and create new domain for tutorial credentials(4,5).


Change Username to tutorial, Password to welcome1 and save (1,2,3).



Creating job triggered by git change

Create job JT03-PollTriggered as copy of JT03-Base.


Go to the job configuration(1,2) and enable Git integration (3,4,5,6). After that git  will checkout automatically

Note that you can specify some branch pattern using the Branches to build(6).

Enable poll SCM checkbox to trigger the job on git repository change. Enter appropriate Schedule. In my case job checks on every 2 minutes for SCM change.




Basic test on master branch

Run the JT03-PollTriggered job manually to check its behaviour. The reposirory was checked out(1) but the build  failed (2), because there is no test.sh fife in the repository.


Let’s fix it. Checkout repository and add following test.sh script:

#remove the old repository copy
#rm -rf /tmp/J03-git-integration
cd /tmp
git clone http://ubuntu-tutorial:9093/better-coding-tutorials/J03-git-integration.git
cd J03-git-integration
echo "echo \"Hello world.\"" > test.sh
git add test.sh
git commit -m "add test.sh"

Push changes and wait 2 minutes. The new instance of JT03-PollTriggered should build successfully.

git push origin master


Non master branch test

Create a branch non-master-test and push it to origin.

cd /tmp/J03-git-integration
git checkout -b non-master-test
git push origin non-master-test

After 2 minutes there is no effect, bacause there is no change in the repository  – no new commit.

Make some change and push push:

cd /tmp/J03-git-integration
touch dummy_file.txt
git add dummy_file.txt
git commit -m "add dummy_file.txt"
git push origin non-master-test

After 2 minutes job JT03-PollTriggered builds successfully on branch non-master-test (1) , but there is one build history (2).  This problem I resolved in the next part.

Multi-branch Project support

Multi-Branch Project Plugin

Install Multi-Branch Project Plugin(1-6) to enable Jenkins multi-branch project support.


Creating multi-branch job triggered by git change

Create a job named JT03-MultiBranch-PollTriggered  as Freestyle multi-branch project. This project will be clone foreach branch discovered in repository.


Fill Source Code Management and Sync Branch Triggers sections as below. Both of them are responsible for discover changes in repository.


Scroll down and fill Build Triggers and Build sections as described Jenkins base job.


The repository change docscoverer is executed after the creation of the project by default. As you can see JT03-MultiBranch-PollTriggered was cerated as folder with two cloned jobs: master and non-master-test.



Test below cases by yourself:

  • change master branch: add dummy_file2.txt
  • change non-master-branch: remove dummy_file.txt
  • add new branch non-master-branch2
  • delete branches: non-master-branch and non-master-branch2
0 0 vote
Article Rating
Notify of
1 Comment
newest most voted
Inline Feedbacks
View all comments
2 years ago

[…] previous post I showed you the most common way to integrate any git repository with Jenkins – I mean SCM […]