How to Use Fastlane to Deploy iOS App Fast
By: Mahmud Ahsan
Tags: Programming, Software Development, iOS, Swift, iOS App Development

If you’re an iOS developer like me, you should use Fastlane to deploy your app fast. It saves time — a lot of time. Over many years I have been using Fastlane to deploy my apps and our company’s app. Today I will guide you on how to set up and use Fastlane to deploy an iOS app to Apple Store faster.

I assume you know, how to deploy an iOS app manually on the App Store.

Fastlane is founded by Felix Krause. The tool is open-sourced. You can use it to automate deployment for both iOS and Android applications in your own machine or using it with other web services for continuous integration. Here is the source code:

1. Install

If you use RubyGems (macOS/Linux/Windows), you can use the following way to install Fastlane on your machine.

sudo gem install fastlane -NV

I use macOS and Homebrew, so I use the Homebrew to install Fastlane on my machine.

brew install fastlane

If you get any installation issue related to new macOS like macOS Big Sur, sometimes uninstalling Homebrew and reinstalling it works. Run the following commands to uninstall and install Homebrew only if you couldn’t able to use Homebrew to install Fastlane.

2. Setup

First, enter your project directory in the terminal.

Screenshot created by the author

Then run the following command:

fastlane init

Screenshot created by the author

You see, there are 4 options that will be shown to proceed. If you choose among 1 to 3, the tasks are automated. You can also choose 4 to put configuration later.

Based on the selection, Fastlane can ask you to enter iTunes connect username and password. This will connect with iTunes and save that information.

3. Fastlane Configuration

After first initialization, Fastlane will create a directory ‘fastlane’ in your project. There you’ll see 2 files

Appfile Fastfile

Appfile contains the configuration related to iTunes Connect. Normally I open my VSCode editor to write my changes, but you can use any text editor.

Fastfile contains the configuration related to the app deployment.

3.1 Appfile

If you open the Appfile you will see now 2 actions are commented out:

Screenshot created by the author

Normally if you use the automated connection and select the appropriate app from your developer account you can see the data like this:

app_identifier("APP_ID") # The bundle identifier of your app apple_id("EMAIL_ADDRESS") # Your Apple email address itc_team_id("XXXXX") # App Store Connect Team ID team_id("XXXXX") # Developer Portal Team ID

Screenshot created by the author

If you choose a manual selection, please fill in the appropriate data for this file.

3.2 Fastfile

It holds the deployment-related task. Each task can holds multiple sub-tasks. According to Fastlane, the major task is called a lane. Simply to say:

A lane is just some configuration for deployment

If you open the Fastfile now you will see the following screen:

Screenshot created by the author

The configuration in both Appfile and Fastfile are written in Ruby programming language. But it’s not difficult to understand.

Fastlane has a very good documentation. So you can easily know how to use fastlane. But now I will share some common cookbook type solutions which I regularly use in my case.

4. How to Build and Upload in Test Flight

Open the Fastfile and write the following actions. In this case, I assume you don’t use CocoaPod.

Now in your terminal, if you write fastlane beta it will execute the beta lane.

Screenshot created by the author

So here in the configuration, we first defined a lane named beta which starts from do and completes with end. Within this we have written:

increment_build_number — increment the XCode project’s build number

Screenshot created by the author

build_app — Using the build_app action it will build and sign the app

upload_to_testflight — This will upload the new binary to App Store Connect for testing.

Also, there is a desc before the lane which simply for documentation purpose that what the lane is for.

5. Build and Upload in Test Flight With Cocoapod Based App

Normally, in all of my iOS apps, I use CocoaPod. But if a library supports Swift Package Manager I prefer the Apple one. Anyway, if you use CocoaPod in your XCode project, the same solution for the above scenario is like this:

In this case, you see, increment_build_number and build_app has some parameters that defined which XCode project is using and which XCode workspace and scheme is using.

6. Connect to Itunes Connect to Download Metadata

