I've been trying to setup a Jenkins (opens in a new tab) build of a private GitHub (opens in a new tab) project recently and found it wasn't as straight forward as I thought. GitHub provides a feature called Deploy Keys (opens in a new tab), which are SSH keys used to authenticate servers to your GitHub repository. Here's how I set them up.
- On your Jenkins server, generate a new SSH key if you don't already have one. (Make sure you are logged in as the user that Jenkins runs as): ssh-keygen -t rsa
- Log into your GitHub project and click the project's admin button.
- Click the Deploy Keys link and add a new key.
- Copy the contents of the public key you just generated (id_rsa.pub) into the GitHub Deploy Keys field.
That's it! Now, using the Jenkins Git plugin (opens in a new tab), you can point your Jenkins Build Job at your GitHub repository and it should authenticate just fine.
This will only work for one repository. GitHub requires that your Deploy Keys be unique among your repositories. Ideally, you should be able to create multiple SSH keys on your Jenkins server, but I had trouble getting this to work. What I ended up doing instead was adding the SSH public key (opens in a new tab) to my GitHub user account (opens in a new tab). This allows Jenkins build access to any of the private repositories I create.
I would ideally like to get the Jenkins GitHub plugin (opens in a new tab) working, but after a 10 minute test, I stuck with my current setup so that I can move on to the next problem.