Small goal: Build with Jenkins and automatically upload to the App Store.

Why do you chooseFastlane?

Fastlane is the easiest way to automate test deployment and release for iOS and Android applications. 🚀 It handles all the tedious tasks, such as generating screen shots, handling code signatures, and publishing applications.

Usage scenarios

  • Tests (both unit and integration tests) are executed at commit time.
  • Build and distribute internal tests, public beta builds.
  • Build the production version and upload it to ITC (including updating the configuration file, creating new screen shots, uploading the application and submitting it for review).

Tool set

Fastlane combines the following suite of tools, from managing certificates to unit testing, from compiling and packaging to uploading and publishing, all easily done from the command line. The suite supports integration with Jenkins and other third-party tools such as CocoaPods and XCTools, as well as the ability to define lanes to support different deployment goals.

  • Testing tools
    • Scan: Automatically runs a test tool that generates nice HTML reports.
  • Generate certificates and configure tools
    • Cert: Automatically creates an iOS code signing certificate (.cert file).
    • Sigh: Create, update, download and fix Provisioning Profiles for App Store, Ad Hoc, Development and enterprise Profiles.
    • Pem: automatically generates and updates the pushed configuration file.
  • Capture, upload, and trace the device frame
    • Deliver: Upload screenshots, metadata, and apps to iTunesConnect.
    • Snapshot: Use UI Test to take snapshots.
    • Frameit: Quickly put a screenshot of the app into the device frame.
  • Automated compilation tool
    • Gym: builds and packages iOS apps to generate signed IPA files.
  • App public test tool
    • Pilot: Manage TestFlight test users and upload binaries.
    • Firim: Manages fiRIM.

Two, preparation, very important yo

  1. To configure the current device environment, the latest Fastlane (2.75.1) requires ruby version 2.1 or higher, and the normal version of the low, but also requires 2.0 or higher. Because fastLane tools are written in Ruby. If the version is too early, you are advised to install RVM to upgrade Ruby.

    curl -L get.rvm.io | bash -s stable  
    
    source ~/.bashrc  
    source ~/.bash_profile 
    
    Check whether the installation is successful
    rvm -v
    Copy the code
  2. Set environment variables. Fastlane needs to set some environment variables to run properly. If the current locale is not set to UTF-8, this can cause build and upload problems. Add the following to the ~/.bashrc, ~/.bash_profile or ~/.zshrc files:

    export LC_ALL=en_US.UTF-8
    export LANG=en_US.UTF-8
    Copy the code
  3. Install the Xcode command line tool xcode-select –install. If the Xcode command line tool is already installed, xcode-select: error: Command line tools are already installed, use “Software Update” to install updates.

  4. Create your App ID, certificate, and create an App for testing in iTunes Connect.

  5. Install the fastlane.

  6. Create a test demo.

    1. Set its scheme to shared, otherwise fastlane init will fail.

    2. The signature configuration file is configured.

    3. Add icon to app.

    4. Change devices to iPad or iPhone.

Three, practice

fastlane init

  1. CD to the project directory, and for the Ruby installer, use the sudo Fastlane init command. Swift uses Fastlane Init Swift. The Swift installation is still in beta. See the Fastlane.swift documentation for more information.)

  2. What do you want to do with FastLane? Here we type in 3, and it’s automatically published to the Apple Store.

    You will be asked to enter the Apple ID of the Apple Development certificate, and if there are multiple teams, you will be asked to select Team.

  3. You’ll then be asked if you want to use FastLane to manage your app metadata.

    • Type y, and Fastlane downloads the existing metadata and screenshots. If we edit the.txt file from download, we will also upload it to iTunes Connect when we use Fastlane to upload the app to iTunes Connect.

    • You type in n, you don’t do anything, you can still use FastLane to upload your app to the App Store.

  4. If there is a FastLane release at the end, init succeeds.

  5. This will create an extra FastLane folder in the project directory.

    If the Deliverfile, ScreenShots, and Metadata directories are not created, run Deliver Init to create them.

inDeliverfileFile, addforce trueOr it will be uploaded toiTunes connectWill pop up onePreview.htmlA web page.

Use the Gemfile

  1. Touch a Gemfile file in the project root directory and add the following

    source "https://rubygems.org"
    gem "fastlane"
    Copy the code
  2. Run the following command:

    # installation bundler
    sudo gem install bundler
    
    Update the bundle to generate a version controlled gemfile. lock file
    [sudo] bundle update
    Copy the code
  3. Run the bundle exec fastlane [lane_name] command to run the lane_name script. Lane_name here is the name of the script, which we can understand as the function name. If we just execute the bundle exec fastlane command, there will be a place for us to choose which script to execute.

    The project name, ipA path, app_identifier and other information will be printed out.

    ipaanddYSMFiles are stored in the project root directory.

    The metadata and IPA are then automatically uploaded to iTunes Connect.

    The final output is the time (s) taken to execute each script.

