TeamCity and a private GitHub repo

October 27, 2014 — 3 Comments

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.

add-deploy-key

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.

upload-ssh-key

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.

teamcity-authentication-settings

We’re done! To try it out, scroll to the bottom and click on “Test connection”. Hopefully, you’ll see this:

teamcity-test-connection

Success!

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?

3 responses to TeamCity and a private GitHub repo

  1. 

    Nice post!

    There was one thing that bit me, so I’m leaving this here for the next guy that finds this resource to save them some time:

    This approach works well *if the build server is responsible for checking out the sources*, in other words if you have only a single machine with everything on, or if you specifically tell TC that the checkout should be done by the server.

    This is not configured on the vcs root itself, but on each build project; in the project settings, there’s a menu item for “Version Control Settings” (on the same menu level as “Build Steps”), under which “VCS checkout mode” *must* be set to “Automatically on server”.

  2. 
    refactorsaurusrex January 14, 2016 at 00:09

    Thanks for this, very helpful. One note to future travelers: In TeamCity, the fetch url for your repository cannot be in https format; you must use ssh format: `git@github.com:AccountName/RepoName.git`. See here for more info: http://stackoverflow.com/a/26628949/300212

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s