Jenkins and GIT

September 26, 2012

Jenkins is a continuos integration software designed to run tests and compile projects and providing lots of meaningful metrics over test and compilation process such as test coverage, coding style and such things. Among other features it’s possible to integrate it with several VCS, to ensure you can test every new piece of code.

Few weeks ago, I migrated the repository of the software project I’m most involved from Subversion to git. To make a long story short, the reasons were mainly the branches support. Now I need to make Jenkins use the git repository instead of Subversion one. This is pretty simple.

In my installation, Jenkins runs under its own user, which is jenkins. So first of all you need to become that user and create a new ssh key for it.

 esauro@legolas:~$ sudo su
[sudo] password for esauro:
root@legolas:/home/esauro# su - jenkins
$ $ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/var/www/.ssh/id_rsa): ~jenkins/.ssh/git
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in ~jenkins/.ssh/git.
Your public key has been saved in ~jenkins/.ssh/

They passphrase should be empty. The next step is to give read access for the repository to that ssh key. That’s beyond the scope of this post, as it depends on the git server that you are using.

Add and ssh config file which use that key for the git server. Edit the file ~jenkins/.ssh/config and add the next

Host <put_your_git_host_here>
IdentityFile    ~jenkins/.ssh/git

You need to add the git host to the known hosts file. The simplest way to do this is just connecting throw ssh using the jenkins user. That way we also test the ssh key is working, and the ssh key is in the authorized keys on the server side.

We are almost done, just config a and a

 jenkins@ubuntu:/tmp$ git config --global ""
 jenkins@ubuntu:/tmp$ git config --global "Jenkins CI"

The last step is to setup that repository in your jenkin task.