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.

Requirements

Preparations

Empty git repository

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

d1a0_001

 

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:

 

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.

b1b7_001

Credentials

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).

7ec2_001

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

cca2_001

 

Creating job triggered by git change

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

7af6_001

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.

12b3_001

 

Testing

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.

c120_001

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

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

23fe_001

Non master branch test

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

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

Make some change and push push:

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.

1edb_001

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.

e98c_001

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

8fcb_001

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

d61b_001

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.

48e4_001

Testing

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
Facebooktwittergoogle_plusredditlinkedinmail

Leave a Reply