Using fastlane is one of the easiest ways to automate the build and release of mobile apps. The code build automation tool is portable across any platform and can automate publishing to Apple's App Store and the Google Play Store.
Dev teams using this tool don't need to track different versions of build tools used by team members to avoid compatibility issues. With fastlane, teams can also quickly publish beta builds and get feedback on their applications before release.
Fastlane integrates with TestFlight, a service that allows a team to test their iOS application with early access and beta testers before it's published. This tutorial will show you how to use fastlane to create a beta build in TestFlight and an app to be released to the Play Store.
Continuous deployments of beta builds reduce the time to production of your applications. Automating builds reduces the workload on the QA team to test and deploy the application. In addition, there are several tools available to automate the building and deployment of mobile applications to app stores.
Fastlane boosts your automated build and deployment processes in multiple ways, including the following:
Fastlane categorizes build tasks into actions called lanes, which are executed independently of each other. A fastlane lane is similar to a function in any programming language. Below is a sample fastlane action:
To execute the above lane, you would run the following command in the terminal:
You’re going to set up a fastlane script to automate the building, signing, and deploying of iOS and Android applications to TestFlight and the Play Store. To follow along with this tutorial, you can clone the Android project from this GitHub repository and the iOS project from this GitHub repository.
For this tutorial, you'll need to install the following tools:
You have two options for installing fastlane:
Choose your preferred option. Once fastlane is installed on your computer, navigate to the root project folder of both your Android and iOS projects and execute the command below to initialize fastlane:
This command should generate a fastlane folder containing a Fastfile and an Appfile, as shown in the folder structure below:
You need a discrete fastlane setup for each repository holding your project. In a situation where you are using a monorepo (ie, same repository for iOS and Android code), you can initialize fastlane for each platform project and then merge the resulting files into one on the root folder. If you are using cross-platform tools like Flutter and React Native, you will have Android and iOS project folders in the same project. You'll need to initialize fastlane within each platform's respective folder, as shown in the folder structure below:
The Appfile can be used to specify the name of the application and the bundle identifier. Although you can set the bundle identifier in the Fastfile, it's more convenient to externalize it in the Appfile. You can also define more than one application bundle identifier in the Appfile for each lane. This is important when you want different bundle identifiers for other build types, such as development, staging, and production builds.
The Fastfile is the main file that contains the build tasks. It's also a Ruby file that contains the commands that fastlane executes.
Fastlane provides various actions that you can use to automate the build process:
Add the relevant code below in the Fastfile you created in the previous step.
For the iOS platform:
For the Android platform:
Note: You should change the app_identifier to match your application's identifier.
In the Fastfile above, default_platform defines the platform on which fastlane will execute the build, while platform :ios do or platform :android do defines the platform under which you will add the actions.
To execute the above lane, run the command fastlane android staging on the Android platform or fastlane ios staging on the iOS platform.
Before writing functionality within the lanes, you should be familiar with the steps required to build and deploy your applications:
The final steps involve building an IPA or APK file to distribute the application and uploading the IPA or APK file to TestFlight or the Play Store.
Store the key in a location that limits access by unauthorized users. For this tutorial, you'll store it in a Git repository.
Create a directory named keys and move the .p8 API key file downloaded into it. Initialize Git in the keys directory, then push the repository to GitHub as a private repository.
Note: The keys directory should be separate from the project directory. The main reason for this is that you should not push your .p8 key file to the same repository as your project, to limit access to the .p8 file.
In the fastlane directory, create a .env file and add the following environment variables, replacing the values with the actual values obtained from App Store Connect and GitHub:
In the Fastfile for your iOS project, add the following code:
The code clones a Git repository containing the .p8 API Key file by utilizing the app_store_connect_api_key action.
Create a new function named setup_ci as shown below. In this function, you will add all the required scripts to build and deploy to TestFlight:
Update the staging functions created previously on the iOS project with the code snippet below:
The code snippet calls the setup_ci function to set up the CI script and the setup_api_key function to set up the API key for App Store Connect.
Finally, update the build iOS app lane with the code snippet below:
Now that everything is set up, you can easily push the iOS application to TestFlight by executing the command fastlane ios staging. This command builds, signs, and uploads your application to TestFlight.
For Android app deployment to the Play Store, execute the command fastlane android staging. This command automatically builds, signs, and deploys your Android application to the Play Store.
Once the API key is created, the API key JSON file will be downloaded to your computer.
In the Fastfile for your Android project, create a new function called setup_ci, as shown below:
Create a .env file in the fastlane folder and add STORE_PASSWORD, KEY_ALIAS, and KEY_PASSWORD variables with actual values as shown below:
Add the JSON access key to the Appfile in the fastlane folder as shown below:
Finally, update the staging function with the code snippet below:
When you execute the command fastlane playstore build_flavor:"staging" build_type:"release", fastlane will build and deploy the application to the Play Store.
You can deploy the app's build by manually running the fastlane commands, but this becomes tiresome when you make several releases within a short period. Instead, automate the process of running the build and deploy command. In addition, you can utilize the free GitLab CI/CD tool.
Create a new file named gitlab-ci.yml in the root iOS project directory and add the code snippet below:
Create a new file named gitlab-ci.yml in the root Android project directory and add the code snippet below:
When you push the project to GitLab, the GitLab CI tool will automatically run the script above, building, signing, and deploying the applications to TestFlight and the Play Store.
You've successfully deployed your application to TestFlight and the Play Store using fastlane and GitLab CI/CD. For any subsequent code push to the GitLab repository, your code will be automatically built and deployed to TestFlight, and the Play Store without any manual intervention required.
As you saw in this tutorial, fastlane can save you time and effort in beta testing your projects. Fastlane can be used alongside any CI/CD pipelines or as a standalone tool to automate the build and release of mobile applications.
To further optimize your mobile app development, try Unflow. It offers a visual editor for no-code, cross-platform development, and the Unflow Mirror app allows you to test your app design in real-time before deploying. In addition, Unflow updates all your expected flows so you can focus on building your projects. Read the documentation to see how Unflow can help you.