Use Visual Studio Team Services As A Continuous Integration Platform for GitHub

Editor’s note: The following post was written by Visual Studio and Development Technologies MVP Davide Benvegnù  as part of our Technical Tuesday series with support from his technical editor, Windows Development MVP Marco Minerva.   GitHub is the de-facto standard platform for hosting Open Source projects. It has a lot of nice features, but it doesn't have an integrated build server.

We can overcome this shortfall by using Visual Studio Team Services (VSTS) as a continuous integration platform for Github. This article will outline how to achieve this using the following steps:

  1. Link the GitHub account to VSTS
  2. Create a Build Definition
  3. Configure the GitHub repository
  4. Configure the Triggers
  5. Execute the Build

To get started, you need to link where your open source project resides in GitHub, to VSTS. This is important because VSTS needs to be able to download the project files from your selected repository, and build the solution.

To link the GitHub account to VSTS,  go to the Settings Page of the Team Project you want to use (you can create an ad-hoc team project, as I have done in this example, or use an existing one) and select the Services tab.

On this page, click New Service Endpoint and select GitHub from the list of available services.

At this point a pop-up window will open. You can choose between two different ways to link your GitHub Account: "Grant authorization" and "Personal access token".

The "Grant authorization" method is the easier way to authorize your account. This approach grants your Team Services account access to GitHub via OAuth - this is the method we’ll use in this article.

If you want to use a PAT (Personal Access Token) to limit the surface exposed to VSTS instead, you have to create it in GitHub first, and then copy and paste it to the VSTS window. If you want to use this approach, be sure that the token you create has the repo, user, and admin:repo_hook scopes selected.

For this article, however, we are using the OAuth approach. So, click on the Authorize button. 01-connect-github

The GitHub Authorization pop-up window will appear (be sure that your browser doesn't block pop-ups). You’ll have to login to GitHub using your account credentials, and review the authorizations you’re giving to VSTS.

After accepting the authorization, the message "Authorized as GitHub user 'XXXX'" will appear on the VSTS service page - XXXX is the username related to the account you’ve used. Give a name to the connected service and then click OK to complete the operation.

When done, your GitHub account will be listed in the connected services page. Now VSTS is aware of our GitHub account and is authorized to access it.

Create a Build Definition

It’s now time to create the Build Definition. VSTS has a bunch of ready-made Build Definition templates available to create a build, and one can build almost any kind of software project - from .Net, Java, iOS, Android, Node.js, and so on.

In this example, we’ll set up a Build Definition for a .Net solution.

To do so, navigate to the Build & Release tab in the menu and we go to the Builds section. After this, click on the  New definition button and select the proper build template, depending on the kind of software solution you want to compile (You can also start with an empty Build Definition and add the different tasks you need, step by step).

As already mentioned, we are building a .Net project with no particular needs, so we can select the Visual Studio build template. 02-build-initial-setup

After clicking the Next button, you’ll be prompted to select the repository type. By default, VSTS proposes to use the internal TFVC or Git repository, but it is also possible to select an external one like GitHub, SVN or any other external Git providers.

In this case, you want to grab your source files from GitHub, so we click on the GitHub tile. 03-repository-selection

If you want, you may set up a continuous integration build directly on this page (by selecting the checkbox), but we’ll check out another way to do it later.

Once you click on the Create  button, VSTS will create a new Build Definition with a list of tasks, which depend on the build template we’ve selected.

Configure the GitHub Repository 

After the build definition is created, you must configure the correct repository for the GitHub account you previously linked. To do so, navigate to the Repository tab. Here you’ll see the connected GitHub account and can choose the repo to build from. 04-repository-setup

After doing this and saving the changes, go back to the Build Tasks page. Select the files to build the repo directly in the build task.

In this example, we have more sln files in the repository, so we want to select the right one to build. Just click on the button near the textbox to navigate the content of the repo. 05-sln-in-repository

Since this is a normal build definition for VSTS, you may configure all the settings in the Build Tasks as usual.

Configure the triggers

Since you want to set up a Continuous Integration service for GitHub, navigate to the Triggers section. Here you’ll be able to activate the CI (if we haven’t done it on the repository selection page) and to configure it. 06-triggers

You can also decide which branches will be monitored for Continuous Integration. By default, only the Master branch is monitored.

Execute the build

When everything is configured, it is time to queue a build using the Queue new build button.

After few seconds, a build agent will take charge and start it. 07-get-sources-from-github

In the real-time log console, you may see that VSTS is actually connecting to the GitHub account and getting the source files from the repository we configured before. When all the files are synced, the actual compilation process will start.

So now you can see just how powerful Build service can be, allowing you to create a Continuous Integration process that connects to GitHub, get the source files from there, and compile them on the fly.

mvp-profile-image-bigDavide Benvegnù is the Founder, Director and CTO of DBTek Limited, a Software House and IT consultancy company based in Hong Kong. He is also a Visual Studio and Development Technologies MVP, with more than 14 years of experience in software development, IT and Application Lifecycle Management.

Davide is a trainer, a speaker at international technical conferences and an active technical communities contributor.

Follow him on Twitter @DavideBenvegnu