React Native updates with VS App Center CodePush
Tags: reactnative, codepush, javascript, ci
Published: 2018-06-12 13:05:53

---
title: React Native updates with VS App Center CodePush
published: true
description: 
tags: React Native, Code Push, javascript, Continuous Integration
---

One handy feature of **‘not-so-native’** apps which get easily overlooked is —
to publish updates without having to go through the usual pain of submitting an update in the play store/app store.

In this tutorial, we’ll see how we can send CodePush updates using the Visual Studio App Center along with CI distribution.

**Prerequisites**

1.  [GitHub account](https://github.com/)
1.  [VS App Center account](https://appcenter.ms/apps) (You could sign in using
github)
1.  [react-native
cli](https://facebook.github.io/react-native/docs/getting-started.html)

**Step 1:**

Create a react native app using the react-native CLI and push the code to a GitHub repo.

{% gist https://gist.github.com/Chandrasekar-G/f82931743b514704329ea4e1f352e583 %}

This is how your app will initially look like when you run **react-native run-ios**

![](https://cdn-images-1.medium.com/max/1600/1*nZ1mI_LbEcyG6AJuJOU4Bg.png)

**Step 2:**

2.1 Login to [http://appcenter.ms/](http://appcenter.ms/). On the Home page, click on the **Add new App** button.

![](https://cdn-images-1.medium.com/max/1600/1*yXJ1it1Itd3KKQQDRxu26w.png)

![](https://cdn-images-1.medium.com/max/1600/1*kaYaLN7JOKS1OLVemDwtmA.png)

In the model shown, fill in the details of the app you’re developing. Although we use the same codebase for both the android and iOS versions of our apps, the build process is entirely different. Hence, we’ll have to create separate apps for iOS and Android respectively.

For the sake of the tutorial, we’ll choose the OS as android. The steps will be the same for iOS except that we’ll have to choose the provisioning profile and certificates instead of the android Keystore file.

![](https://cdn-images-1.medium.com/max/1600/1*fD6qkGzNxZpKbrMqZVsAQA.png)

2.2 Now, link our remote repo in GitHub with this app. You’ll see the list of branches available in the repo. We just have a master branch as of now.

![](https://cdn-images-1.medium.com/max/1600/1*t0n450-lqS7lHGKETvEMug.png)

2.3 Click on the gear icon next to the desired branch. This will be the branch we’ll be using for continuous integration.

2.4 To distribute the built apk/ipa files to users we’ll have to sign our builds. For android, we’ll have to generate a** Keystore file** first to be used for signing the build. This is a sample command for generating a Keystore file.

{% gist https://gist.github.com/Chandrasekar-G/faba505c04da69c1323371eacc78b62a %}

![](https://cdn-images-1.medium.com/max/1600/1*ekipYt1fAkB85KV90OP4aQ.png)

2.5 Turn on the ‘**Sign Builds’** option and **‘Distribute Builds’ **option. Upload the Keystore file we just created. Enter the password for Keystore file, key alias and the key password which we gave in the previous prompt.

For iOS, this step will be a little different. We’ll have to choose the **provision profile **and the** p12 certificate** and provide it with the requires passcodes for the same.

![](https://cdn-images-1.medium.com/max/1600/1*hBcD9nGbMxTDwo_9DyeEQQ.png)

![](https://cdn-images-1.medium.com/max/1600/1*R-mOkK2xCsoYhW8gtEc9Pw.png)

Once we click **‘Save and Build’** option, a manual build will be triggered. Any subsequent commits to the master branch will automatically trigger builds unless we’ve manually turned off that option.

![](https://cdn-images-1.medium.com/max/1600/1*PM_bpO_ESuFpJJoAVYy0zg.png)

Once the build is complete, you’ll receive an email with a link to download the app. Now, our app is available for distribution and we can add collaborators to use these apps.

![](https://cdn-images-1.medium.com/max/1600/1*htVtY89arcBnop3z9yEGmA.png)

We have now successfully established a CI flow for our react-native android app along with app distribution

**Step 3:**

3.1 Install the app center command-line utility. This includes a host of other features apart from codepush like crashes, analytics…

Log in to the app center. This will open up the browser and display you an authentication token. Copy and paste this token to the command window.

{% gist https://gist.github.com/Chandrasekar-G/70a1de44732167d41f4d40b5db5a8095 %}

![](https://cdn-images-1.medium.com/max/1600/1*TuNjoiHCz_sR3vZNZOSF4Q.png)

3.2 Go back to the app center. Choose our app and select **Distribute -> CodePush. **Select the Create Standard Deployments button. This will by default create two standard deployments for Production and Staging.

![](https://cdn-images-1.medium.com/max/1600/1*jr5b9WBP6jcCprvQEFN1Qg.png)

3.3 Now, go back to the terminal again and get the deployment lists. This will give us the list of keys available for the standard deployments available. Make a note of the staging key as we’ll be using this key in the next step.

{% gist https://gist.github.com/Chandrasekar-G/1a4696e5e61834e639127330bfb9b145 %}

![](https://cdn-images-1.medium.com/max/1600/1*QkFKvgkIEMYI1IMV1VQY6g.png)

**Step 4:**

4.1 We’ll now add the code push client-side SDK to our react native app so that it will be able to listen for updates from the app center and make updates accordingly. Go to the project directory and install the code-push SDK.

4.2 After this use the **react-native link** command to link this npm module with the android native build. Here you’ll be asked for the deployment keys which we got from the previous step. Use the same key for both android and iOS since we’ll be dealing with the iOS update in a totally different app.

{% gist https://gist.github.com/Chandrasekar-G/e0407e22075ad51742ab149f0ef9921d %}

![](https://cdn-images-1.medium.com/max/1600/1*3bETnhznNVkPT0dDxSbdGQ.png)

**Step 5:**

Our next step will be to use this client-side SDK and make changes in our code so that our react native app is able to recognize the updates.

We’ll create a button, on press of which the app will search for updates.

5.1 Go to app.js and import the npm module.

{% gist https://gist.github.com/Chandrasekar-G/86322fe328a8d7bf35cec7134ecc469b %}

5.2 Add a Touchable Opacity with a corresponding handler.

{% gist https://gist.github.com/Chandrasekar-G/d948a477b8f35862a8469143145edca7 %}

{% gist https://gist.github.com/Chandrasekar-G/a4c5bed545b968a39b68a723356509dd %}

5.3 Make the above changes and check in the code in the remote git repo. This will trigger a build automatically in the app center and we’ll receive an mail with the new download link for the updated app. Once installed, it’ll look something like this

![](https://cdn-images-1.medium.com/max/1600/1*qmzkAHBGOFPe5MJU_6cXCQ.png)

**Step 6:**

We’ll now make some text change in our app and release a build using code push.

6.1 Make some text changes in the app. Now, go to the terminal and release a build using code push in staging. Now, this release will be available for apps in the version mentioned in the app.

{% gist https://gist.github.com/Chandrasekar-G/cdbb8f9f9c2254e67fd631db47020a2a %}

![](https://cdn-images-1.medium.com/max/1600/1*-4A0vJrJLmjJUNJkUezQSw.png)

6.2 Now open the latest mobile app installed. You’ll be prompted with a dialog to update the app. When you click install, the latest update will be installed and you’ll be able to see the text changes we just made.

![](https://cdn-images-1.medium.com/max/1600/1*I80nWIL_9zainsoef0HEkA.png)
![](https://cdn-images-1.medium.com/max/1600/1*051YkTHVNwaDfHHYW3DHsQ.png)

Here's the github repo if you need a reference,

{% github Chandrasekar-G/ReactNativeCodePush %}