Setting up a CD pipeline for iOS applications with Mobile Center

Having a continuous delivery pipeline for your mobile applications is crucial if you want to allow your team to focus on building features and spend less time trying to package and distribute your applications.
However, setting up and configuring a continuous delivery pipeline for iOS applications has never been a straight forward approach.
Most of the time it involves setting up and configuring a number tools on top of the source code repository, including a CI server, a build server or farm and of course a distribution mechanism for testing and QA.
Application signing adds a bit more complexity on top of that.

Visual Studio Mobile Center aims to make the continuous delivery process much easier for development teams.
In a nutshell, it provides features that can help you build, test, distribute and get crash reposts and analytics about your mobile application from a single cloud service that is easy to use and configure.

In this tutorial I want to provide a brief summary and a step by step walkthrough of the build, test and distribute features of Mobile Center.

I will be looking at these features from an iOS developer perspective, and will be using a sample application written in Swift and Objective C that uses CocoaPods for dependency management.
The sample application is provided by Firebase and can found here.

Signing up for Mobile Center

The sign up process is very simple, just head over to Mobile Center and select to connect with one one of the supported authentication providers.

I personally chose to connect with GitHub since that is the code repository that I will be using.

Adding an app

Once you complete the signup process you will be given the choice to add a new organization or a new app.
If you would like to create multiple administrators and need to manage permissions, add an organization, otherwise you may proceed by adding a new app.

Selecting to add an app provides you with a dialog where you can provide a name for your app and select the OS and platform.
I will name my app Mobile Center Tutorial and of course select iOS and Objective-C / Swift for the application type.

The screen that follows provides instructions on integrating the Mobile Center SDK into the app. This is required if you want to take advantage of the crash and analytics features.

I will be covering these in a follow up post so you can bypass this at this stage.

Configuring the build

Navigate to the build section of the portal and select the type of your source code repository.
As I mentioned before I will be using GitHub in this instance.

After selection, if no further authentication and permission granting is required, a list of all available repositories in your account are listed for selection.

Selecting a branch

Once you select the desired repository, you are prompted to select the branch that you want to build.

Select the desired branch and also select setup branch.

Selecting a project and scheme

Most of the configuration items here are self explanatory and straight forward, you get to select the workspace, the scheme you want to build and the Xcode version that you would like to use.
You most likely have multiple schemes for the application, at lease a debug and a release I assume, and you will have to a create a build definition for each them.

I will also select the option to run XCTests and to sign the build.

Signing the build

Signing the build is required if you want to test on a real device and distribute the app to your team members.
It is recommended to use the manual provisioning option when running builds on Mobile Center.

You can configure Xcode to use a specific certificate and provisioning profile to sign a build. Make sure that you upload the exact same files to Mobile Center.

In order to sign the build you have to upload a provisioning profile and your developer certificate, if you don’t have these available you can follow the steps described here in order to generate and upload them.

Distribute builds

Here you have the option to distribute the app to the members of your team or organization.
I have opted to do every time there is a new build available.

Finally you can save your configuration.

The Build output

Saving a new build kicks off a new build task and it is displayed in the site dashboard.

Clicking on the build brings up the console that displays the progress of the build and the status of every task.

You will see a lot of information in the console and you will be able to identify some of the key stages in the build process.

Mobile center will create a new repository on the cloud and sync it with your repository.

It will install the required certificate and provisioning profile and install any dependencies via CocoaPods.
I personally do not include the CocoaPods source code into my project, I set the build configuration to do so during the build process.
As you can see Mobile Center did this automatically without any configuration from our end.

It will run all the tests for the application.

It will sign the build and create a distribution for the application package.

In the end it will perform a clean up and remove any credentials and files from the server.

If all has gone well you will see the success message where you have the option to download the packaged application as in .ipa file, the build symbols for debugging and of course the logs.

Test

The next step in the process is to run the UI tests that we have created for
the application.
The test feature of Mobile Center uses the excellent Xamarin Test Cloud service, allowing you to test the application against a number of physical devices and configurations.

When you select the Test section of the portal you are presented with the following screen,

go ahead and start a free trial.

You will now be presented with a screen informing you that that you don’t have setup any test runs yet.

Select start testing your app, you will be asked to select the devices and configurations against which you would like to run your Ui tests.

I selected the latest devices running on the latest version of iOS. I also selected to save this set for future reference.

Next we have to select the test framework, so I selected XCUITest since this is what we will use to test.

The screen that follows provides you with a set of instruction on how to prepare, generate and run UI tests.

Install the pre-requisites by installing Node.js, version 6.3 or later and install the mobile-center-cli NPM package by running the following command in a terminal:

npm install -g mobile-center-cli

preparing your solution for UI testing

The first step that is required is to set automatic signing or manual signing with a local certificate for the UI test target in your project.
I opted for automatic signing.

You now have to install the VSMobileCenterExtensions.framework to the app.
Since we are already using CocoaPods in our project it’s just a matter of adding the framework into the podfile under the UITests Application target.
For my project it looks like this

Close Xcode if it is open, from the terminal navigate to the directory of your project and run pod install
This should install the Mobile Center extension that allows you to take a screenshot of the application when the UI tests are running.
The instructions on how to achieve this can be found here.

Finally you need to build your application and XCUITest runner bundles, to do this run the following command from your project’s directory

$ rm -rf ddp #remove the derivedDataPath if it exists
$ xcrun xcodebuild build-for-testing -configuration Debug -workspace YOUR_WORKSPACE -sdk iphoneos -scheme YOUR_APPLICATION_SCHEME -derivedDataPath ddp

the second command looks like this for my sample project

$ xcrun xcodebuild build-for-testing -configuration Debug -workspace DatabaseExample.xcworkspace -sdk iphoneos -scheme DatabaseExample -derivedDataPath ddp

This will generate a test build folder named ddp on the root of your project.

Running Tests

To run the tests simply follow the instructions on the running tests section of the Mobile Center screen.

Run the command listed and replace the pathToXCUITestBuildFolder with the path to the .app file within the ddp folder that you generated above, most likely it will be /ddp/Build/Products/Debug-iphoneos.

If you are asked to authenticate to mobile center before you can run the command above, simply type the following command at the the terminal:

$ mobile center login

This will launch a browser window and once you verify your credentials, you will be provided with a token that you can use to authenticate from the terminal.

If all has gone well, you will see a success screen once all tests have been executed successfully.

Distribution

The final part in the configuration process is the distribution.
There is nothing to setup here at this stage as through the previous steps in the configuration, Mobile Centre has a group of users or a distribution group already available.

You will have probably noticed that after a successful build, Mobile Centre has sent an email to your users notifying them that there is a new release available which they can install and test.

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