A Simple Continuous Deployment of a React Native App to Google Play

We will create a simple Github Action to build and deploy a React Native mobile application automatically upon pushing to a specific branch.

Photo by Kelvin Ang on Unsplash

Pre-Requisites

  1. A react-native application that builds successfully.
  2. An active Google Play account with the desired app already deployed (Google does not allow the first deploy to be programatic).
  3. A JSON key file for a service account user with access to the Google Play APIs. See how to create one here. Renamed to api.json and placed inside the android directory.
  4. A valid App upload key. See how to create one here.

Prepare for Secret Injection

To ensure authenticity, an uploaded application needs to be signed via an upload certificate that is password protected. In order to securely provide this password to our application at compile time we need to define it in our build configuration.

Edit the release certificate configuration in android/app/build.gradle by adding the following code block inside signingConfigs:

Then edit the line signingConfig signingConfigs.debug to signingConfig signingConfigs.release in buildTypes → release. These changes will allow us to inject the password for the upload key at build time.

Configure Fastlane

To ease integration with Google Play upload APIs we will be using Fastlane to build and upload our application. Fastlane needs to be configured locally before running it in our CI. For installation instructions on your local machine see here. To configure Fastlane, run fastlane init from inside the android application directory and follow the on-screen instructions. When asked for the location of the JSON key file enter api.json.

Edit the android/fastlane/Fastfile to include a deploy lane as shown below:

The -P flag is used to inject our runtime configuration as explained here. Note that these variables correspond to the passwords we setup in the previous section.

Saving Secrets

The JSON api key, the keystore containing the upload key and associated password should not be committed to GIT or otherwise exposed. Therefore, we will be creating the following secret variables using Github Secrets:

  1. STORE_PASSWORD → keystore password
  2. KEY_PASSWORD → key password
  3. UPLOAD_KEY → Base64 encoded upload keystore
  4. SERVICE_ACCOUNT_JSON → Base64 encoded service account API key

To base64 encode the upload key and service account use the command: cat fileToEncode.extension | base64 on your local machine.

The Action

Github Actions provide a way to transparently run deployment, testing and other workflows directly from within Github without having to configure or maintain infrastructure. These actions are fully defined using YAML files.

The full action can be seen in the code block below. It consists of the following steps:

  1. Clones the repository
  2. Retrieves the keys, base64 decodes them and dumps them to their respective files that can be accessed by later steps
  3. Installs all NPM packages
  4. Jetifi’s the application modules. This may be required if some of your dependencies are not yet AndroidX capable.
  5. Compiles and uploads the application using Fastlane!

Conclusion

Following the above steps you should be able to programmatically deploy the Android version of a React Native mobile application to the Google Play Store without human intervention.

The current setup deploys to the closed Alpha track where you can have a designated developer responsible for promoting it into a full release. However, you can change that to what works best with your organization’s release flow!

A curious minded engineer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store