Why Continuous Integration?

In the complex projects, developers and testers (also called as full stack developers in agile methodologies) simultaneously work on their modules. You can imagine several products are getting created in parallel leading to huge amount of integration work in the regular intervals of time. A single change in the development code needs to be checked into the code repository. This delta is then integrated, tested to make sure that the changes didn’t add any defects, hence a stable product, stable build.

This process is called as Continuous Integration (CI). It lets you find defects in the least possible time in the development cycle from the time of their occurrence. It helps to identify code coverage and facilitate the stability of the release build.

Pre-requisites for Xamarin CI using TFS

For this post, we are going to concentrate on using TFS On-Premise Server for Continuous Integration. When using TFS (i.e. configuring Build.vNext), first steps is to log in and configure the agent pools. Agent Pool is called as a collection of several build agents (i.e. services running on the operating system / build server and listens for builds from TFS). This agent pool needs to be created first and then the build agents. It is to be noted that build agents should be free and should also meet the demands of the particular build requested. For example, an iOS build can only be requested to build agent which has Xamarin.iOS capabilities, similarly for Android also. The choice of how many pools or agents you need totally depends on the different projects/purposes.

For the management of the agent pools, you should know below account groups:

  1. Administrator Account Group
  2. Service Account Group

Administrator account group member is allowed to add, remove and configure agents in the pool whereas Service account group only let you run builds.

At first, you need to add your user account in Agent Pool Administrators group by logging into TFS. Later, you can create any number of Agent Pool Service Accounts for the production environment.

CI for Xamarin.Android

This section will address the complete steps required to perform the continuous integration process for Xamarin.Android Native Apps. This will also consist of the process to add the windows build agent to build the source code for a project.

Create and Configure Xamarin.Android Build Agent on Windows

This section will provide you step by step details for creation of Xamarin.Android Build Agent on Windows.

When Build agent receives a build from TFS Server, it executes the steps in the build definition. Currently, you have to use a Windows build agent to build Windows/Windows Phone apps and Xamarin.Android apps and a cross-platform build agent on the Mac to build Xamarin.iOS projects. In the future, the cross-platform build agent should also be able to build Xamarin.Android projects too.

This section will provide you step by step details about different items to be installed on your machine to satisfy it as a Windows Build Agent:

Install below software’s on the build machine.

  1. Xamarin (Latest Version)
  2. Java JDK 1.7
  3. Android SDK (Latest Target Version)

Please note that ANDROID_HOME and JAVA_HOME environment variables should be set to locate the required components and advertise them as capabilities of a build agent. Once done, you need to download the agent from TFS, configure and run the build agent.

For configuration of build agent, you need to create a BuildAgents (or any name with which it makes sense) folder in the C:\ drive to store all of the agents in one machine. You can create AndroidAgent folder in it and extract the agent zip file downloaded from TFS. Once done, open Windows PowerShell as Administrator and run .\ConfigureAgent.ps1, press enter. Fill in the required details for the agent and you are done!

The Windows Build Agent is successfully created with the Xamarin.Android Capabilities. The agent starts as a service in Windows. If you wish to start/stop the agent manually, you can stop the service.

Create Xamarin.Android Build Definition

This section will provide you step by step analysis on the creation of a Build Definition for Xamarin.Android project. It uses Windows Build Agent for creation and deployment of the builds.

  1. Navigate to TFS Team project Home page, click on the Build tab and click Add button to add new build definition
  2. Select Xamarin.Android template and click OK. This will create a new Build Definition with some pre-defined steps

There are different tabs associated with the creation of build definition like Build, Options, Repository, Variables, Triggers, General, Retention and History.

You can modify build steps to build particular Android project present in the TFS Code Repository, sign APK file as a build artifact to TFS. Some more steps are as follows:

  1. For the Target input field, type: PackageForAndroid (this will trigger the build target that will create .APK package once the project is built)
  2. Add Android Signing builds step, check the ‘Sign the APK’ option and enter valid Keystore details as required
  3. Add Publish Build Artifacts step and put ‘Path to Publish’ as $(build.binariesdirectory)\$(BuildConfiguration)

