Using Fastlane to simplify library release

Photo by SpaceX / Unsplash

Remember when I made a post about Publishing iOS framework made easy? I will try to further simplify the whole process by adding Fastlane to the mix. For our testing ground, we will use an old library that I created - ReusableViewExtensions.

Initial setup

To start we need to install a toll named Bundler. To do that open terminal and enter the command:

gem install bundler

When Bundler is installed we can start setting up Fastlane.

Creating Gemfile

Open the project folder, and in the rood directory create Gemfile, I prefer to do it from the terminal by running touch Gemfile && atom Gemfile, this way it is created and immediately open in the editor:)

Inside Gemfile we will add all the Gems that we will use:

source "https://rubygems.org"

gem "fastlane"
gem "cocoapods"

Save the file and in the terminal run:
bundle install

After all gems are installed, we are ready to initialize Fastlane!

Init Fastlane

To use installed gems, we need to prepend all commands with bundle exec.

Open terminal in the project root folder, where Gemfile is located and run
bundle exec fastlane init. When asked select 4 as answer:

After setup is complete, there will be a new folder in the project - fastlane, it contains all Fastlane configuration files we need to run our lanes.

Create first lane - testing

Navigate to the fastlane folder and create a new file named Fastlane_config - it will contain all variables needed in Fastlane lanes. Open the newly created file and fill it with the necessary info:

platform :ios do
  class Config
    WORKSPACE = "Example/ReusableViewExtensions.xcworkspace" #1
    TEST_SCHEME = "ReusableViewExtensions-Example" #2
    BUILD_SCHEME = "ReusableViewExtensions" #3
    TEST_DEVICES = ["iPhone 6s", "iPad Air"] #4
    PLATFORMS = ["iOS"] #5
    ARCHIVE_PATH = "archive" #6
    PODSPEC_FILE = "ReusableViewExtensions.podspec" #7
  end
end
  1. Path to workspace
  2. Name of test scheme
  3. Name of framework scheme to build
  4. Array with devices to run tests on
  5. The platform for which we will build xcframework
  6. Folder for artifacts
  7. Podspec needed to publish a new pod version

For now, we will need only the first two. The following five will be used in the next lanes:)

We can save and close Fastlane_config. Now open Fastfile. Before we start creating lanes, we need to inform Fastlane about our config file, so modify file content:

default_platform(:ios)

import("./Fastlane_config")

Now we are ready for testing! After the import statement, in a new line, add this definition:

platform :ios do
  desc "Run tests"
  lane :test do
    run_tests(
      workspace: Config::WORKSPACE,
      devices: Config::TEST_DEVICES,
      scheme: Config::TEST_SCHEME
    )
  end
end

As you can see, running tests with Fastlane is a cakewalk! Now we can use our lane by running bundle exec fastlane test.

Creating xcframework

To create xcframework we will need to add the Fastlane plugin. To do that, open the terminal in the projects root folder and run bundle exec fastlane add_plugin create_xcframework. When asked to modify the Gemfile, say yes.

Now we can add a new lane to Fastfile:

default_platform(:ios)

import("./Fastlane_config")

platform :ios do
  desc "Run tests"
  lane :test do
    run_tests(......) #from last step
  end

  desc "Builds xcframework for distribution"
  lane :build_xcframework do
    test
    create_xcframework(
      workspace: Config::WORKSPACE,
      scheme: Config::BUILD_SCHEME,
      product_name: Config::BUILD_SCHEME,
      destinations: Config::PLATFORMS,
      xcframework_output_directory: Config::ARCHIVE_PATH
    )
  end
end

And building our library for distribution is done in a couple of lines of code! Every time you run bundle exec fastlane ios build_xcframework, all necessary files and created xcframework will be created in the archive folder! Isn't this nice? No bash scripts, manual execution, no room for error or typo :)

Cocoapods

You guessed it, Fastlane can distribute pod to trunk!

We will modify the Fastfile once more:

default_platform(:ios)

import("./Fastlane_config")

platform :ios do
  desc "Run tests"
  lane :test do
    run_tests(......) #from last step
  end

  desc "Builds xcframework for distribution"
  lane :build_xcframework do
    test
    create_xcframework(......) #from last step
  end

  desc "Push new version to pod trunk"
  lane :release_pod do
    pod_push(path: Config::PODSPEC_FILE)
  end
end

Yes, it is that easy, one line! With bundle exec fastlane release_pod the new pod version is pushed to the trunk.

Final words

Fastlane is a very robust and powerful tool for iOS development and release. It can create frameworks, work with Cocoapods, upload apps to Testflight, automate screenshots, run tests, and many, many more. If you are not using it already I highly recommend giving it a try! Happy automating!

Artur Gruchała

Artur Gruchała

I started learning iOS development when Swift was introduced. Since then I've tried Xamarin, Flutter, and React Native. Nothing is better than native code:)
Poland