The advanced

If it’s just a simple upload to iTunes Connect, that should do the trick.

If we have multiple targets or need to configure something above ITC, we need to go a little further.

metadata

Metadata contains the information applied to the ITC and can be used to configure our ITC. It is recommended to use Deliverfile.

screenshots

Screen capture data.

Appfile

Store App information, such as Apple ID, bundle ID and so on.

Deliverfile

Delivery of documents. In this file, you can set all the configuration items of iTunes Connect, such as:

  • release_notes, this release is new.
  • copyright, copyright information.
  • submit_for_review, whether to directly submit the new version for review after uploading.
  • force, skip the HTML report file validation.
  • .

When setting the release_nores, support_URL, and private_URL configurations, hash the country code, for example:

release_notes(
	# China
	'zh-Hans' => ENV['RELEASE_NOTES'].# Australia
	'en-au' => ENV['RELEASE_NOTES_AU'].# the U.S.
	'en-us' => ENV['RELEASE_NOTES_US'])Copy the code

Fastfile

Automatic script configuration files. Is the entry point for our script, where all event-driven events are scheduled.

default_platform(:ios)

platform :ios do

	desc "demo upload_to_app_store"
	lane : Archive_TargetA do |options|
		scheme = options[:scheme]
		date = Time.new.strftime("%Y%m%d-%h%M")
		
		# export_method supports app-store, ad-hoc, package, Enterprise, development
		gym(
			scheme: "#{scheme}",
			output_name: "#{scheme}-#{date}.ipa",
			clean: true,
			export_method: 'app-store'.)# upload_to_app_store
		deliver When deliverfile is empty, this function is the same as upload_to_app_store
	end
end

Copy the code

CD Go to the project root directory and run the bundle exec fastlane Archive_TargetA scheme:”CDDemo”. The following scheme is the parameter.

Multi-Target

If we need to configure multiple targets for packaging, we can use environment variables to do so. If we now have two target, targetA and targetB, we need to create two. Env files, for example. Env. TargetA,. Env. TargetB, on Fastfile file directory at the same level

Env files can be configured with different (non-public) content such as app_identifier, release_Notes, and so on. The screenshot is as follows:

In Appfile, Deliverfile, Fastfile, etc., we can use the contents of the.env file directly.

Appfile

# Appfile

#The bundle identifier of your app
app_identifier ENV['APP_IDENTIFIER']

# Your Apple email address
apple_id ENV['APPLE_ID'] 

# Developer Portal Team ID
team_id ENV['TEAM_ID']

Copy the code

For Deliverfile, hash the release_noRES, support_URL, and private_URL configurations.

# app_identifier
app_identifier ENV['APP_IDENTIFIER']

# User name,Apple ID email address
username ENV['APPLE_ID']

ID # team
team_id ENV['TEAM_ID']

# team name
team_name ENV['TEAM_NAME']

# copyright
copyright ENV['COPYRIGHT']

# key
keywords(
	'zh-Hans' => ENV['KEYWORDS'],)New version change record
release_notes(
	# China
	'zh-Hans' => ENV['RELEASE_NOTES'].# Australia
	'en-au' => ENV['RELEASE_NOTES_AU'].# the U.S.
	'en-us' => ENV['RELEASE_NOTES_US'])# Support url
support_url(
	# China
	'zh-Hans' => ENV['SUPPORT_URL'].# Australia
	'en-au' => ENV['SUPPORT_URL_AU'].# the U.S.
	'en-us' => ENV['SUPPORT_URL_US'])# privacy policy url Country code https://www.cnblogs.com/Mien/archive/2008/08/22/1273950.html
privacy_url(
	# China
	'zh-Hans' => ENV['PRIVACY_URL'].# Australia
	'en-au' => ENV['PRIVACY_URL_AU'].# the U.S.
	'en-us' => ENV['PRIVACY_URL_US'])# Submit the new version for review after uploading
submit_for_review false

# Skip HTML report file validation
force true

# Enable grayscale publishing with iTC's phased publishing function
phased_release true

Contact information for the app review team
app_review_information(
  first_name: "xx",
  last_name: "xx",
  phone_number: "+ 86 18888888888",
  email_address: "xxxx",
  demo_user: "[email protected]",
  demo_password: "test123")...Copy the code

