Skip to content

Continuous integration with Git, Jenkins and Maven

30 March 2013

Git is an excellent version control system, maven and jenkins are excellent ways for continuous integration and deployment, but how can you set them all up, so they will all work happily and content next to each other?

Requirements
To start it all of, you are going to need :

  • a maven project
  • a central git repository,
  • and jenkins.


A central repository is needed since this repository will communicate with Jenkins to start the automated builds on each check in. This may seem a bit contradictory to git, since git is a distributed version control system, but the builds have to be triggered from some place, or repository, so a central place for the git repository is needed. Also it would be nice to have some overview of all the submits to the repository, so there for gitblit will be used. This is a open source tool for managaing, viewing and serving git repositories. Gitblit can be run as standalone application or as a web application. And since Jenkins is also going to be run inside a web servlet container, the gitblit war is going to be used.

Configuring gitblit
Download those wars and install them in your local web server. Then go to the gitblit start page to configure your repository. By default, gitblit will store the repository inside the web application in the data directory. I prefer to not have this inside the web application directory and there for we will have to configure gitblit to use a different directory. This can be done by opening web.xml and changing the parameter for baseFolder. Restart gitblit and you will see your directory being created (note that on linux the user permissions should be set correctly). In the repository directory, a data directory will be created. In here we will find the users.conf which contains the admin user and the password. Also a groovy directory will be created in this directory, containing some groovy scripts. More on those later on.

Now open the gitblit web page and login as admin. You should now be able to create a repository. After you have filled in the name, you should select the hook scripts and choose the post-jenkins script. This will cause jenkins to start a build if new commits are being added to the repository. Note that you can also use the pre checkin script. This will not create a sandbox environment, so if there are failing tests, it will still be committed. If you want more control, you could check out gerrit. This is a code review tool for git, but I am digressing.

And behold, an image to break up all the text.
0002

Gitblit browsing on Tomcat
Start coding, commit your changes, and push them. Nothing will happen, of course, since we haven’t configured jenkins yet, but if you go to the gitblit page, you can see the commits you have pushed into the repository. You can also browse the tree of your sources, but then you might find out that there is a problem displaying some files. This is due to the fact that tomcat will encode the ‘/’ character, which is part of your directory structure, as %2F. Luckily the answer to this problem is in the faq of gitblit. We can fix this by:

  • Tweak Tomcat, add
    -Dorg.apache.tomcat.util.buf.UDecoder.ALLOW_ENCODED_SLASH=true
    to CATALINA_OPTS or to your JVM launch parameters
  • web.mountParameters = false and use non-pretty, parameterized urls
  • web.forwardSlashCharacter = ! which tells Gitblit to use ! instead of /

Configure Jenkins
Now it’s time to configure Jenkins. Go to the jenkins page and select the manage jenkins link and then the manage plugins link. At this page we have to install the git plugin. Download and install the git plugin and restart jenkins. Then we can add a maven 2 or 3 job to jenkins. Create the job with the name of your project and start the configuration.

The first thing jenkins wants you to do is to choose the jdk and maven that should be used to run the builds. After you have configured the global parameters (check all the possibilities here for all kind of parameters like sending mails for failed builds), you can go back to configuring your job.

Configure your job to use the git source code management. This plugin needs the repository where it can pull the commits from, so for our current installation that will be something like : http://localhost:8080/gitblit/git/myProject.git but you can see that more clearly in gitblit, where you can copy the git url. For this go to the gitblit page, select your repository and there you can see the repository url.

Next select the build trigger and only check the poll scm option. This will poll the source code manager periodically, which is not needed in our case, but this will enable the hook. This hook will be used by gitblit after each commit occurs. So you will have to enable this and put some kind of pattern in this. I choose @yearly, since I only want this job to be run after each commit and not really poll the git repositories.

So gitblit is now up and running, jenkins is up and running, and now we only have to inform gitblit what to do after a commit was made. To do this we will have to inform the gitblit webapplication where jenkins is running. Gitblit uses groovy scripts to be run after a commit. Remember that we added the jenkins post commit hook in gitblit? Now we have to use this script to inform gitblit where to push the notification to. This can be done in the gitblit.properties (of the gitblit/data directory) or in the web.xml. The parameter that we should add is ‘groovy.jenkinsServer’ and it should point to http://localhost:8080/jenkins. Now if we push some commits to the repository, jenkins should start automatically.

Done
Yes, it is now up and running. You have a central git repository which will trigger automatic builds by using gitblit that informs jenkins of new commits. Jenkins will then start the normal maven build sequence and run your tests. After this has been set up, you can tweak jenkins some more, but the continuous integration with maven, jenkins and git should now be done.

Advertisements

From → Uncategorized

Leave a Comment

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

%d bloggers like this: