Sunday, October 23, 2016

Trigger Jenkins Build Using SVN PostCommit hook



This post explains about triggering jenkins build as soon as you commit files/changes to svn repository.

There are two ways to achieve this.

1. Poll repository to find if any changes done in your project location. But with this approach there is a chance of build skips since polling is done in a particular interval. This is ideally not preferable as it increases load on server, building jobs even when there are no changes detected.

2. Configure SVN repository to trigger post-commit hook which can trigger jenkins build.

This post covers 2nd way in detail.

Location of hooks folder: SVN hooks can be found right in the place where repository is created which contains directory name hooks. You can find all the type of hooks which are supported by svn.

By default all the extensions of hooks are .tmpl, in order to make them executable the extension should be changed to .bat/.exe in a windows environment.

3. Post Commit hook by default receives two arguments. They are received in an order REPOS and REV

REPOS: is the location when svn repository is created. EX: "file:///E:/WORK/svn" (on windows)

REV : is the revision value generated for every commit. EX: 18928

4. It is necessary to get an idea about utility svnlook which is a command-line utility for examining different aspects of a Subversion repository. It does not make any changes to the repository—it's just used for “peeking.” svnlook is typically used by the repository hooks explore much about "svnlook --help"

3. Triggering jenkins build on post commit will involve a series of steps i.e

a) Initially a property has to be set on the project location as a keyvalue pair. Make sure you commit property name and value changes to svn ex: name:ci:buildurl value:jenkins-job-build-url



b) Finding out which directories were updated on commit.

"svnlook" with option "dirs-changed" has the ability to find out directory changes on postcommit using revision number EX: >svnlook dirs-changed E:\WORK\svn -r 392 ( 392 is revision number, command execution would result below)
>testproj/trunk/src/main/java/testproj/

c) Extraction svn-properties on the directory that resulted in change.

"svnlook" with option "propget" and valid arguments can result in property name that was set to project.

EX:> "svnlook propget E:\WORK\svn ci:buildurl testproj/trunk/src/main/java/testproj/"

> http://localhost:8080/job/testproject/build?delay=0sec

("testproj/trunk/src/main/java/testproj/" is the folder that was updated during a commit in step b and ci:buildurl is the property name, E:\WORK\svn is the repository location)

4. Using the value obtained from step C execute curl command to run jenkins build

EX: curl -X POST http://localhost:8080/job/testproject/build?delay=0sec

5. After all the subsequent steps you can find jenkins building the job.

6. This is a sample post-commit hook attached below.

   
setlocal  
set REPOS=%1  
set REV=%2

FOR /F %%i in ('svnlook dirs-changed E:\WORK\svn -r %REV%') do SET dirloc=%%i 
 
FOR /F %%i in ('svnlook propget E:\WORK\svn ci:buildurl %dirloc%') do SET PROP=%%i

if defined %PROP%
curl -X POST %PROP%