Continuous Integration for React Native with TestFlight and TestFairy Deployment

I started working on a greenfield React Native project two weeks ago and the first order of business was to get continuous deployment setup so that the rest of the team could easily trigger and receive test builds of the latest code. In this post I will outline the process and configuration I used to get this setup and working.

Our Basic requirements:

Every time we push changes to our qa Git branch we want the CI process to build the code, run any tests, and then build and publish release-mode apps to be distributed to the internal team testers.

Each test build must have the same version and build number on both platforms (Android and iOS).

We want an email to get sent out to all testers when a new build is available.

Note: I will assume you have some familiarity with building React Native iOS and Android apps.

I will quickly outline the tools and services we used.

Build distribution services

We opted to use TestFlight for distributing the iOS test app, and TestFairy for distributing the Android test app.

Both have provision for auto-emailing testers when a new build becomes available.

CI choice

We’re building a React Native app for both iOS and Android devices. Thus we need OS X to run the build because the iOS build tools are (as far as I’m aware) only available on OS X. This means we need a CI service which offers OS X machines on which to build.

After some preliminary research, including trying out dedicated mobile app build services such as GreenhouseCI we opted to for CircleCI as our service of choice. They don’t try to do too much, and give you an OS X box on which you can install and run whatever you want, as well as do iOS builds. Their UI is also simple enough that other team members can login and trigger builds at will.

Moreover, they have excellent Github integration – you can not only use repo deploy keys, but also user keys if your build requires pulling in code from multiple private repos (see below).

Node modules dependency management

It is imperative that all developers and the CI build have the exact same package dependencies installed when building the app. I found yarn to be the most reliable package manager so far in this respect, and it has added benefit that it installs your dependencies a lot faster than NPM. By using Yarn combined with CI build artefact cacheing we’re able to get the dependency installation part of our build down to <2 seconds.

Fastlane – command-line build automation

The most painful part of doing iOS builds – and especially test builds vs production builds – is including the right provisioning profiles and signing certificates. Luckily, fastlane match automates the entire process of generating and including signing certificates and provisioning profiles.

Fastlane also provides other useful built-in commands, such as uploading the iOS app to TestFlight, and allows you to define and write your own commands which can additionally arbitrary external scripts.

Thanks to Fastlane, each developer is able to execute to build and deploy from their local machines too, which means we’re easily able to diagnose problems with the CI build.

1. Get it running locally

Before we setup the CI service we need to get everything working locally. Once the React Native app is building and running on devices we setup Fastlane and the pre-build process.

Pre-build setup

One of our requirements is for app builds to have the same build number in both Android and iOS. Fastlane (see below) has mechanisms for setting the app version and build numbers at buildtime, but not built-in central place for storing and keeping track of the same.

So, before we actually build the native app we run a pre-build script which sets up various native dependencies and, importantly, sets the app version and build number to use.

In our case we can use a private Github repo which contains a single JSON file to store the current app version and build number:

{"appVersion":"1.0","buildNumber":12}

When the pre-build step gets run we clone this repo, increment the build number and then push back the changes. We then place the final app version and build number into a file called appConfig.json which the Fastlane build makes use of (see below).

The pre-build step additionally updates the Android and iOS project files with the version and build number info (because injecting them via Fastlane) doesn’t always work):

Notice the private Github repository entered into the Matchfile. This is the repository in which match stores all the generated iOS certificates and provisioning profiles, encrypted. Even though the data is encrypted in the repository (match will ask you to set an encryption password), I recommend making this a private repo.

The apple_id parameter in the Appfile tells match which user to login to the Apple developer portal as in order to upload the generated certificates and profiles.

The first step is generate the iOS certificates and profiles:

$ fastlane match development
$ fastlane match appstore

One this is done the private Github repo (see above) will be populated with files.

We can now makes use of these files to build iOS apps. The Fastfile contains our custom actions:

fastlane_version"2.9.0"default_platform:ios# we will call some node scripts which are written in ES6 (see below)nodeExec='../node_modules/.bin/babel-node'# load in config generated in the pre-build step (see above)file=File.read('../appConfig.json')appConfig=JSON.parse(file)# iOSplatform:iosdodesc"Submit a new Beta Build to Apple TestFlight"lane:betado# fetch previously generated certificates, but don't generate new ones if none already existmatch(type: "appstore",readonly: true)# ensure we're on the "qa" git branchensure_git_branch(branch: "qa")# set the app build number from our previously generated configincrement_build_number(xcodeproj: "./ios/myApp.xcodeproj",build_number: appConfig["buildNumber"])# set the app version from our previously generated configincrement_version_number(xcodeproj: "./ios/myApp.xcodeproj",version_number: appConfig["appVersion"])# build the app for app store exportgym(clean: true,export_method: 'app-store',workspace: "./ios/myApp.xcworkspace",scheme: "myApp",output_directory: "./build-tools/deploy/data")# upload to TestFlight and notify testerstestflight(skip_submission: true)endend# Androidplatform:androiddodesc"Submit a new Beta Build to TestFairy"lane:betado# ensure we're on the "qa" git branchensure_git_branch(branch: "qa")# build the app in release modegradle(project_dir: "./android",task: "assemble",flavor: "defaultConfog",build_type: "Release",properties: {'versionCode'=>appConfig["buildNumber"],'versionName'=>appConfig["appVersion"]})# call a Node script to upload the generated APK to TestFairysh"#{nodeExec} ../build-tools/deploy/testfairy-apk-upload.js '../android/app/build/outputs/apk/app-instabug-release.apk'"endend

To build the apps we can do:

$ fastlane ios beta
$ fastlane android beta

Uploading to TestFairy

Fastlane doesn’t have built-in support for uploading to TestFairy, which is why in our Android build action (above) we call a script to do this for us:

2. Get it running on CircleCI

Now that the build is running locally we need to get it running on the CircleCI OS X machine. The key point to note is that the iOS dependencies (XCode, etc) are already installed whereas the Android SDK isn’t. Which means we need to install the Android SDK before we can do the Android build.

Note how we setup a global gradle.properties containing our Android app signing key passwords – this is so that when the Android Gradle build takes place it is able sign the final APK using our keystore file (bundled inside our repository). Note that we are using the same password for both the keystore and the private key – this is just to reduce the number of passwords we keep track off.

Also note how we set the ANDROID_HOME environment to where Homebrew installs the Android SDK by default. This variable gets picked up by the ci-install-android-sdk.sh script:

The reason for piping echo y into the android update commands is to auto-accept any licenses which get presented during installation. This is also why we copy the contents of our repo’s android-licenses folder into the SDK’s folder path – during the Gradle build there are likely to be other SDK tools which need installing and thus require license acceptance confirmation.

Certain environment variables need to be set in CircleCI’s Environment Variables settings:

ANDROID_KEYSTORE_PASSWORD – Password for both the Android signing keystore and its private key.

FASTANE_PASSWORD – Passsword for the Apple Id you entered in the Fastlane config file.

MATCH_PASSWORD – Encryption password for Fastlane’ match to use with the iOS certificate and provisioning data stored within the private Github repository entered in the Matchfile.

In CircleCI’s SSH settings (see Checkout SSH keys) a user key as opposed to deploy key needs to be used so that within a build we can clone other private repos within our Github organization, specifically the repo which contain the iOS certificates and profiles and the one which tracks the current build number:

And that’s it! If you now commit and push to the qa branch you should soon have two new test builds pushed to both TestFairy and TestFlight.