Environment setup and certification

Recently, I was researching how to do a good job on MOBILE TERMINAL CI-CD, and found a good tool, which is our protagonist todayfastlane. In fact, both Firim and Dandelion, the sites we use in our daily Beta testing, provide plugins for Fastlane: Dandelion, for exampleUpload apps using Fastlane. Now let’s learn how to use Fastlane on iOS clients.

Environment setup macOS

Xcode command line tools (macOS)

Install Xcode command-line tool xcode-select –install

Install the fastlane

The official website recommends using Bundler + Ruby (version2.5+). Ensure that the Version meets the Ruby version requirements. If you are not sure, run the ruby –version command to view the version

Bundler installation mode

Use Bundler and Gemfile to define our dependency on Fastlane. This will clearly define the version of Fastlane to use and its dependencies, and will also speed up Fastlane execution.

  1. Using the commandgem install bundlerTo install the Bundler
  2. Create a Gemfile at the root of our project with the following contents
source "https://rubygems.org" 

gem "fastlane"
Copy the code
  1. Run the command after creating the new onebundle updateAnd add./Gemfile and./ gemfile. lock to our version control
  2. Each time fastlane is usedbundle exec fastlane [lane]
  3. Add in CIbundle installAs a setup for the first build
  4. Upgrade fastlane usagebundle updateCan be

Install using Homebrew (macOS)

This way, we don’t need to install Ruby separately, Homebrew will install a Version of Fastlane Ruby for us, see details, we can choose the appropriate version of Intel CPU or Apple Silicon CPU according to your needs.

brew install fastlane
Copy the code

Ruby + RubyGems (macOS/Linux/Windows) not recommended

This approach is not recommended in your local environment, but you can still install Fastlane in your system Ruby environment. Because of file permissions, using Sudo often produces unwanted results that make managing the environment more difficult.

sudo gem install fastlane
Copy the code

Configure Fastlane for the project

CD to our project directory in Terminal

fastlane init
Copy the code

Note that if you want to create your first app on your App Store Connect account, you will need to set the developer name (company_name) with the PRODUCE_COMPANY_NAME environment variable:

PRODUCE_COMPANY_NAME="YOUR COMPANY NAME" fastlane init
Copy the code

See Create app Documentation for more descriptions of company_name.

Use Swift for Fastfile configuration (new feature being tested)

See Fastlane.Swift Docs for more information

fastlane init swift
Copy the code

Depending on the configuration you choose, there are different configuration files. If we choose to download metadata for an existing application, we get this directory structure

The most interesting file is Fastlane /Fastfile, which contains all the information you need to publish your application.

Setting environment Variables

Fastlane requires some environment variables to be set up to run properly. In particular, if our computer’s local setting is not set to UTF-8, there will be problems building and uploading builds. Bashrc, ~/.bash_profile, ~/.profile or ~/.zshrc may need to be modified on different systems. Add the following lines to the shell configuration file:

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
Copy the code

What features will Fastlane bring next?

Fastlane created all the required files for us. Now we can continue customizing Fastlane to generate screenshots, or automatically distribute new builds, and much more. Here are some examples:

  • Generate localized iOS screenshots for the App Store
  • IOS beta is automatically deployed
  • Automatically deploys the iOS App Store
  • Discover all: Fastlane Actions

Please note that if the automation you’re building requires a connection to An Apple server, such as code signing when building an app, or uploading an app to App Store Connect, etc., you’ll need to authenticate. To viewAuthentication for Apple servicesTo learn the best authentication methods to meet our specific use case.

Certification: Through the services provided by Apple

A series of Fastlane Actions calls to the Apple interface require security authentication. This presents some challenges, especially in CI/CD(continuous integration and continuous delivery). Then there are four ways to solve the problem.

Method 1

App Store Connect API Key (recommended) This is the official authentication scheme, but it does not yet support all fastlane features. See App Store Connect API for details.

Method 2

For actions that do not support the App Store Connect API, We needed to authenticate with our Apple ID, and thankfully Fastlane fully supports Apple’s two-factor verification (and 2-step verification (2SV)) for logging into our developer account. As shown in the picture below, a familiar taste.

Manual authentication (enter account and password)

After enabling two-factor authentication (or two-step authentication), you will be asked to verify your identity by entering a security code. If you have configured a trusted device for your account, the code will appear on the device. If you don’t have any devices configured but trust a phone number, the code will be sent to your phone.

The generated session will be stored in the ~/.fastlane/ Spaceship /[email]/cookie directory

usespaceauthStores manually authenticated sessions

Since your CI machine will not be able to prompt you for two-factor authentication or two-step authentication information, you can generate a login session for your Apple ID ahead of time by running the following command:

fastlane spaceauth -u [email protected]
Copy the code

The generated value must be stored in the “FASTLANE_SESSION” environment variable on the CI system. Instead of triggering a new login every time Fastlane communicates with the Apple API, this session will be reused.

It is recommended that you run ‘spaceauth’ on the same machine as CI, rather than running it locally on your machine — see the comments on session duration below.

An important note about the duration of the session

Sessions that are generated, stored, and reused as part of 2FA/2SV authentication or as part of * Spaceauth are subject to technical limitations imposed by Apple. That is:

  • Apple ID sessions are only valid within a specific locale, which means that if you use a different locale (such as a CI system) from the one where you created the session (such as a local machine), you may run into problems. It is recommended that you create sessions on the same machine that you will use them to make them last longer.

– Session validity can vary widely (from 1 day to 1 month, depending on factors such as geographic location where the session is used). This means you must generate new sessions at least once a month. You usually don’t know this until the build starts to fail.

Fastlane can’t do much better in this regard, because these are technical limitations on how to handle App Store Connect sessions.

Method 3: Use a special password for the program

If you want to upload a build from your CI machine to App Store Connect(action ‘upload_to_app_store’ and ‘deliver ‘) or TestFlight(action’ upload_to_testflight ‘, ‘Pilot’ or ‘TestFlight ‘), you can generate an application-specific password:

  1. Visit appleid.apple.com/account/man…
  2. Generate a new application-specific password
  3. Using environment variablesFASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORDProvide application-specific passwords

This will provide the application-specific password to the iTMSTransporter, which is the tool these operations use to perform the upload.

Note: If you do anything other than upload binaries, such as updating any metadata, such as setting up to publish notifications or distribute to testers, etc., application-specific passwords will not work. For these operations, you must use one of the other methods.

Method 4: Apple ID without 2FA

Apple announced that as of February 27th 2019, it would enforce 2-factor authentication on developer Apple IDs with the “Account Holder” role. Since then, they extended this rule to all roles, and then later throughout 2020 they slowly enforced all existing accounts to register 2FA. As of March 3rd 2021, no accounts without 2FA registered are able to login until they register a 2FA method, essentially breaking all “non-2FA compliant Apple IDs” that still existed. For this reason, when using fastlane in your CI, you will have to work your way with 2FA.

Refer to the official Fastlane website