Fastlane: iOS Automation on Cruise Control
Working on an iOS application can be tedious, especially when you are wasting countless hours doing the most boring tasks: taking screenshots, signing code, and jumping through similar hoops to get your app to the users’ devices.
In this article, Toptal Freelance iOS Developer Francisco Reynolds walks you through the ultimate release automation tool for your iOS app and shows how it can save you from all of the tedious tasks.
Working on an iOS application can be tedious, especially when you are wasting countless hours doing the most boring tasks: taking screenshots, signing code, and jumping through similar hoops to get your app to the users’ devices.
In this article, Toptal Freelance iOS Developer Francisco Reynolds walks you through the ultimate release automation tool for your iOS app and shows how it can save you from all of the tedious tasks.
Francisco is a technical lead with extensive experience in iOS, Node.js, and web project development.
Expertise
PREVIOUSLY AT
Making software for iOS is more than just about writing Objective-C or Swift code: It involves knowing how UIKit works, how certificates are made, how to publish your app to the store, how to deal with app reviews, and more. If you find this eternal task list tiring, some automation in your workflow is exactly what you need.
Meet Fastlane, the tool designed to save you minutes or even hours in each deploy.
Fastlane helps you automate the process of sending beta builds to Crashlytics, TestFlight, the App Store, and much more.
In this article, you will learn how you can use Fastlane to automate your iOS app deployment workflow. Although we will focus on the iOS aspect of this tool, Fastlane works equally well for Android development automation.
Getting Started with Fastlane
There are several installation methods that you can choose between, depending on what you find the easiest. If you have Homebrew installed and configured, installing Fastlane takes just one command:
brew cask install fastlane
Otherwise, Fastlane being a Ruby-based tool, gem
is always an option:
sudo gem install fastlane -NV
Or, you can always download Fastlane from the official website.
Once you have installed Fastlane, run fastlane init
in your project directory and follow the instructions.
Deployment Automation
With Fastlane installed, you can now write Ruby scripts to indicate step by step what you need to do to get your deployment where you want it. This is done using actions, tools, and lanes.
Fastlane Tools, Actions, and Plugins
Fastlane works by having tools at its core that help you build your app. On top of the build tools, there are over 170 built-in integrations with services like Crashlytics, HockeyApp, TestFlight, and more plugins that third parties make and publish so you can connect to other services and more.
At its core, you get the main tools that let you do a variety of tasks: From taking automated screenshots to framing them and uploading them to the store, it lets you run all your tests automatically before building the app.
Actions are the core tools that Fastlane has, and plugins are a way to add third-party actions to your Fastlane setup.
Lanes
Fastlane uses lanes that essentially group a list of actions or plugins sequentially so that you can achieve the same result every time.
When running a lane, each action will export an environment variable, and the following actions will automatically get these variables to keep the process going. For example, using the gym
action will result in the path of the .ipa
file being generated, and using crashlytics
will access this .ipa
location to upload this file to its beta service distribution system.
Using Fastlane with iOS
You are starting with a new Toptal client who wants a very simple app. The design is already done, and the app can be coded in a few weeks. The next step is to start coding it, and during your the iteration process, you are going to send a beta app every a couple of weeks to the client so that they can see your progress and give you feedback on how things are working.
Every time you send a beta version to the client, you are going to go through a basic checklist: signing the app, uploading it to a beta service, adding a new version to it, etc. Add to this the things you have to do once (every year): signing certificates, signing push notification certificates (if needed), and more.
With Fastlane, all of these can now happen automatically!
Sending to Crashlytics Beta
For example, you need to create signing certificates and provisioning profiles to share your app via Crashlytics Beta.
Fastlane has an action called match
that, once you set it up, does the following:
- Create distribution signing certificates.
- Create an ad-hoc provisioning profile (attached to the certificate from #1).
- Save the certificate and the profile from #1 and #2 to a Git repository, encrypted using OpenSSL.
The setup is simple.
First, run fastlane match init
to get started and, once you set your Git repo, just call fastlane match adhoc
to get an ad-hoc provisioning profile or run fastlane match appstore
to get an App Store certificate.
Advantages of using match
are that you can sync the certificates very easily between computers—and between teammates—without having to revoke the certificates anymore, and the speed that you gain. Just running fastlane match appstore
gives you a certificate in a couple of seconds.
Well, now we have the certificates and provisioning we need, let’s make a release to Crashlytics.
A typical workflow for submitting your app to Fabric’s Crashlytics includes the following steps:
- Create the distribution signing certificates.
- Create an ad-hoc provisioning profile (attached to the certificate from #1).
- Set your app to use the provisioning profile from #2.
- Archive your app.
- Open Fabric and follow the steps to send the app to beta testers.
Fastlane replaces the chore with this:
platform :ios do
lane :beta do
match(type: “adhoc”)
gym
crashlytics
end
end
Now, running fastlane beta
from your terminal will launch this script.
This will first call match
, an action that handles creating and updating signing certificates and provisioning profiles, then call gym
, an action that builds and packages the app for you, using the same certificates and profiles created before, and finally, crashlytics
, which is going to take this signed .ipa
and upload it to the Crashlytics service.
All of those steps get called, always, in the same order, making the resulting file more predictable and reducing errors.
MacBook-Pro:TestProject user$ fastlane beta
+------+-------------------------------------+-------------+
| fastlane summary |
+------+-------------------------------------+-------------+
| Step | Action | Time (in s) |
+------+-------------------------------------+-------------+
| 1 | Verifying required fastlane version | 0 |
| 2 | match | 57 |
| 3 | gym | 592 |
| 4 | crashlytics | 195 |
+------+-------------------------------------+-------------+
[20:25:13]: fastlane.tools just saved you 14 minutes! 🎉
So that was a basic setup for how to run Fastlane to send your app to Crashlytics. What else can we automate?
Incrementing Build Versions
We can, for example, set that each time we are going to send a beta, we want to increase the build version number.
This can be achieved using an action called increment_build_number
that can be called from a terminal by fastlane run increment_build_number
or used inline in our beta lane:
platform :ios do
lane :beta do
increment_build_number
match(type: “adhoc”)
gym
crashlytics
end
end
Congratulations! You now have an automated deploy script that also increments your build version number. The only caveat is that, once you call this lane, you are going to have files changed in your project (or, at least, the ones that incorporate the build number).
In case you were planning to commit that change to the repository anyway, there is an action for that: commit_version_bump
. This action commits the files with the new version number.
platform :ios do
lane :beta do
increment_build_number
match(type: "adhoc")
gym
crashlytics
commit_version_bump(
xcodeproj:"myProject.xcodeproj",
)
end
end
This will commit files with a commit message like this “Version Bump to 27”.
Crashlytics to TestFlight
Well, if you got this far, you can have an automated deploy to Crashlytics in a very short time that will save you countless hours in the long run. The best part about it is, if you wanted to send your app to TestFlight instead, all you would need to do is change crashlytics
to pilot
.
Or, you can create a separate lane for that:
platform :ios do
lane :beta-testflight do
increment_build_number
match(type: "adhoc")
gym
pilot
commit_version_bump(
xcodeproj:"myProject.xcodeproj",
)
end
end
iOS Automation Made Simple
Fastlane is simple and easy to use. But, it brings amazing value to your regular iOS development workflow by saving you from doing menial things and hours of your time that you would have ended up wasting.
I hope you will find this introduction to Fastlane useful. You can check out the official documentation to learn more about Fastlane, especially the list of actions if you are looking for some inspiration on what you could automate for your current and next iOS projects.
Understanding the basics
What is Fastlane?
Fastlane is a tool that automates the release and delivery workflows for your iOS and Android apps.
How do I install Fastlane?
Run “brew cask install fastlane” in a terminal or download the installer from https://download.fastlane.tools/
Francisco Reynolds
Buenos Aires, Argentina
Member since December 8, 2016
About the author
Francisco is a technical lead with extensive experience in iOS, Node.js, and web project development.
Expertise
PREVIOUSLY AT