If you want to download metadata — screenshots, description, keywords, and other related things for an iOS App, just type the following command on your terminal.

fastlane deliver download_metadata

Screenshot created by the author

You see, it will download all the metadata within the fastlane directory.

7. Download Screenshots From iTunes Connect

If you want to download screenshots from your iTunes Connect account try this:

fastlane deliver download_screenshots --use_live_version true

# OR

fastlane deliver download_screenshots

If you are editing a version and download screenshots from that version, then skip the --use_live_version true part.

These commands will create a new directory inside your fastlane directory named ‘screenshots’ and will download all screenshots in there.

Screenshot captured by the author

8. How to Use and Upload Metadata Effectively

Let me share with you a scenario. Normally I use localized language in the app store. But every time when I release a new app, I don’t use a different language for the release not. I use the same English language-based release notes for all of my app store release notes.

Long ago, it was a tedious task. If you use 20 languages, for your app store metadata, you have to write 20 release notes for each language. But using Fastlane the task is easy and let me share the solution with you.

Screenshot created by the author

Create a directory named ‘default’ within the metadata directory Put release_notes.txt file there and write what you want to inform the user for the latest binary If there is any other release_notes.txt file in any other language, just delete them

default means if there is no localized information found, Fastlane will use the default localization for the following language.

So in my case, sometimes I only use different keywords, subtitles, and descriptions for different languages, but other information is the same. In that case, I just put the same information related metadata file within the default directory.

Now when I want to upload metadata only, I first defined a new lane in the Fastfile named meta

And just type in the terminal:

fastlane meta

deliver — action upload the data in the Itunes Connect. In this case, I am just uploading metadata only, but skipping the screenshots.

If you want to upload both metadata and screenshots set skip_screenshots: false

9. How to Build and Submit to the App Store for Review

If you want to build the app, upload metadata, and submit the app store for review, you can use the following template.

The actions are self-explanatory by the command names. Check the next template which is most important.

10. How to Create New App Version and Submit for Review

In this scenario, you want to:

Create a new app version You want to pass a version number from the terminal Submit the beta build from TestFlight to the app store for review

Normally what we do, we visit iTunes Connect and create a new iOS App version manually by clicking (+) button, right? But in this case, we will do everything automatically.

Here is the configuration:

Now Run the following commands in Terminal:

Build the app and upload to testflight, wait for the task to complete in the terminal.

fastlane beta

2. After testing the app, if you are satisfied now submit for review. Here you passed the app store version: v:18.1.1

fastlane submit_for_review v:18.1.1

Screenshot captured by the author

So let’s discuss the above configuration:

Line 4–6 — is used to capture any parameter passed in the command line. So before executing any lane, it will pass the key:value parameters as options. As we are using fastlane submit_for_review v:18.1.1 we want to capture version number 18.1.1 More info about passing parameters.

submit_for_review lane also adds some changes.

Line 25 — we add an |options| after do to capture the parameters from the command line

— we add an after to capture the parameters from the command line Line 28 — We add a if logic if options[:v] means, if the version number is provided only then execute further. So if you only run the commands fastlane submit_for_review v:VERSION_NUMBER it will work. if you skip the v:VERSION_NUMBER portion it will not work

— We add a logic means, if the version number is provided only then execute further. So if you only run the commands it will work. if you skip the portion it will not work Line 30 — We add the app_version: options[:v] to create the iTunes New App Version automatically and based on our version number passed in the command line. So in my case, it will be 18.1.1 More info about deliver

— We add the to create the iTunes New App Version automatically and based on our version number passed in the command line. So in my case, it will be More info about deliver Line 34 — As we want to use the last build we uploaded already via beta lane, we use skip_binary_upload: true which will use the last build submitted to the TestFlight. If you have a separate build for staging and production then you have to do it a bit differently.

Normally, in my case the build I submit to TestFlight is also used for production.

Screenshot captured by the author

If everything runs successfully, you will see a new version is created, added the last build, and submitted to the app store for review.