React Native & Fastlane for iOS (TestFlight beta releases)
Tags: reactnative, fastlane
Published: 2020-08-25 13:52:18

When you build React Native applications, you have three options to automate the building process. From the official docs you can use Expo or do it via Xcode or Android Studio. Expo simplifies life a lot but has some disadvantages: you can use libs only supported by expo, you do not have webRTC, you can not change native code. But in case you do not need all, this is the best choice. Manual deployment. Well, it works. But you have to do everything manually! The third option is to use automation tools like Fastlane. You can wrap all the process in scripts and just watch! Actually, Expo uses Fastlane on their servers.

In this guide we will set up Fastlane for a bare React Native project for iOS only to automate TestFlight deployments.

Foremost - Fastlane should be installed locally. On MacOS run the following commands:

            `xcode-select --install
            brew install ruby
            sudo gem install fastlane -NV`

After Fastlane successfully installed navigate into ios/ directory in your React native project and run

            `fastlane init`

You’ll be asked to answer some questions. For our purposes, please choose “Automate beta distribution to TestFlight” option. Next select Scheme. Provide your Apple ID and Password (in case you have two factors authentication enabled, generate “App-specific password”). If you did not set up your app on Appstore Connect, you’ll be asked if you want to create it.

Initial setup is finished and everything should work “out of the box”. You can find configuration files at “ios/fastlane” folder. The only issue that everything will work locally. In case you have a team it would be hard to deal with certificates, provisioning profiles, etc.

Fastlane provides a sign-in method called Match. The idea behind is that you have an additional repository for certificates and provisioning profiles, and everyone who has access to this repository can sign and distribute the app.

To set up Match method you should run in iosit directory

            `Fastlane match init`

You’ll be asked to provide a link to GIT repository for certificates (any link, http or git). After that you need to run:

            `fastlane match appstore`

To create certificates and profiles.
During setup you’ll be asked for a passphrase for certificates. As a result, it will push your certificate to your GIT repo.

To clear existing certificates you can run

            `fastline nuke distribution`

In case you provision profiles or entitlements will be updated you should run

            `fastline match appstore --force`

For force update of your remote repository

Let's make some adjustments to the building process. In the ios/ folder run
fastlane add_plugin load_json
We need this plugin to read package.json file.

Replace content of ios/fastlane/Fastfile with

            `default_platform(:ios)
        platform :ios do
        desc "Push a new beta build to TestFlight"
        lane :testflight do
            sync_code_signing(type: "appstore")
            package = load_json(json_path: "../package.json")
            increment_version_number(version_number: package["version"])
            increment_build_number(build_number: latest_testflight_build_number + 
        1, xcodeproj: "vidsig.xcodeproj")
            build_app(workspace: "vidsig.xcworkspace", scheme: "vidsig")
            upload_to_testflight
            slack(
                message: "App successfully uploaded to TestFlight.",
                success: true,
                slack_url: "WEBHOOK URL"
        )
     end
   end`

Here we have a lane that will:
–Sync code sign-in assets with your MacOS
–Set up version number as provided in package.json
–Increment build number based on the latest in Appstore Connect
–Build and upload your app to Testflight
–Send message in Slack channel

To set up webhook URL use the following guide: https://api.slack.com/messaging/webhooks

And the last - in your package.json file add the following into ‘scripts’ section

            `"fastlane:ios:testflight": "cd ios/ && fastlane testflight"`

That’s all! Now you can run

            `npm run fastlane:ios:testflight`

and enjoy automation! After the build is processed you’ll get a notification into Slack channel.

To run configured repository with Fastlane on another MacOS you need to take the following steps:

1. Install Fastlane locally
2. Navigate into ios/ directory and run fastlane match appstore to sync certificates. Enter passphrase.
3. Run npm run fastlane:ios:testflight in the root directory of the project.

Enjoy!

P.S. Need help - contact our [react native developers](https://webspaceteam.com/react-native/) team.