The Fastfile file uses environment variables, which are slightly different. In Fastfile, we need to tell Lane to use the.env file. In this case, we need to use the sh script to call a lane followed by the env environment variable name. In this case, we cannot declare the lane as private_lane.

lane :releaseDemo2 do
	# no parameters
	sh "fastlane Archive_TargetA --env TargetA"
	
	# a parameter
	sh 'fastlane Archive_TargetA type:\'Ha ha ha ha \' --env TargetA'End calls bundle directly outside (with arguments)exec fastlane Archive_TargetA type:"haha" --env TargetA

Copy the code

We then use ENV[‘xx’] in the Archive_TargetA lane and read from the.env.targeta file. Similarly, the DeliverFile file for the deliver Action is read from the.env.targeta file.

private_lane : Archive_TargetA do |options|
	scheme = ENV['SCHEME'] 'scheme' is 'TargetA', read from '.env.targeta '

	# export_method supports app-store, ad-hoc, package, Enterprise, development
	gym(
		scheme: "#{scheme}",
		output_name: "#{scheme}.ipa",
		clean: true,
		export_method: 'app-store',
	)

	deliver '.env.targeta 'is the file from which deliverfile is read
end
Copy the code

Calls between lanes

The same way we write our own method calls, for example:

desc "Packaged unified entry"
lane :Archive do |options|
	If the 'type' we pass in is targetA, then we execute Archive_TargetA for the lane...
	type = options[:type]
  	if type= ="TargetA"
    	Archive_TargetA(options)
  	elsif type= ="TargetB"
        Archive_TargetB(options)
	else
    	Archive_TargetA(options)
  	end
end
Copy the code

System level lane

Fastlane has lane by default.

  • Before_all is the code that is executed before the script is executed once. We can execute common things like git_pull, cocoapods.

    before_all do
      # Check out the Developer branch
      sh 'git checkout Developer'
      git_pull
      cocoapods(repo_update: true)
    end
    Copy the code
  • After_all, after successful completion, handles the common postlogic.

  • Before_each, executed before each lane execution.

  • After_each is executed after each lane execution.

  • Error, any environment error will be aborted and executed once.

Execution order

Execution order The method name instructions
1 before_all Only once before lane is executed.
2 before_each This is done before each lane execution.
3 lane Custom tasks.
4 after_each This is done after each lane execution.
5 after_all Execute once after lane execution has successfully ended.
6 error Any environment error will be aborted and executed once.

Error

  • Command Timed out after 10 seconds on try 1 of 4, timed again… , at the beginning of the Fastlane file add:

    ENV["FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT"] = "180"
    ENV["FASTLANE_XCODE_LIST_TIMEOUT"] = "180"
    Copy the code

The plug-in

  • versioningTo modifybuildThe version number andversionThe version number.FastlaneThe embeddedactionincrement_build_numberIt was supplied by AppleagvtoolAgvtool changes all target version numbers when changing Build. If you have more than one product in a project, each Build will be incremented by one, and you’ll end up with a higher Build.fversioningNot only can you specify a target to add a Build, but you can also add a Version according to the “semantic Version” specification, or you can set the Version directly.
  • firim, directAdHocorInHouseThe packed IPA is uploadedfir.imFor test download.

Custom plug-in

Plug-in installation Format

Fastlane add_plugin [name] needs to be executed in the project root directory.

The fastlane update_plugins update, as above, requires CD to the project root directory.

Pay attention to

  • Keep the packaging machine’s Xcode and certificates up to date.
  • Cannot be set to when invoked using script commandsprivate_lane.
  • private_laneRepresents a private lane, usingbundle exec fastlaneCommand, declared asprivate_laneIs not displayed and cannot be set to when using script commandsprivate_lane.

other

You can checkout the Developer branch by executing a git command directly from the lane, such as sh ‘git checkout Developer’.

Due to my limited level, it is inevitable that there will be mistakes and omissions, welcome to issue corrections. If you have better examples of using Fastlane, you are welcome to share.

Refer to the article

  • Old Xing Thierry’s Fatlane Combat series
  • IOS – Continuous delivery
  • Fastlane brings continuous deployment to iOS
  • fastlaneTools
  • Fastlane document
  • Fastlane – Mobile development automation
  • Fastlane’s dark magic: pack and upload to the AppStore with one click
  • Use Fastlane in iOS for automated packaging and distribution
  • Automated releases for small teams – Fully automated releases from Fastlane
  • Fastlane Instruction manual

Example

Reprint please indicate the source!