This tutorial will give you all the necessary steps to use Jenkins as a continuos integration tool with Xamarin (Android and iOS). If you already have a machine with all the tools installed (Xamarin Studio, XCode, etc) this guide should take you about 30 minutes at most to setup Jenkins running a simple job.

Why this guide

As you probably already know, Using Xamarin with Jenkins it’s not up to date. It will guide you how to setup Jenkins 1.6, instead of the latest one 2.8 that contains quite a few changes. We will also be covering the whole process of fetching the code from a Git repository and starting with simple things like restoring the NuGet packages.

Let’s start with Xamarin CI and Jenkins

This guide contains the following steps:

  1. Setup Jenkins
  2. Prepare Mac Mini as a Jenkins slave
    1. Add Nodes
  3. Prepare to build Xamarin projects
  4. Create jobs
    1. Restore NuGet packages and components
    2. Build iOS and Android

Setup Jenkins

Follow this guide to install Jenkins. At the time of this post the current version is 2.8. Some plugins are useful to install so here’s a list:

  • EnvInject to setup a environment variables a part from your jobs
  • MSBuild plugin to build solution files
  • Multijob plugin to be able to run jobs in parallel (example: build iOS and Android at the same time)
  • SSH Slaves plugin to enable managing slaves over SSH
  • Xamarin Studio Tool Runner plugin to build Xamarin projects (this is not mandatory, the command to build a Xamarin Studio solution is really simple and we probably don’t need a plugin for that, specially one that it’s on version 0.1.1 since 2014)

Prepare Mac mini as a Jenkins slave

Ignore this step if you wish to run Jenkins and it’s jobs on the same machine.

Let’s assume Jenkins is installed and setup correctly.  Steps:

  • Create a jenkins user on your mac. Bear in mind that his user should have certificates, provisioning profiles and all that stuff to build Xamarin.
  • Allow ssh for that user, refer to this link if you’re not sure how to do this.

Add Nodes

  1. Open Jenkins and go to Manage Jenkins => Manage Nodes => New Node
  2. Select “Permanent Agent” and give a name to this node, something like “mac_mini_slave”

Here you can see the configuration for a slave. It’s important to change the following properties:

  • “Remote root directory” – add the directory of your user

Jenkins Remote root directory

  • “Usage” – Depending on what else you’re using Jenkins for, I would recommend “Only build jobs with label expressions matching this node”. We’ll add this slave to a job later

Jenkins Slave Usage

  • Launch method – choose SSH, add the host (ip address or domain name) and the credentials for the user previously created. SSH key can also be configured, let’s keep things simple for now.

Jenkins Launch Method

  • Save and wait until Jenkins connects to the slave.

Prepare to build Xamarin projects

This step covers all the necessary tools to run Xamarin with Jenkins on a OSX machine. If you’re using a mac mini as a slave you probably should login on that machine manually to install these tools:

  • Xamarin Component command line tools, if you use Xamarin Components on your project. Download it here
  • NuGet.exe to restore nuget packages. Refer to this github repo
  • NUnit, if you want to run unit tests. Refer to this link. You should install 2.6.4, which is the current version supported by Xamarin.

These 3 tools above can be placed on the same directory. You can add these to the path on your machine or just use it like “/Users/jenkins-user/tools/xamarin-component.exe”.

  • XCode (and command line tools)
  • iOS developer profile (you can import your current profile from another machine, refer to this link)
  • Xamarin Studio
  • Android SDK
  • iOS and Android Simulators (if needed)

Once this is all finished you should be able to build Xamarin projects on this mac mini slave.

Create jobs

On this section you can find how to restore nuget packages, install components, build iOS and Android projects, install them on a simulator, run tests, etc. To make thing simple let’s create just one job that does all of this. In the future you might want to consider a Pipeline or a MultiJob, to run jobs in parallel.

Assumptions

For the rest of the tutorial let’s assume all commands are environment variables. I’m running Jenkins with multiple slaves so the path for each tool might be different. This means that when you see something like

 ${mono} 

it means

 /usr/local/bin/mono

Restore NuGet packages and Xamarin Components

This is a very simple step and it only requires two commands to restore the NuGet packages and install components. Without this step you will not be able to build your app.

To restore NuGet packages run this command:

 ${mono} ${nuget} restore ${WORKSPACE} -NonInteractive 

To install Components run this command:

 ${mono} --runtime=v4.0 ${xamarincomponent} restore ${APP_SOLUTION}/App.sln 

Build iOS and Android

To build iOS and Android I would advise using the mdtool that should be installed already as it is part of Xamarin Studio. It’s path should be

/Applications/Xamarin\ Studio.app/Contents/MacOS/mdtool

The SOLUTION_FILE variable is not mandatory but it’s cool if you just want to copy and paste into all your projects as this better than hardcoding the solution filename. Here’s the command:

 SOLUTION_FILE=`find ${WORKSPACE} -maxdepth 1 -type f -name '*.sln' -exec basename {} \;`
 "${mdtool}" build --target:Build "--configuration:${Configuration}" --project:iOS $SOLUTION_FILE 

To build Android just replace “iOS” for “Droid”

Stay tuned for a next post on how to run unit tests and UI tests in Xamarin projects with Jenkins!