Automating CodePush deploys with Fastlane
CodePush is a great service that allows you to update your React Native apps over the air without waiting on the user to update their app through the App Store or Google Play. CodePush is extremely configurable, and includes a CLI to configure and initialize deploys. Despite a simple release-react
command and some good defaults, there are lots of questions and parameters to remember, such as:
- What track should I use (dev, staging, or production)?
- What version of the app should this deploytarget (what could break by releasing this)?
- Do I want to force-apply this update immediately and not wait until the next time the app resumes?
Why Automate?
A common thread exists between every software developer, regardless of experience. We’re all human, and humans make mistakes.
The best way to reduce the likelihood of a mistake is to automate as much as possible in your workflows and processes. In the React Native world, the best deploy-related tool we’ve found to do that is fastlane.
NOTE: If your impatient, and just want to see the full script we’re using, skip to the bottom of the page. For more detail on each piece, read on.
Create Codepush Lanes
We’ll start by creating our fastlane entry points, a lane called codepush_ios
and a lane called codepush_android
.
The only job of those lanes is to pass an app name param to a private lane (think of these as private methods).
The main script
The main part of our script first checks to make sure there are no local git modifications, and wraps the built-in release-react
command with a nice confirmation message. This gives a user a chance to make sure everything is correct before continuing.
To keep things clean, we handle getting the correct CodePush environment and app version from other private lanes.
Select a proper Codepush Environment
These private lanes handle fetching the available CodePush environments and prompting the user to select one. By default AppCenter will create a Staging and Production environment, but you can create as many as you’d like.
Select a target app version
This private lane will parse the current app version (it currently assumes iOS and Android are versioned in lockstep), and prompt the user for a target version.
Assuming the current app is version 1.3.0, selecting “All users” will apply the codepush update to every version. Selecting “most recent major” and “most recent minor” will target1.x.x
and 1.3.x
respectively, and “Current” will target 1.3.0
only. To make sure this is clear to the user, we show the version target in the select prompt.
That’s all folks!
Putting this all together, you now just have to run fastlane codepush_ios
and fastlane codepush_android
, answer a few questions, and watch it 🚀!
Here’s the full script:
If you’d like help automating your React Native workflow, say hello! We’d love to hear from you.