Configuring Jenkins Git Plug-in with BitBucket private repositories in Windows

Tell me about writing a specific title. This is my first entry here in TupiLabs. Today I will show you how to configure Jenkins Git Plug-in, to retrieve sources from a private repository hosted at BitBucket. Phew. Ah, and all this using a Windows server.

TL;DR: ssh -vT <host> displays where it is looking for your .ssh directory. Probably you will want check permissions and the %HOME% environment variable too. In case your job is getting stuck, click here to skip the introduction text.

Motivation

  • At TupiLabs, we have repositories hosted at GitHub (public repositories) and BitBucket (mix of public and private repositories).
  • We use Jenkins for many activities. Not only as a build server, but as a function in our development environment. We will talk about that later ;-)
  • We use Jenkins, Mac and Windows for development.
  • We have jobs running at Linode with Ubuntu+Jenkins+Git Plug-in+BitBucket.
  • There aren’t many resources for configuring Jenkins and BitBucket in Windows.
  • I have a good friend of mine who would like to read about it. Heck! That’s enough to write about it :-D

Requirements

It’s really simple to set up your environment for this integration. All you’ll need is:

  • Jenkins
  • Jenkins Git Plug-in
  • An account in BitBucket with a private repository
  • msysgit (a.k.a. Git for Windows)
  • Windows XP or later

I assume you are used with Jenkins, and that you are able to create a private repository in BitBucket and push projects from the command line using git and SSH keys.

Righto so, hammer-time!

Log in to BitBucket and, in case you don’t any private repositories yet, create one. The process to create a private repository is the same to create a public one, except that you need to remember to select the Private check box.

Now you will need to configure Git for Windows. You can find the instructions for doing so at this GitHub help page (strange use a GitHub help page, to configure a BitBucket repository in Windows, no? :-) Few points to take note here. Install git in a directory with no spaces in its name and, if possible, check the option to include all Git tools to your Windows Path (that can make things easier later). The instructions in GitHub are equivalent, to what you have to do in BitBucket, specially include your keys to your profile in BitBucket.

At this point, you should have a private repository in BitBucket and your Windows machine configured to access this repository. Git delegates authentication to SSH, using SSH keys. Push a sample project to BitBucket. We will use this project later in Jenkins.

The first time you connect to your private repository, you will notice git asks you to include it to the list of known hosts. Write y/yes and git won’t ask you that again anymore. You have to take care, cause in case you haven’t added (either manually, or running git in a prompt) the host to the list of known hosts, your Job in Jenkins may get stuck.

Create a new user, jenkins, and add it to the Administrators group. Start Jenkins, install Jenkins Git Plug-in (the Wiki has a lot of interesting stuff).Configure Jenkins to execute as service and log on with the user you have just created. Now log in to Windows using the jenkins user, and try to access your private repository from command line. You can try a simple git pull.

Still logged in as jenkins, execute ssh -vT git@github.com. I know, I know, here I am again, mixing GitHub and BitBucket :-) Pay attention to what is written to the console screen. Probably you’ll find a line similar to debug1: identity file /c/home/bruno/.ssh/identity type -1. /c/home/bruno/.ssh is the repository where ssh is looking for SSH keys.

This is not good. I know, that I have placed my user’s home directory to c:homebruno (my normal account ID is bruno). When Jenkins is running as jenkins user, it may have trouble using that keys. So what I’m going to do is move these keys somewhere else, like c:optjenkins.ssh, and will update my HOME environment variable.

HOME environment variable? Yup. msysigt, or Git for Windows, uses MingW, which by its turn uses this environment variable to create a virtual file structure similar to the one of *nix systems. Update the HOME environment, open a new console (so that it will load the new value) and execute ssh -vT git@github.com again, and confirm that it’s picking your new value. Time to log off jenkins user, and log on again with your normal user.

Create a new job in Jenkins and configure it to use your private repository in Jenkins. Execute it and, if everything is fine, you will see something similar to the following screen.

Not it’s time to install your plug-ins, prepare your scripts and put Jenkins to rock in and build awesome software! I hope you found this post interesting. Feel free to send comments, suggestions or any kind of feedback :-)

All the best, Bruno