fastlane Tutorial: Getting Started

In this fastlane tutorial, you’ll learn how to provision, screenshot, build and upload an app to the App Store using fastlane. By Lyndsey Scott.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Supporting Multiple Targets

See that Multiple_Targets folder in mZone_Sample_Project? Open Multiple_Targets/mZone-2/mZone Poker.xcodeproj in Xcode. This project should look a lot like the first except with two new targets: mZone Poker Pro and mZone Poker Pro UITest.

To switch between running mZone Poker and mZone Poker Pro, click the button immediately to the right of the Run and Stop buttons, then select the scheme of whichever app you wish to run.

Select the scheme of the app you’d like to run.

switch schemes

Build and run mZone Poker Pro. It’s almost identical to mZone Poker, but returns more detailed suggestions.

                            mZone Poker                                               mZone Poker Pro

mZone Pro

On the General page, set mZone Poker’s bundle identifier to the one you created earlier.

Then, set a tentative new bundle ID for mZone Poker Pro; for example:

com.mZone-Poker-Pro.[Insert your email address minus “@”  and “.”]

Now, return to Finder. Copy and paste Gemfile and the fastlane directory from mZone into mZone-2. Then, drag mZone-2/fastlane/SnapshotHelper.swift into the new project. When the Choose options for adding these files window appears, select:

  • Copy items if needed.
  • Create groups.
  • mZone Poker UITests.
  • mZone Poker Pro UITests.

Then, click Finish.

Add snapshots helper file

Setting Up Multiple Environments

Environment (.env) files hold configuration settings that an app may access variably during its execution. For this project, you’ll create two environments — one for each app target.

Open up your favorite text editor, disable smart quotes and enter:

SCHEME = "mZone Poker"
TEST_SCHEME = "mZone Poker UITests"
BUNDLE_IDENTIFIER = "[[YOUR UNIQUE BUNDLE IDENTIFIER]]"
APP_NAME = "[[YOUR UNIQUE APP NAME]]"

This environment holds mZone Poker’s environment scheme, test scheme, bundle identifier and app name. Replace [[YOUR UNIQUE BUNDLE IDENTIFIER]] with mZone Poker’s bundle ID and [[YOUR UNIQUE APP NAME]] with the app’s unique name.

Save the file to mZone-2/fastlane as .env.mZone_Poker (with no file suffix). Since .env variables are hidden files by default, if you can’t see .env.mZone_Poker in Finder, press Shift-Command-. while in Finder to make your hidden files visible.

Similarly, create a second file containing:

SCHEME = "mZone Poker Pro"
TEST_SCHEME = "mZone Poker Pro UITests"
BUNDLE_IDENTIFIER = "[[YOUR UNIQUE BUNDLE IDENTIFIER]]"
APP_NAME = "[[YOUR UNIQUE APP NAME]]"

Replace [[YOUR UNIQUE BUNDLE IDENTIFIER]] mZone Poker Pro’s current bundle ID. Then create a tentative unique app name, ex.:

mZone Poker Pro [Insert your email address minus “@”  and “.”]

Remember, app names can’t be longer than 30 characters, so truncate as necessary.

Save the file as .env.mZone_Poker_Pro in the same directory.

Now, you’ll be able to use variables to access the current settings as you switch between schemes.

For starters, open Appfile, uncomment the app_identifier line and replace:

app_identifier("[[APP_IDENTIFIER]]")

With:

app_identifier(ENV['BUNDLE_IDENTIFIER'])

“ENV[‘BUNDLE_IDENTIFIER’]” tells fastlane to grab the bundle ID from whichever you set as the current environment.

In Deliverfile, replace the .ipa line with:

ipa("./fastlane/builds/#{ENV['APP_NAME']}.ipa")

In Gymfile, replace the scheme line with:

scheme(ENV['SCHEME'])

In Snapfile, replace the scheme and output_directory lines with:

scheme(ENV['TEST_SCHEME'])

And:

output_directory "./fastlane/screenshots/#{ENV['SCHEME']}"

Respectively.

Lastly, return to the new metadata directory. Select all of its contents and place them in a new folder named mZone Poker.

duplicate metadata

Then, duplicate that folder and name that duplicate mZone Poker Pro.

duplicate metadata

Change the contents of mZone Poker Pro/en-US/name.txt and mZone Poker Pro/fr-FR/name.txt to the new app’s name. Keep all the other metadata the same for now for simplicity’s sake.

Now, to run all the commands for the second target, you can enter:

bundle exec fastlane do_everything --env mZone_Poker_Pro

This will run do_everything using .env.mZone_Poker_Pro’s variables.

But what if you want to run all the commands for all the targets at once?

Open Fastfile. Below do_everything, add:

  def execute_for_all_envs
    # 1
    schemeList = Dir.glob(".env.*”)
    # 2
    schemeList.each do |file|
      # 3
      Dotenv.overload(file)
      # 4
      yield
    end
  end

This method:

  1. Puts the current directory’s .env. files into an array.
  2. Loops through each .env file.
  3. Overloads ENV with the current .env file.
  4. Executes the block following execute_for_all_envs invocation.

Then, to call execute_for_all_envs from do_everything, replace the content of do_everything with:

    if ENV['SCHEME'] != nil
      create_app
      screenshot
      build
      upload
    else
      execute_for_all_envs{ do_everything }
    end

Now, if you don’t specify an environment in the command line, ENV[‘SCHEME’] == nil and thus execute_for_all_envs runs. execute_for_all_envs sets ENV to the first environment, returns to the calling block, which then re-runs do_everything.

After do_everything does everything for that environment, the loop in execute_for_all_envs continues and returns the next environment, and so on.

That’s it!

Now you can run:

bundle exec fastlane do_everything

To have fastlane do all the heavy lifting for both targets while you sit back and relax.

fastlane tutorial

Caution: It might feel weird having so much time on your hands compared to your pre-fastlane existence, but trust us, you’ll get used to it. :]

Where to Go From Here?

Check out mZone-Final and mZone-Final-2 using the Download Materials button at the top or bottom of the tutorial to see how the final projects stack up compared to yours.

fastlane also supports TestFlight submission and a ton of integrations. You can customize your lanes to provide real-time feedback on Slack, interact with Jira boards, etc. Visit the the official fastlane website to learn more.

Questions or comments? Feel free to join the forum discussion below!