CI With VSTS And Xamarin

January 22, 2016 | 5 Minute Read

Works on my machine is dead, long live Continuous Integration with mobile development.
Continuous Integration also known as CI is one topic on the developers shelf of best practices. It's a well integrated part of many development teams today. It helps ensuring code also
compiles outside the developers own machine also known as works on my machine. In this blog post I'll explain how to get up and running with CI using Visual Studio Team Services - VSTS on a Xamarin project.

The mission

The goal is to have a CI running and each time a deloper makes a push to the GIT repository, we want to trigger a new build also know as Continuous Integration. The app will be a vanilla Xamarin Forms project.
Further more we’ll see how we can reduce the cost of using the build service in VSTS by making use of an on premise build agent.

Create VSTS Project

The first thing we need, is to create a VSTS project. Log on to VSTS and create a new project.
Here we select GIT as version control, which creates a GIT repository also part of VSTS.

Prepare repository credentials for OSX

One minor detail is your repository credentials. If your primary VSTS user name contains an @ you need to make an username alias before you can clone the repository on osx. Either go to your profile and the security tab or click the Generate GIT credentials
Here you can specify the alias and a password. Recommended by VSTS you can also generate and use a personal access token.
With the new repository setup we’re ready to clone it and add a some code. As mentioned I’ve create a vanilla Xamarin Forms project and made the initial push to the repository. VSTS lets you manage your GIT repositories inside VSTS, just as Bitbucket or GitHub etc.

Build agent setup and reduce build cost!

As we’re are building a project for IOS we need as required by Apple, to build the source code on Apple hardware. Another concern is the cost of having the build performed inside Azure using a hosted build agent, meaning we’ll use Azure clock cycles hence spending more $$ on building our source code.
Currently Azure has no support for building on hosted MAC OS and while we could look for another hosted solution, let’s save the money and make the build on our on premise hardware. This just needs to be some MAC/OSX powered device with the required software installed like Xamarin Studio etc.
If you are using your own developer machine, please notice the build agent will create its own workspace, and not influence your development GIT repository

Install build agent

Just as building the source on azure, we also need a build agent when we want to have the build on premise. We’ll need to install a build agent on the machine and specify some credentials, making it able to communicate with the build setup in VSTS.
Navigate to the root of your default collection and select the Agent queues tab. Select Download agent and navigate to the xplat location.
The recipe is well written so just follow it. Afterwards start the agent. I recommend starting out by running the agent in a terminal to quickly see any error messages. When everything is up and running, just switch over to have the build agent run as a service.
#### Grant bulid agent permissions
When I first started the build agent you might get and error message saying something about the agent not being able to listen….permission required….
This is because you need to ensure the crendentials you specify also is granted the permissions to act as a build agent in VSTS. Return to the VSTS website and the Agent queues tab. Here you should now be able to see the build agent running on your on premise machine.
Select the Roles tab and add the account your build agent is using. Add it to both the Agent Pool Administrators and Agent Pool Service Accounts group.
Restart the build agent and all error messages should now be gone.

Make CI build definition

The final step to enable CI is to make a build definition in VSTS using our build agent.

From the BUILD menu item on your VSTS project, add a new Build definition

Select the Xamarion.IOS template. Afterwards you can just add additional build for other platforms.

On the final page we select GIT as version control, the project we want to build and the branch. This part is really good as it makes it very simple to make different build definitions to feature or release branches.
Ensure you select the non hosted build agent. I installed my agent as the default. If you need to make any changes, just click Manage.
The most important part of this page is to check the Continuous Integration checkbox. Save the template and return to code__</>__

Verify CI is running.

In Xamarin Studio we make a change the source code, add, commit and push… wait a bit and then watch the terminal running your build agent. You should soon see

Running job:build

Job completed:build

Job Finished:build

Continuous integration is now running with Visual Studio Team Services and Xamarin!