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/
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.
Before we begin, there are some prerequisites that need to be installed locally.
- Fastlane CLI
In your project root directory create a folder called fastlane, inside which you will need to create the following files
- 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@example.com, firstname.lastname@example.org
Let’s get to business then…
First, let me summarise all the steps that will lead to a successful deployment
- Increment Build Number / Version Number
- Override some build settings to align with Fastlane
- Create a keychain to store values (only required when using CI)
- Obtain Certificates and Provisioning profiles to sign the app
- Update code signing settings
- Build and archive the app
- 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.
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 a new Keychain
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.
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!)
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.
Build and archive the app
For this step, we will be using thegym service of Fastlane
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!
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
- Set Node Version to 12.17.0
- Run yarn install and install required dependencies
- Run pod install to install Cocoapods dependencies
- Run Fastlane to create the signed IPA Build
- Upload the IPA File as an artifact on Github
The environment variables for reference are
The unique identifier your app
Your Apple ID / Email
Your Apple Developer Account Password
Password entered while creating Github Repo with Match
Name of your keychain
Password of your keychain
URL of Github Repository containing your certificates
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!
Fastlane just saved me an hour of my time! Just imagine how many hours developers can save in their lifetime!