Please note that BuildConfiguration is the variable created under Variables tab which will be used runtime.

  1. Check the Continuous Integration (CI) in Triggers tab or you can schedule the trigger as per your project requirements
  2. Select the Default agent Queue/Pool which is already created
  3. Save the changes and click OK

Once the build definition is created, you can queue new build for the definition and if everything has been configured correctly, your build definition should finish with the message “Build Succeeded”. If failure, you can check logs about the exact cause behind the issue.

CI for Xamarin.iOS

This section will address the complete steps required to perform the continuous integration process for Xamarin.iOS Native Apps. This will also consist of the process to add the windows build agent to build the source code for the project.

Create and Configure Xamarin.iOS Build Agent on Mac OS

This section covers the creation of the cross-platform build agent and configuration for Xamarin.iOS project. The cross-platform agent has been recently introduced along with Visual Studio 2015. This agent can only be configured and executed on Mac OS X and Linux Machines for build definitions created using the Build.vNext tools included in TFS 2015.

Please note that the cross-platform agents are written in Node.js and you can install/update/run them using Node.js.

This section will provide you step by step details about different items to be installed on your machine to satisfy it as a Mac OS X Build Agent:

  1. Install below software’s on the build machine:
  • Xamarin
  • Xcode
  • js
  1. The next step is to install the VSO Agent using the following command in Mac OS X Terminal:

sudo npm install vsoagent-installer –g

If you think that this will create an agent for you, then it is not. This is just going to create a copy which will be used later to create actual agents.

  1. To provide permissions for Node Package Manager, please run below command:

sudo chown –R $USER ~/.npm

  1. It is recommended to create a BuildAgents folder in the root of your Macintosh HD partition to store all of the agents you wish to create (you can have multiple agents running on the same machine). Inside this BuildAgents folder, create another folder called Agent1 (or whatever you want to name your build agent). You can create these folders using the Finder
  2. Install the VSO agent in the folder you created and run ./config.sh file to start configuration
  3. The console will ask following questions to configure the cross-platform build agent on Mac OS X:
    1. Enter alternative username: You need to enter the username of the account that was configured to administer build agents earlier
    2. Enter alternative password: Enter the corresponding password for the account chosen in the previous step
    3. Enter server URL: You need to enter the TFS URL
    4. Enter Authentication Type: Click enter to continue
    5. Enter agent name: You can use whatever name you want to here. This name is to identify agent in the agent pool
    6. Enter agent pool name: This allows you to add an agent to a specific pool which is already available in TFS. If you do not pass this value, the agent will be added to the default pool

The cross-platform build agent is now successfully configured with Xamarin.iOS capabilities. Run ./run.sh to start the agent as a service. Do not close the Terminal window until you want to stop the build agent. You can stop the build agent by pressing Ctrl-C.

Create Xamarin.iOS Build Definition

This section will provide you step by step analysis on the creation of a Build Definition for Xamarin.iOS project. It uses Mac OS X Build Agent for creation and deployment of the builds:

  1. Navigate to TFS Team project Home page, click on the Build tab and click Add button to add new build definition
  2. Select Xamarin.iOS template and click OK. This will create a new Build Definition with some pre-defined steps

Most of the steps mentioned in the build definition for Android are pretty similar.

You can modify build steps to build particular iOS project present in the TFS Code Repository. You need to modify Xamarin.iOS build steps so the build agent will build the solution and upload the .ipa package that is created as a build artifact to TFS. Some more steps are as follows:

  1. Click on ‘Add Build Step…’ and select ‘Publish Build Artifacts’ item
  2. Looking at the new ‘Publish Build Artifacts’ build step, In the ‘Copy Root’ input field type: $(build.binariesdirectory)\$(BuildConfiguration)
  3. Check the Continuous Integration (CI) in Triggers tab or you can schedule the trigger as per your project requirements
  4. Select the Default agent Queue/Pool which is already created
  5. Save the changes and click OK

Once the build definition is created, you can queue new build for the definition and if everything has been configured correctly, your build definition should finish with the message “Build Succeeded”. If failure, you can check logs about the exact cause behind the issue.

This way your Continous Integration System for both Android and iOS apps using Xamarin can be set up. I will cover more details about Xamarin in coming posts.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s