Hooking Up Hudson to Your Bitbucket Mercurial Account

| Comments

This past weekend, I saw @wakaleo running into some issues getting bitbucket hooked up to hudson.

I responded with a long reply that I thought would be worth cleaning up and posting here.

Mercurial, Bitbucket and Hudson

I’ve been using Mercurial as my personal revision control system for a while now.

I’ve more recently been using it at work with a repository hosted at BitBucket, a great service that offers free accounts (including one private repo) and very reasonable upgraded accounts. (I don’t have any relationship with bitbucket other than just as a happy and satisfied user.)

Being a good agile shop, we use hudson as our continuous integration server (much better than Cruise Control IMO). It polls the source control repository on a periodic basis, if it sees any code changes, it checks them out and runs all of the tests.

Quick Mercurial/Hudson Setup

Install Mercurial

There are lots of ways to install mercurial. On the Ubuntu box, the quickest way to get a recent version for me was to just use python’s easy_install:

sudo easy_install mercurial
Install Hudson

There are better guides out there for getting Hudson going depending on the platform you’re using, but here’s the quick instructions for how I got it going on an Ubuntu 9.10 image running on Amazon EC2.

Using Ubuntu’s apt-get package management, installing hudson is easy. You need to add the repository that has the hudson source so that apt-get knows where to get it. Add this to your /etc/apt/sources.list file:

deb http://hudson-ci.org/debian binary/ 

Then update apt-get and install hudson:

sudo apt-get update
sudo apt-get install hudson 

That creates a hudson install with a new “hudson” user on your system.

Setting up Hudson to SSH

We want this user to be able to connect to BitBucket over SSH and check out the repository. To do this, we’ll need an ssh public and private key that hudson can use. Sudo to the hudson user and generate one:

sudo su - hudson

# cd to the home dir, default in /var/lib/hudson
cd ~

# create the ssh key in the ~/.ssh directory
ssh-keygen -t rsa

# that creates the public/private key in the /var/lib/hudson.ssh directory
cat ~/.ssh/id_rsa.pub

Copy that key to your clipboard (make sure to get all of it, it’s all one big line that probably has wrapped in your terminal window a couple of times). We’ll be using this as our identification on bitbucket.

Give the Public Key to BitBucket

If you’ve already got a BitBucket account that you want to give Hudson access to go to it now. Otherwise, create a new account for Hudson to use. Go to the “account” page and paste the public key in the “SSH Keys” field and hit the “add key” button.

If this is the account that owns the repository (or already has access to it), you’re done on BitBucket. Otherwise, if it’s a private repository owned by another user, you’ll need to add the hudson bitbucket user as a “read-only” user to the repository on the repository’s “admin” screen (or ask the repo owner to do that).

Then, go to the repository and get the ssh clone string for the repo you want to clone. By default, bitbucket shows you the HTTPS clone string, which will NOT work with this authentication method. To see it, click on the “SSH” link and it should show you something like:

hg clone ssh://hg@bitbucket.org/tednaleid/grails-test-data/

If the protocol in the url is “ssh”, you’ve got the right one.

Finish Hudson Setup

Back on the hudson box, you’ll want to manually clone the repo as the hudson user so that bitbucket is in the ~/.ssh/known_hosts file. Just execute the ssh clone command we found above:


hg clone ssh://hg@bitbucket.org/tednaleid/grails-test-data/

ssh will prompt you to add the host to the list of known hosts and then will clone the repo. If that works, authentication is set up right and we’re almost done.

Install the Mercurial plugin in Hudson

All that’s left to do now is install the Mercurial plugin in hudson. In a browser, go to http://INSERT_YOUR_IP_HERE:8080. Hudson should come up.

Click on “Manage Hudson” and go to “Manage Plugins”. Go to the “Available” tab, check “Hudson Mercurial plugin” and hit the “Install” button. Hudson will prompt you to restart, and then it’s installed.

After that, just create a new job and you’ll have a new “mercurial” option in the “source control management” section. Select that and put the ssh URL in the “Repository URL” field. Then put “default” in the “branch” field and set up the rest of the job to build/test your code (an exercise left to the reader).