I have a few private repositories at GitHub, and I was looking to set up a continuous integration/build solution for one of them. If it had been a public repository I would probably have looked at Travis CI or AppVeyor, which are free for open source projects. However, they require you to fork out a bit of cash for private repos. I usually don’t mind paying for software, especially not good software, but since I already have a server running at home, I figured that TeamCity could do the job nicely – it is free if you have less than 20 build configurations, it has a lot of great features and I am quite familiar with it after using it at work for the past year.
So the basic need I had was for TeamCity to be able to access and fetch the code for a private GitHub repo. Now, there are a few different ways of setting up access to a private GitHub repo, and having investigated the different available options I settled on using what GitHub calls a deploy key. In short, this is an SSH key that gives full access to the repository.
This guide will assume that you have a repository in GitHub, and that you have a TeamCity project set up.
Generating the SSH key
So let’s start by creating the SSH key that we will use. The simplest way I could think of was to use the PuTTYgen tool. If you don’t have it, you can find it a the PuTTY download page, or – if you are running Windows – by installing PuTTY through Chocolatey.
Once you’ve generated the key, copy all the text from the “Public key for pasting into OpenSSH authorized_keys file” onto the clipboard. This will go into your GitHub project settings. We’ll need the private part of the key later, so save it to disk by clicking on “Save private key”.
Setting up your GitHub project
Next, go to the Settings page for your GitHub repository. In the menu on the left, you’ll find the Deploy Keys entry. Go there and click on the “Add deploy key” button. Enter a title for your deploy key and then paste the contents of your clipboard into the “Key” part of the form.
Click on “Add key”. There, you’ve just added a deploy key to your project in GitHub. Now for TeamCity…
Setting up TeamCity
TeamCity will be using the private part of the SSH key, the one that we saved to disk earlier on. First, go to your project settings page. Go to the SSH Keys menu item, and click on “Upload SSH key”. You can give the key a title, or just select the file and use the filename.
Click “Save” and it will be added to your project. However, there’s one more thing you have to do.
Whether you’ve already set up the VCS root for the TeamCity project or not, what you need to configure in the VCS root is under the Authentication Settings section. For Authentication method, choose “Uploaded Key”. The Username must be set to “git”, and in the Uploaded Key dropdown you simply select the SSH key that you just uploaded.
We’re done! To try it out, scroll to the bottom and click on “Test connection”. Hopefully, you’ll see this:
If not, make sure to double-check all the settings. Also, bear in mind that the SSH key allows full access to the GitHub repository, so make sure that you keep it in a safe place.
TeamCity can now get the source code from GitHub for your project. Now there’s only the matter of writing the software that TeamCity will continuously integrate – and perhaps deploy – for you! But that’s the easy part, right?