TExcelTAD
Home › Mobile Build Pipelines: Gradle/AGP, Xcode, SwiftPM & Fastlane › Fastlane: Lanes, Actions & the Fastfile

Fastlane: Lanes, Actions & the Fastfile

By now a release involves a dozen steps per platform: compute the version, build, sign, verify, upload the binary, upload metadata and screenshots, notify the team. Fastlane is an open-source Ruby tool that turns those steps into named, versioned lanes that run the same on a laptop and on CI. It's the most widely used release automation for mobile, for native, Flutter and React Native apps alike.

Note: Fastlane isn't in this course's container (it's a Ruby gem tree with native extensions), so Fastlane files and sessions here are non-run, written against the documented actions. The runnable example models how lanes execute.

Installing it reproducibly

Fastlane is a Ruby gem. Pin it like any other build tool, with Bundler and a committed lockfile:

ruby
# Gemfile
source "https://rubygems.org"
gem "fastlane", "~> 2.228"
bash
bundle install                 # writes Gemfile.lock: commit it
bundle exec fastlane lanes     # always run through bundle exec, so the pinned version is used

The files

text
fastlane/
├── Appfile        app identity: package name / bundle ID, team, credentials file locations
├── Fastfile       the lanes
├── Matchfile      settings for match (iOS signing)
├── Pluginfile     optional plugins (another Gemfile, managed by fastlane)
└── metadata/      store listing texts and screenshots (supply / deliver)
ruby
# fastlane/Appfile
app_identifier("com.example.hello")          # iOS bundle ID
team_id("ABCDE12345")                         # Apple developer team
package_name("com.example.hello")             # Android applicationId
json_key_file(ENV["PLAY_JSON_KEY_PATH"])      # Google Play service account (never committed)

Lanes and actions

A lane is a Ruby block with a name; an action is one step (gradle, build_app, upload_to_play_store, slack, …, several hundred built in). Lanes are grouped per platform:

ruby
# fastlane/Fastfile
default_platform(:android)

platform :android do
  desc "Unit tests for the free flavor"
  lane :test do
    gradle(task: "test", flavor: "free", build_type: "Debug")
  end

  desc "Signed bundle to the Play internal track"
  lane :internal do
    gradle(task: "bundle", flavor: "free", build_type: "Release",
           properties: { "buildNumber" => ENV.fetch("BUILD_NUMBER") })
    upload_to_play_store(track: "internal", aab: lane_context[SharedValues::GRADLE_AAB_OUTPUT_PATH])
  end
end

platform :ios do
  lane :beta do
    setup_ci
    match(type: "appstore", readonly: true)
    build_app(scheme: "Hello", export_method: "app-store")
    upload_to_testflight(skip_waiting_for_build_processing: true)
  end
end

error do |lane, exception|
  # runs when any lane fails, e.g. post to a chat channel
  UI.error("#{lane} failed: #{exception.message}")
end
bash
bundle exec fastlane android internal       # platform + lane
bundle exec fastlane ios beta
bundle exec fastlane lanes                  # list lanes with their descriptions
bundle exec fastlane action gradle          # documentation of one action and its parameters

Many actions have short aliases from Fastlane's history as separate tools: build_app = gym, upload_to_testflight = pilot, upload_to_play_store = supply, upload_to_app_store = deliver, sync_code_signing = match, capture_screenshots = snapshot. You'll see both spellings in real Fastfiles.

Passing values between steps

Actions write their results into lane_context (SharedValues::GRADLE_AAB_OUTPUT_PATH, IPA_OUTPUT_PATH, DSYM_OUTPUT_PATH, …), and later actions use them as defaults. That's why upload_to_testflight usually needs no path: build_app already put the .ipa there.

How a lane runs: a model

Fastlane runs before_all, then the lane's actions in order, then after_all; if an action raises, it stops and runs error instead. A small Python model of that control flow and of lane_context:

python · try it yourself
class LaneFailed(Exception):
    pass

context = {}

def gradle(task, flavor, build_type):
    path = f"app/build/outputs/bundle/{flavor}{build_type}/app-{flavor}-{build_type.lower()}.aab"
    context["GRADLE_AAB_OUTPUT_PATH"] = path
    return f"./gradlew {task}{flavor.capitalize()}{build_type}"

def upload_to_play_store(track, aab=None, json_key=None):
    aab = aab or context.get("GRADLE_AAB_OUTPUT_PATH")
    if not json_key:
        raise LaneFailed("No JSON key file given (json_key / json_key_data)")
    return f"upload {aab} to track '{track}'"

def run_lane(name, steps):
    print(f"[before_all] {name}")
    try:
        for step in steps:
            print("  ", step())
        print(f"[after_all] {name} succeeded")
    except LaneFailed as e:
        print(f"[error] {name} failed: {e}")

run_lane("android internal", [
    lambda: gradle("bundle", "free", "Release"),
    lambda: upload_to_play_store("internal", json_key="play.json"),
])
run_lane("android internal (no key)", [
    lambda: gradle("bundle", "free", "Release"),
    lambda: upload_to_play_store("internal"),
])
[before_all] android internal
   ./gradlew bundleFreeRelease
   upload app/build/outputs/bundle/freeRelease/app-free-release.aab to track 'internal'
[after_all] android internal succeeded
[before_all] android internal (no key)
   ./gradlew bundleFreeRelease
[error] android internal (no key) failed: No JSON key file given (json_key / json_key_data)

Where Fastlane fits

Fastlane doesn't replace Gradle, xcodebuild or your CI system; it orchestrates them. A healthy split:

Layer Owns
Gradle / Xcode how to build: variants, signing configs, settings
Fastlane the release procedure: which variant, version numbers, signing assets, upload, store metadata
CI (GitHub Actions…) when and where: triggers, machines, secrets, approvals

Keep lanes thin: if a lane contains 50 lines of Ruby computing version codes, move that logic into the build (as in versionCode & versionName Strategy) so a plain ./gradlew build gives the same answer.

Test yourself

Why run Fastlane as bundle exec fastlane …?

  1. It's faster
  2. Bundler uses the Fastlane version pinned in Gemfile.lock, so laptops and CI run the same version
  3. Fastlane requires root otherwise
  4. To enable plugins only

Which action is the same as gym?

  1. upload_to_testflight
  2. build_app
  3. sync_code_signing
  4. upload_to_play_store

How does upload_to_play_store find the bundle built earlier in the lane without a path argument?

  1. It searches the disk
  2. The gradle action stored the path in lane_context (GRADLE_AAB_OUTPUT_PATH)
  3. It rebuilds the app
  4. From the Appfile