ReactNative CI/CD integrated with virtual mobile device testing

CI/CD is an integral part of software development and operations, there are a myriad tools and services available for developers and IT operations professionals to choose from. In this article, I will walk you through the mobile CI/CD service called Bitrise and integrate it with AWS Device Farm for virtual device testing.

There are a number of different CI/CD services, but only a few actually specialise in mobile CI/CD, such as MS App Center, CodeMagic, Expo EAS and Bitrise. These services offer both basic and advanced code, as well as application integration, building, testing and deployment. Specialised mobile CI/CD services also take care of provisioning the build machines for you.

With Bitrise, you can access and adapt rich workflows with ease by either using one of the readily made templates, or choosing from a variety of workflow steps either from Bitrise itself, the community or your own. With config as code, Bitrise also allows you to save your CI/CD configuration to use it as part of your codebase.

The goal of this article is to demonstrate how to build an artefact from a ReactNative source code using Bitrise and do automated testing on virtual devices in AWS Device Farm. App signing and publishing to app stores will not be covered and, for the purpose of this demo, we will be generating an APK artefact for Android devices only.

Requirements

The following are the requirements for this demo, refer to the links below if you haven’t done the setup.

ReactNative setup

 

$ npx react-native init ReactNativeDeviceFarm
$ git add .
$ git commit -m “Game”
$ git remote add origin https://github.com/your-github-username/ReactNativeDeviceFarm.git
$ git push -u origin master

AWS Setup

In this setup, we are going to generate an AWS Access Key, a secret Device Farm project ARN and a Device Farm Device Pool ARN. All of these are required for the Bitrise setup.

Creating AWS Device Farm Project

Open your AWS Console and head to AWS Device Farm.

Step 1: Create a new Device Farm project under Mobile Device Testing by clicking the ‘New Project’ button.

Mobile device testing

Step 2: Name your project ReactNativeDeviceFarm.

Step 3: Record the Project ARN after the project is created.

Creating AWS IAM Role for Device Farm

Open your AWS console and head to the AWS IAM page.

Step 1: Create a policy with the following permission.

Project ARN
Create policy

 

{
    “Version”: “2012-10-17”,
    “Statement”: [
        {
            “Sid”: “VisualEditor0”,
            “Effect”: “Allow”,
            “Action”: [
                “devicefarm:ScheduleRun”,
                “devicefarm:ListDevicePools”,
                “devicefarm:GetDevicePool”,
                “devicefarm:GetRun”,
                “devicefarm:ListUploads”,
                “devicefarm:CreateUpload”,
                “devicefarm:GetUpload”
            ],
            “Resource”: “*”
        }
    ]
}

Step 2: Attach your policy to a user with Programmatic access only. Generate an AWS Access Key and AWS Secret key for the new IAM user.

Step 3: Record the credentials.

Getting the AWS Device Farm Device Pool ARN

AWS Device Farm Device Pool is a set of devices where you will run your mobile application for testing. You can create your own device pool and choose specific Android Devices and versions. In this article, we’ll just select the default device pool.

To get the device pool ARN, open up your command line and run the following commands.

 

$ export AWS_ACCESS_KEY_ID=EXAMPLE_ACCESS_KEY_ID
$ export AWS_SECRET_ACCESS_KEY=EXAMPLE_AWS_SECRET_ACCESS_KEY
$ export AWS_DEFAULT_REGION=us-west-2
$ export AWS_DEVICE_FARM_PROJECT_ARN=arn:aws:devicefarm:us-west-2:1234567890:project:EXAMPLE-AWS_DEVICE_FARM_PROJECT_ARN

