From Manual to Marvelous: How Fastlane Automate your React Native Build and Deployment

Abdelwahed ELMAJOULI
7 min readAug 4, 2023

--

Motivation :

Are you tired of spending countless hours releasing new versions of your mobile app to end users or beta testers? Do you use multi-platform library like Flutter or React Native and dread the thought of releasing on two platforms? Fear not! By automating your release process with fastlane, you can save time and focus on what you love — evolving your skills and enjoying coding sessions with a hot cup of coffee. In this blog post, I’lI share everything I know about build and release automation using fastlane. Let’s get started!

(In the sake of simplicity, I’ll do my best to explain everything I know in details taking IOS platform as a base for our example)

What is Fastlane?

Fastlane is a tool that helps mobile developer on both platforms Android/IOS to not only build and deploy their release target automatically, but also it regroups a dozen of useful commands and extensions that handle tasks like bumping versions, code signing, screenshots and more...

Fastlane consists of several tools, including:

  • Gym: a tool that automates the process of building an app, including compiling source code, resolving dependencies, and creating an executable file.
  • Match: a tool that automates the management of code signing certificates and provisioning profiles.
  • Pilot: a tool that automates the process of uploading apps to the App Store and TestFlight.
  • Snapshot: a tool that automates the process of taking screenshots for app store listings and marketing materials.

Fastlane setup:

Ruby:

Fastlane is based on Ruby. Hence, installing and managing ruby environment is mandatory. One of the easiest and recommended ways is by using rbenv tool. It allows developers to easily switch between different versions of Ruby, and ensures that the correct version of Ruby is used for a particular project.

Bundler:

In the Ruby realm, Bundler plays the role of a package manager, similar to npm on the JavaScript side. Bundler works by defining a Gemfile, which lists the required gems (packages) and their specific versions for a project. Bundler then installs these gems and their dependencies into a separate directory, called a gemset, which is specific to the project. This ensures that the project has access to the correct versions of gems, regardless of the versions installed on the system.

Install Bundler by running :

gem install bundler

Create ./Gemfile if not exist on your root project directory and add the following:

gem "fastlane"

Install all required gems:

bundle install

Potential issues:

You may encounter some problems due to incompatible Ruby version issues. for instance this error can appear:

❯ bundle install
/Library/Ruby/Site/2.6.0/rubygems.rb:263:in `find_spec_for_exe': can't find gem bundler (>= 0.a) with executable bundle (Gem::GemNotFoundException)
from /Library/Ruby/Site/2.6.0/rubygems.rb:282:in `activate_bin_path'
from /usr/local/bin/bundle:23:in `<main>'

This error is thrown due to an incompatible Ruby version. Try to fix it by updating your environment ruby version to match the ./_ruby-version.

IOS build automation:

After setting up our Fastlane environment, let’s deep dive into how we can automate our iOS build with only one command.

On our IOS root directory, run the following command to generate fastlane configuration:

bundle exec fastlane init

The command will prompt us to provide general information about our application and App Store credentials to easily push our build.

At the end it will create a fastlane folder with generated Fastfile and Appfile. Don't worry if you didn't understand their content we will explore them further.

Exploring Fastfile and AppFile:

For those of you who are familiar with Ruby. They already guessed that it’s a block of Ruby code defining some instructions. Oh yeah Fastfile is just an ordinary ruby syntax with an additional concept of lane.

platform :ios do
desc "Push a new beta build to TestFlight"
lane :beta do
increment_build_number(xcodeproj: "XhubTemplate.xcodeproj")
build_app(workspace: "XhubTemplate.xcworkspace", scheme: "XhubTemplate")
upload_to_testflight
clean_build_artifacts
commit_version_bump(message: 'bump build')
push_to_git_remote
end
end

Lane is simply a wrapping block of instructions. We can create as many lanes as we want. In our case, we have one lane named beta. It's responsible for incrementing our build number, building the app, uploading the artifact to TestFlight, and removing the build results. I've added also the two last commands to commit the last version bumping and push changes to the remote repository.

Usually, before publishing our application to our end beta tester we wanna make sure that we are on the right git branch and all our changes are taken into consideration. To do so we can add before_all block that gets run before any lane execution.

before_all do
ensure_git_branch( branch: 'main')
ensure_git_status_clean
git_pull
end
...

Now we are all set, we can launch our first fastlane project. Use this command (beta is the name of our lane):

bundle exec fastlane beta

Oops! Didn’t work correctly. No worries! Let’s see what went wrong.

From the log above, it seems that we are encountering some code signing issues. Which is normal because we are trying to build and archive our application without providing the mandatory code signing requirement.

Match tool and Code signing:

In the software world code signing ensures the identity of the author, the integrity of the code, build system and versioning. So the user of the application feels secure and safe while using it.

To solve our problem there’s two ways. Manually by creating our certificate signing request(CSR), upload it to the apple developer portal, then download and install the certificate on our local machine and finally generate our application provisioning profile. Otherwise, we can go with Fastlane match tool.

Match is one of the most popular tools in fastlane. It makes it easy to manage code signing by automating the process of generating and distributing certificates and profiles. It does this by creating a secure Git repository that stores the certificates and profiles, encrypted with a passphrase. The repository can be shared among team members and used to store multiple sets of certificates and profiles for different projects.

Let’s get ourselves in gear for using match. first create your github private repo to store your certificates, private keys, public keys and provisioning profile. Initialize Match: Navigate to your project directory in the terminal and run the following command to initialize Match:

bundle exec fastlane match init

In the prompt, chose git as your cloud storage and provide match with the link for your private repo. This will create a Match configuration file named Matchfile. It Contains all necessary information to run match such as our git repo, type of certificate...

Run the following command to run Match and create the certificates and profiles for Appstore you can go also with development or Adhoc. It depends on your needs:

bundle exec fastlane match appstore

Now we can continue our Fastlane adventure. We’ve successfully created our provisioning profile. We’re almost finished, just one more thing to do. We need to install the provisioning profile and configure it to our application. To do so, add this line on your beta lane:

identifier = CredentialsManager::AppfileConfig.try_fetch_value(:app_identifier)
....
platform :ios do
desc "Push a new beta build to TestFlight"
lane :beta do
match(type: "appstore", app_identifier: identifier)
build_app(workspace: "XhubTemplate.xcworkspace", scheme: "XhubTemplate",export_method: "app-store")
....

Run again the command bundle exec fastlane beta.

Hallelujah!! Everything works now as expected. You can see your project on your Appstoreconnect portal.

Conclusion:

Fastlane is a powerful and popular open-source tool for automating mobile app development workflows. It provides a wide range of features that helps developers to streamline the development process, including automating tasks like building, testing, and deploying mobile applications. With Fastlane, developers can automate many time-consuming and error-prone tasks, making it easier to build and release high-quality mobile applications.

One of the key advantages of Fastlane is that it is highly customizable and flexible, allowing developers to configure it to fit their specific needs. For example, they can use Fastlane to automate their entire development workflow, or just specific tasks, depending on their preferences and requirements.

Another advantage of Fastlane is that it has a large and active community of developers who contribute to its development and maintenance. This means that there is a wealth of resources available, including documentation, tutorials, and plugins, that can help developers to get started with Fastlane and troubleshoot any issues they may encounter.

Overall, Fastlane is a valuable tool for mobile app developers looking to improve their development workflows, save time, and build high-quality mobile applications. It has become an essential tool in the mobile app development ecosystem, and its popularity continues to grow as more developers adopt it for their projects.

--

--