October 10, 2022

Automate TestFlight and Play Store Builds Using Fastlane

Automate TestFlight and Play Store Builds Using Fastlane

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.

Why Do You Need Continuous Deployments of Beta Builds?

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:

  • Automated screenshot capturing: Fastlane can automate capturing screenshots required by the App Store, TestFlight, and Play Store while publishing your applications.
  • Beta build distribution: Fastlane works with more than fifteen beta testing tools, including TestFlight, Firebase App Distribution, and Microsoft's App Center. You can distribute your beta builds to testers quickly, regardless of your platform.
  • Automated code signing: Fastlane automatically builds a signed IPA file from your iOS project and a signed APK file from your Android project for easy distribution to the App Store and Play Store.

Automating Beta Builds with Fastlane

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.

Prerequisites

For this tutorial, you'll need to install the following tools:

Installing Fastlane

You have two options for installing fastlane:

  • With Homebrew, executing the command brew install fastlane
  • With RubyGems, executing the command gem install 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:

  • match can be used to share signing certificates and keys across development teams. When it's used for the first time, a private repository for certificate and profile storage is required, with a passphrase for encryption and a description of the files stored in the private repository.
  • scan allows you to run tests on a simulator or connected device.
  • gym allows you to build an IPA or APK file with the provided configurations.
  • pilot is a service that makes it easier to distribute and manage builds on TestFlight. It's a great way to publish your app to early access and beta testers before you publish it to the App Store. In addition, it manages deployments to TestFlight and Firebase App Distribution.

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:

  • Setting up an API key for App Store Connect and an access key for the Play Store allows fastlane to perform operations on TestFlight and the Play Store that require user authentication.
  • Setting up continuous integration creates a temporary keychain for the continuous integration environment.
  • Creating and syncing provisioning profiles maintains these profiles across your development teams.
  • Updating code signing settings ensure that the code signing identifiers match bundle identifiers and profile names.
  • Incrementing the build number ensures that the build version is automatically updated on each build.

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.

iOS Platform

  1. Visit App Store Connect and log in with your Apple account.
  2. On the App Store Connect homepage, select users and access as shown below:
App Store Connect homepage
  1. Select the keys tab shown below on the users and Access page:
Users and Access keys tab
  1. Generate a new access key by clicking the plus button, as shown below:
Generating new access key
  1. Once you create a new access key, take Note of the issuer and key ID, then download the .p8 API key file as shown below:
Downloading access key

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:

Setting Up the CI Script

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:

D7D2B1

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.

Android Platform

  1. Navigate to the Google Play Console and log in with your Google account.
  2. On the Google Play Console homepage, select the setup, then API Access menus as shown below:
Google Play Console homepage
  1. Click on the View API keys link on the API access page. The link will direct you to the Google cloud console API credentials page. Create a new service account by clicking the Create credentials button below:
Google Play Console API Access
  1. Click on the created service account and select the Keys tab. Click on the Add key button and select the JSON key type, as shown below:
Google Play Console Service Account

Once the API key is created, the API key JSON file will be downloaded to your computer.

  1. Navigate back to the Play Store console and select the setup menu. The service account you just created is listed under API keys, as shown below:
Google Play console API keys

Setting Up the CI Script

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.

Automating Builds with GitLab CI

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.

iOS Platform

Create a new file named gitlab-ci.yml in the root iOS project directory and add the code snippet below:

Android Platform

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.

GitLab CI pipeline

Conclusion

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.

Paul Odhiambo

What are you waiting for?

Make apps smarter,
with Unflow

Thanks for your interest!
Something's gone wrong while submitting the form