$  aws devicefarm list-device-pools –arn $AWS_DEVICE_FARM_PROJECT_ARN
{
    “devicePools”: [
        {
            “arn”: “arn:aws:devicefarm:us-west-2::devicepool:EXAMPLE-DEVICE-POOL-ARN”,
            “name”: “Top Devices”,
            “description”: “Top devices”,
            “type”: “CURATED”,
            “rules”: [
                {
                    “attribute”: “ARN”,
                    “operator”: “IN”,
                    “value”: “[\”arn:aws:devicefarm:us-west-2::device:EXAMPLE-DEVICE-FARM\”,\”arn:aws:devicefarm:us-west-2::device:EXAMPLE-DEVICE-FARM\”,\”arn:aws:devicefarm:us-west-2::de>
                }
            ]
        },
        {
            “arn“: “arn:aws:devicefarm:us-west-2::devicepool:EXAMPLE-DEVICE-POOL“,
            “name“: “Web Performance“,
            “type“: “CURATED“,
            “rules“: [
                {
                    “attribute“: “ARN“,
                    “operator“: “IN“,
                    “value“: “[\“arn:aws:devicefarm:us-west-2::device:EXAMPLE-DEVICE-FARM\”,\”arn:aws:devicefarm:us-west-2::device:EXAMPLE-DEVICE-FARM\”,\”arn:aws:devicefarm:us-west-2::de>
                }
            ]
        }
    ]
}

The last command gave us a couple of device pools but we’re getting the Top Devices which has an ARN of: arn:aws:devicefarm:us-west-2::devicepool:EXAMPLE-DEVICE-POOL-ARN.

Now that you’ve done those steps, let’s list the requirements for Bitrise.

Bitrise project setup

In this setup, we will create our first Bitrise CI/CD pipeline for our ReactNative project. You will need a Bitrise account for this. If you haven’t done so, sign up for free at https://app.bitrise.io/users/sign_up.

To create a Bitrise ReactNative project, login to your Bitrise account and follow the steps below.

Step 1: Click the Dashboard on the top menu and you should see the screen below. Click the Add new app button and select Web UI from the dropdown menu.

Select Dashboard

Step 2: In the Create New App page, your default workspace is already selected but you need to select Private for your app’s privacy. Hit the next button.

Create new app

Step 3: Select the project in your Github repository.

Select project

You might need to add a permission for Bitrise to read your Github repository. ONce permitted, you’ll see the list of repositories from Github. Select ReactNativeDeviceFarm.

Step 4: The next configuration is to set the Bitrise repository access. Select Automatic and No, auto-add SSH key for creating an SSH key.

Bitrise repository access

Step 5: The next configuration is to choose the git branch you want to build. In this example I chose master. Hit the next button after you have chosen the branch.

Choose git branch

Step 6: This step will take some time to process as Bitrise will download the source code to their system. Once they read the source code, it will automatically detect that your project is ReactNative and you will be asked to define some values. Refer to the Project build configuration I have below. Hit the Confirm button once done, then click the skip buttons in the App icon and Webhook setup.

Step 7: A few more to go! You can now see your app in the dashboard. Click the ReactNativeDeviceFarm on the list of your apps on the right.

Select ReactNativeDeviceFarm

Step 8: On the ReactNativeDeviceFarm app page, click the Workflows button

Select workflows

Step 9: On this page, you can see the workflows as a well-presented UI. We’ll come back to this page in a while but now click the bitrise.yml tab on the far right.

bitrise.yaml

Step 10: This bitrise.yml page shows the configuration as a code, which can be replaced with the config prepared for this article. Open this bitrise.yml file, copy all the contents and replace the existing yaml configuration in your bitrise.yml page. You can also add this file to the root of your source code and Bitrise will use it automatically. Hit the Save button on the top right. Now click the Env Vars tab in the middle.

Env Vars

Step 11: The Env Vars page is where we will add our AWS ARNs. Under the deploy* Workflow Environment Variables, add the environment variables and their values AWS_DEVICE_FARM_PROJECT_ARN, AMAZON_DEVICE_FARM_ANDROID_DEVICE_POOL_ARN and AWS_REGION. Save those and click the Secrets tab next.

Note: The deploy workflow is a default name given by Bitrise, it’s just a name and it’s doing testing rather than deploying. You can change this name anytime.

Secrets

Step 12: In the Secrets tab, you will add the AWS credentials AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, save it and click the Workflows tab next.

Workflows

Step 13: In the Workflows page, select the deploy workflow and you’ll be presented with 11 workflow steps on the left.

Deploy workflow

The first steps are self explanatory so I’m going to start discussing the steps starting Install React Native.

  • Install React Native* – This step will install the React Native CLI tools used for bundling the ReactNative application

  • React Native bundle* – Bundling is the process of minifying your apps, getting all the important codes to run your code and placing the resulting code in android/app/src/main/assets/index.android.bundle

  • Install missing Android SDK components* – Install the Android SDK components that are required for the app

  • Android Build* – Builds the android project with gradlew command. This will include the output of the React Native bundle step at the top. This step generates the .apk file.

  • Deploy to Bitrise.io – Apps, Logs, Artifacts – This step will upload the generated artefacts (.apk file) in Bitrise and will be downloadable for installation to an android device.

  • Amazon Device Farm File Deploy* – This step will upload the .apk file in the AWS Device Farm project

  • Amazon Device Farm Runner* – This step will run the .apk file for testing in the virtual android devices in the selected device pool in your AWS Device Farm project. Note that BUILTIN_FUZZ is the AWS Device Farm testing type selected. That’s just a fuzzy test where random clicks and scrolls are going to happen. There are many test types to be selected in AWS Device Farm if you want to have more defined tests.

* These are the steps I’ve added since they are not part of the original workflow

Project build and test

Fun times! To run the project building and testing, click the ReactNativeDeviceFarm link in the breadcrumbs menu at the top.

Step 14: To start the build, click the Start/schedule build button on the top right.

Run project building and testing

When a modal appears, type in master as a branch, then select deploy for the Workflow, Pipeline box. Then hit the Start Build button.

Start build

Step 15: You’ll be presented with every step and status of the build. Once all steps are finished, you should see a SUCCESS status!

Bitrise success

Step 16: Go to the AWS Device Farm project, you’ll notice that there are test runs in different devices, each with the testing status.

AwesomeProject
Testing status

There’s also a screenshot and video recording done for you automatically by BUILTIN_FUZZ test.

Build machines

By using CI/CD specifically for mobile apps, you don’t need to worry about provisioning your own machines, which is especially a headache when building artefacts for Apple devices.

Bitrise comes with 11 build machines to choose from for building iOS apps ranging from XCode v11 – v14 on MacOS 10-12. They regularly update the build machines list when there’s a new XCode or MacOS released.

Similarly, Bitrise also comes with a default Android & Docker on Ubuntu 20.04 for building Android apps. The good news is that you can also bring your own docker image and use it to build your Android apps.In terms of pricing, an Android project build costs 1 credit/minute, and iOS project build costs vary from 2 – 6 credits/minute. More pricing details can be found here.

Summary

With the help of config as code and rich Bitrise integration support, we’ve quickly and successfully generated an .apk file and run an automated test on AWS Device Farm.

Running tests along with your build pipeline is critical to ensure that you are shipping confidently with quality code. Needless to say, automated testing reduces human error and the time spent on manual testing can be shifted to building your product.

Thanks for reading and I hope you’ve learned something new in this article!

Enjoyed this blog?

Share it with your network!

Move faster with confidence