Quick guide on fastlane for iOS development
Learn to automate steps like running tests, code signing, build uploading and more.
13 Feb 2023 · 8 min read
When it comes to publishing an iOS app to the App Store, we need to perform multiple steps like running tests, code signing, archiving and uploading the build, creating screenshots, setting metadata and more. These steps can be very time consuming, especially when publishing on a regular basis.
fastlane is an open source tool that allows you to automate all of these steps and more. At its core, fastlane is a set of Ruby scripts that wrap xcodebuild, the App Store Connect API and more and allow us iOS developers to use commands to automate iOS builds.
Let's see how to do that.
source "https://rubygems.org"gem "fastlane"
Defining and running lanes
From the created files, the most interesting one is fastlane/Fastfile which is a Ruby configuration file we can use to add so called lanes for signing, testing, building, deploying etc.
Each lane is a group of actions. A lane definition looks as follows:
lane :my_lane do# fastlane actionsend
Which we can run with the following command:
bundle exec fastlane [lane]
Using fastlane actions
fastlane provides predefined actions for different purposes like running tests, taking screenshots, deploying the app etc. We can combine multiple actions to build a lane, for example:
lane :beta dosync_code_signing(type: "appstore")build_app(scheme: "ExampleApp")upload_to_testflightslack(message: "New beta build available.")end
We'll look at some available actions in more detail later in this article.
Authenticating with Apple services
Several fastlane actions communicate with Apple services that need authentication.
fastlane has historically used Apple IDs with username and password for authentication. Since the announcement of the App Store Connect API at WWDC18, the official and recommended way for authentication is by using the App Store Connect API key. Checkout this guide to learn more on how to generate an API key.
fastlane provides the app_store_connect_api_key action which we can use to generate the key we need for JWT authorization:
api_key = app_store_connect_api_key(key_id: "79NWH6F376",issuer_id: "69a6de73-ea56-47e3-e053-5b8c7c11a4d1",key_filepath: "./AuthKey_79NWH6F376.p8")
Sync certificates and provisioning profiles across the team
For many fastlane actions like deploying to the App Store, a beta testing service or installing the app on a device, a valid certificate and provisioning profile is required.
fastlane provides an action called match which creates all required certificates & provisioning profiles and stores them in a separate Git repository.
After successfully setting that up, we can use the match action on our Fastfile to automatically fetch the latest provisioning profiles and certificates:
match(api_key: api_key, type: "appstore")
Now, let's look at some actions fastlane provides.
For example, to run tests, we can use fastlane's run_tests action as follows:
lane :tests dorun_tests(workspace: "ExampleApp.xcworkspace",devices: ["iPhone 13 Pro"],scheme: "ExampleAppTests")end
There are various other parameters we can provide such as reset_simulator to automatically erase the simulator before running the iOS application or slack_url to posts results in Slack. To see a full list of parameters, run fastlane action run_tests.
Taking screenshots and uploading screenshots to App Store Connect
To automate the process of taking screenshots with fastlane, we can use its snapshot action which uses the capabilities of UI tests to create the desired application state.
Check out this step-by-step guide on how to setup fastlane's snapshot action.
After creating screenshots, we can use fastlane deliver to upload them to App Store Connect.
Building the application
To build the iOS application, we can use fastlane's build_app action:
It creates a ExampleApp.ipa in the current directory.
Uploading to Testflight
Since Testflight is part of App Store Connect, uploading a beta build is just another fastlane action:
fastlane also supports beta deployments to Firebase, HockeyApp and TestFairy.
Uploading to the App Store
Uploading the build to the App Store is also just another fastlane action:
I hope, this guide helped you to get an overview on fastlane which is a powerful tool for automating the development and deployment process for iOS apps. To dive deeper into fastlane, check out their official documentation.
Like to support my work?
Articles with related topics
How to automate taking screenshots with fastlane for iOS
Learn how to setup fastlane's snapshot action.
24 Jul 2023 · 2 min read
Continuous integration and delivery for iOS
Learn CI/CD concepts and possible workflows for iOS applications.
29 May 2023 · 5 min read
Authorizing App Store Connect API requests
Learn how to create and use a JSON Web Token when communicating with the App Store Connect API.
26 Dec 2022 · 4 min read
Developer guide on iOS code signing & provisioning
Understanding provisioning profiles & certificates.
12 Dec 2022 · 4 min read
Developer guide on the App Store Connect API
Learn how to automate App Store Connect.
28 Nov 2022 · 7 min read
Latest articles and tips
Introduction to Kotlin for Swift developers - protocols, extensions & generics
Learn how protocols, extensions and generics in Kotlin work from a Swift developer's point of view.
02 Dec 2023 · 8 min read
How to store images in SwiftData
Learn how to efficiently store larger amounts of data in SwiftData.
27 Nov 2023 · 1 min read
How to migrate to a new schema with SwiftData in iOS
Learn how to create a migration plan in SwiftData.
20 Nov 2023 · 4 min read
How to implemet a free trial period for StoreKit 2 subscriptions in iOS
Learn how to implement StoreKit 2 introductory offers.
13 Nov 2023 · 2 min read
How to migrate from Localizable.strings to String Catalogs
Learn how to migrate your iOS project to .xcstrings for localization.
06 Nov 2023 · 1 min read