Back to Blog

Automatic Deployment of React Native iOS Apps with Fastlane and Github Actions

Prameet Chakraborty
Image for post

As developers, we have all faced the urge to automate our deployments in order to save precious development time. This urge escalates to its crescendo especially when dealing with mobile app deployments due to the complex and lengthy processes involved.

Any iOS Developer would relate to time wasted sitting in front of their machines waiting for the signed build to archive and the whole nightmare of uploading to testflight or the App store! The story with Android platforms is no different.

Thankfully, these days we have great tools and choices that are so remarkable that they not only make our development life easier but also carves out more time for things that really need our focus, clearly deployment is not one of them!. One of these great tools is fastlane

Their official website is https://fastlane.tools/

fastlane

https://docs.fastlane.tools/

In this article we will look at setting up automatic deployments of React Native iOS Apps with Fastlane on a CI Tool called Github Actions or similar

While I was working on it, I couldn’t find a comprehensive article that explained all of the processes in tandem, so I decided to write one myself to help developers in the community who are looking for better ways to automate their build process.

Prerequisites

Before we begin, there are some prerequisites that need to be installed locally.

  1. Fastlane CLI
  2. XCode

In your project root directory create a folder called fastlane, inside which you will need to create the following files

Image for post
Image for post
  • The Fastfile is the brain of the whole process; We shall discuss it in detail in the below sections.
  • The Appfile contains app specific information
  • The Pluginfile contains plugin dependencies (autogenerated when you install dependencies)
  • The testers.txt contains all testers’ emails separated by commas, eg: [email protected], [email protected]

Let’s get to business then…

First, let me summarise all the steps that will lead to a successful deployment

  1. Increment Build Number / Version Number
  2. Override some build settings to align with Fastlane
  3. Create a keychain to store values (only required when using CI)
  4. Obtain Certificates and Provisioning profiles to sign the app
  5. Update code signing settings
  6. Build and archive the app
  7. Upload and distribute to testers

Yes that’s it! These are all the steps that we will go through to get the app deployed!

Now let’s get to each step in detail!

Increment Build Number / Version Number

The first step involves updating the build / version number so that it does not conflict with your earlier build.

increment_build_number

http://docs.fastlane.tools/actions/increment_build_number/#increment_build_number

Updating version number is optional. However please note that build numbers have to be updated every time you create a new build to maintain uniqueness

To achieve this, we use fastlane’s increment_build_number and increment_version_number methods and pass the path to our Xcode Project.

The bump type of patch indicates that we are doing a minor version change.

Override some build settings to align with Fastlane

Next, we shall make some small manual overrides to make sure that we are using the right settings before building

Here we are explicitly setting our Bundle Identifier, Provisioning Profile and Development Team.

Create a keychain to store values (only required when using CI)

This step is only required if you are running Fastlane on a CI Tool like Jenkins, Github Actions, Bitrise etc. Locally it would use your system keychain

create_keychain

Create a new Keychain

http://docs.fastlane.tools/actions/create_keychain/#create_keychain

docs.fastlane.tools

We need to specify a name and password for our keychain, make sure you note down your CI Keychain password safely.

Obtain Certificates and Provisioning profiles to sign the app

For this step we will be using the match service of fastlane that can dynamically pick up certificates and profiles directly from a Github repo.

match

https://docs.fastlane.tools/actions/match/

Before setting up match we need to first store our certificates safely in a private repository.

  • Create a Private Github Repository
  • Then run fastlane match development locally and follow all the steps, other methods are appstore , ad-hoc and enterprise
  • The earlier step would automatically set up all certificates, profiles and push to the remote repository.
  • When asked for the repo url, it needs to be specified in this format (IMPORTANT!)

https://<GITHUB_TOKEN>@<REPO_URL>

NOTE: Setting readonly: true ensures that certificates are not generated again when match is run

Update code signing settings

Next step is to modify the code signing setting and force the usage of manual signing.

update_code_signing_settings

http://docs.fastlane.tools/actions/update_code_signing_settings/#update_code_signing_settings

Build and archive the app

For this step, we will be using thegym service of Fastlane

gym

http://docs.fastlane.tools/actions/gym/

Upload and distribute to testers

This is the final step. After archiving and signing the build we shall upload it and release to testers directly.

We are using Testflight to distribute our builds. Firebase App Distribution is another great alternative!

upload_to_testflight

https://docs.fastlane.tools/actions/upload_to_testflight/#upload_to_testflight

The final Fastfile would look like this…

To run this, just go to your root directory and run fastlane ios beta , this is because we are calling our lane as beta in line 97. Feel free to give any name to your lane!

Set up Fastlane on CI

Now, a final additional step is to setup this fastlane process on the CI. In our case we have used Github Actions!

We are configuring the build to run on every push action to the repo.

The above code snippet shall do the following

  1. Set Node Version to 12.17.0
  2. Run yarn install and install required dependencies
  3. Run pod install to install Cocoapods dependencies
  4. Run Fastlane to create the signed IPA Build
  5. Upload the IPA File as an artifact on Github

The environment variables for reference are

APP_IDENTIFIER

The unique identifier your app

APPLE_ID

Your Apple ID / Email

FASTLANE_PASSWORD

Your Apple Developer Account Password

MATCH_PASSWORD

Password entered while creating Github Repo with Match

CI_KEYCHAIN_NAME

Name of your keychain

CI_KEYCHAIN_PASSWORD

Password of your keychain

GIT_REPO_URL

URL of Github Repository containing your certificates

DEVELOPER_TEAM_ID

Apple Developer Team ID

If you want to run the whole process locally from terminal, setup a .env file in your fastlane folder add these variables

VOILA! Our automatic deployments are now set up. At the end fastlane also tells you how many minutes you saved. Sweet!

Image for post

Fastlane just saved me an hour of my time! Just imagine how many hours developers can save in their lifetime!

Image for post

More from the Blog

Interns Spotlight: Mickey Yau, Software Engineering Intern S20

Being passionate about start-up and various new technologies, Mickey is a penultimate year student studying dual degree in Computer Science and Business in HKUST.

Read Story

Syncing Git Repos in Real-Time: Part 1

Gitstart Fork: Fork is an internal tool we use which leverages the power of web hooks in order to do almost real-time syncing of code across a pair of repositories, seamlessly.

Read Story

How to handle multiple webhooks at scale

There has been a trend moving from using APIs where you have to constantly request data from a third party service towards Webhooks where the third party service you are using instead will send a POST request to your url of choice

Read Story

Stay in the Loop.

Gitstart runs a monthly email newsletter for developers, engineering leaders and CEOs, covering latest tips and trends.
We will never share your email address with third parties.