Configuring Jenkins continuous integration server to work with Git

Having tuned our Agile process to release iteratively and often I decided it was time to spend some time looking at how we could introduce continuous integration into our PHP and Javascript BDD workflows. Given that Travis isn’t suitable for most of our work (your code must be open source), I chose Jenkins as our CI server and was able to get up and running fairly quickly. I  found a few resources that covered integrating Git with Jenkins but I ended up doing a bit of digging myself so thought I’d quickly share the steps I followed.

1. Install Jenkins and Git

I provisioned a fresh Ubuntu 12.04 box on AWS, which meant I was able to follow the official Jenkins docs without any problems. If you’re using anything other than Ubuntu/Debian or Redhat you may need to look elsewhere. The installation instructions outline simple Apache or Nginx vhost configurations that can be used to serve the Jenkins administration console. You’ll need to install git on the same box so that Jenkins can eventually pull down, or commit to, your git repositories. To install Git I simply ran sudo apt-get install git, other distributions may differ.

2. Install the git plugin

Once you can access your Jenkins console,  goto `Manage Jenkins -> Manage Plugins` from the home screen.

Open the ‘Available’ tab and find the plugin entitled Git Plugin.There is a filter box but it didn’t work particularly well for me, I ended up using Find in Chrome.

3. Create ssh keypair

Part of the installation process will create the user `jenkins` which will envoke all Jenkins processes, including all git commands. Therefore you’ll need to provision a keypair for this user then add the public key to your git repo. There’s a bit of trick to doing the former which I’ll cover now:

    1. Login to your box and switch to the Jenkins user. The installation process doesn’t create a password so you’ll need to have root/sudo permissions to do this. Run the command sudo su - jenkins. The ‘-’ specifies a login shell, and will switch you to jenkins’ home directory (for me this was /var/lib/jenkins’).
    2. Create a .ssh directory in the jenkins home directory.
    3. Create the public private key pair. There are many tutorials which cover using the ssh-keygen command to do this. The most important thing is not to set a password, otherwise the jenkins user will not be able to connect to the git repo in an automated way.
    4. Add the public key to your Git repo. We use bitbucket so this was fairly straightforward for me and I imagine anyone reading this will have performed similar actions for all their devs keys in the past.
    5. Set a git user and email address. This is also mentioned in the git plugin documentation. Run:
      cd /srv/jenkins/jobs/project/workspace
      git config user.email "some@email.com"
      git config user.name "jenkins"

 

  1. Connect to the Git repo. This is a one time step which will dismiss that ‘Are you sure you want to connect’ ssh message, again jenkins won’t be able to deal with this. Just run ssh git@your_git_server_url info

4. Create a new job in Jenkins

The official docs provide a good level of detail on how to configure a basic Jenkins job, so I’d recommend following them here. The git-plugin docs also provide some useful info, so there’s not too much more for me to say other than list the steps I followed:

  1. Select Git in Source Code Management
  2. Enter your git repository URL. This performs a asynchronous check and will give you an error if it can’t connect. Double check you followed the steps in #3 if you get an error.
  3. Select the branch to build. This is branch jenkins will pull from when a build is started so enter whatever is suitable. We are building from develop so I entered this here.

5. Run a test build

Once you’ve saved the job it’s worth kicking off a build to check everything’s working OK. You can do this in the main dashboard. Have a look at the latest build, you should see the commit id that was pulled down. If you dig around in the docs you’ll find that the plugin will have fetched your branch (in my case develop) and pulled from the remote repo. The repo is checked out into the job’s `workspace` directory and during the build the Jenkins user is cd’d into this directory. By adding your makefiles and build scripts into your repository it’s then a straightforward case of configuring the Jenkins job to execute these upon build. Again, this is covered in good detail in the docs.

6. Post receive hook

Finally you’ll need to setup your Git repository to initialise a build each time you push to your repository. Github and bitbucket both have hooks ready to be used, but if you’re self hosted a good place to start is at the plugin documentation which specifies the HTTP endpoint which can be used to trigger the job.

I hope you’ve found this useful!

This post was tagged with: , , , , , , .

  • Ryuzaki

    Very helpful article.

    Can you also provide the link to how to configure post-commit hooks

  • graham

    ta

  • hpavc
  • jasonm23github

    “Be careful not to set a password on the ssh key” … use ssh-agent to manage passkey’s, don’t risk the chance of a unsecured key being compromised.

  • Piyush Shah

    Great artivcle. Have you worked with git submodules ? Seems to be complex . Would love an update if you have figured that out.

  • don fong

    thanks for the tips. FYI the “code” presented in section 3 step 5 is all smushed together as if it were a single line. the presentation would be clearer if this were corrected.

    cd /srv/jenkins/jobs/project/workspacegit config user.email “some@email.com”git config user.name “jenkins”

    what i think would be very useful is a complete concrete example of how the jenkins git project should be configured, ideally with snapshots. it seems there are a lot of ways to go wrong, and i am finding the configuration process to be frustrating.

  • Shane P.

    Amazing write up – saved so much time due to the “Connect to git repo” step.

  • Walid A. Shaari

    how would you automate processes if you do set a password? i would like to hear from you in a possible secure way to how to secure the key with password but still able to automate processes

  • Walid A. Shaari

    thank you for sharing your experience. I am looking for Jenkins+GIT+Configuration Mgmt tutorial, book, seminar. have you come across any? what is the best way to get up to speed with jenkins+git for operations not development really?

  • John Behrens

    Thanks for the tutorial
    “The most important thing is not to set a password”
    is not absolutely right.
    There is the possibilty to run SSH-Agent-Plugin and use passphrase keys.
    That might be useful if a policy requires them

  • Giel Berkers

    Good article. I’m only wondering:

    You mention that I “need to setup your Git repository to initialise a build each time you push to your repository”. But what are the advantages and/or disadvantages over polling the repository from within Jenkins (with Poll SCM)?

  • Chris Sissingh

    Hi Geil,

    The benefit of the post hook is that when your jenkins server receives this message it will immediately trigger polling for that repository. This reduces the wait time for the job to build. For more information on this, see https://wiki.jenkins-ci.org/display/JENKINS/Git+Plugin, under the heading “Push notification from repository”

    Chris