fastlane

自动构建iOS包,主要有2中方式,
1)shell脚本,使用xctool、xcodebuild等工具打包、提取成果物。
2)fastlane
本文描述fastlane构建程序的使用方法。

环境:

  • macOS High Sierra 10.14.6 (因为要打包iOS需要使用Xcode工具链,所以采用macOS)
  • Xcode 10.3 (10G8)
  • brew (macOS 安装三方软件工具)
  1. 安装
  • 使用brew安装
$ brew cask install fastlane
  • 使用RubyGems安装(未验证)
$ sudo gem install fastlane -NV
  1. 初始化工程
$ cd path/to/project
$ fastlane init
fastlane init

选择用途,本文选择4,手动配置。


卡在bundle update

如果卡在这里,可以关掉窗口,删掉项目目录下的fastlane目录,然后重新执行 fastlane init 命令,如果出现下面内容,代表初始化成功。

[19:43:48]: What would you like to use fastlane for?
1.   Automate screenshots
2. ‍✈️  Automate beta distribution to TestFlight
3.   Automate App Store distribution
4.   Manual setup - manually setup your project to automate your tasks
?  4
[19:43:50]: ------------------------------------------------------------
[19:43:50]: --- Setting up fastlane so you can manually configure it ---
[19:43:50]: ------------------------------------------------------------
[19:43:50]: --------------------------------------------------------
[19:43:50]: --- ✅  Successfully generated fastlane configuration ---
[19:43:50]: --------------------------------------------------------
[19:43:50]: Generated Fastfile at path `./fastlane/Fastfile`
[19:43:50]: Generated Appfile at path `./fastlane/Appfile`
[19:43:50]: Gemfile and Gemfile.lock at path `Gemfile`
[19:43:50]: Please check the newly generated configuration files into git along with your project
[19:43:50]: This way everyone in your team can benefit from your fastlane setup
[19:43:50]: Continue by pressing Enter ⏎

[19:45:11]: fastlane will collect the number of errors for each action to detect integration issues
[19:45:11]: No sensitive/private information will be uploaded, more information: https://docs.fastlane.tools/#metrics
[19:45:11]: ----------------------
[19:45:11]: --- fastlane lanes ---
[19:45:11]: ----------------------
[19:45:11]: fastlane uses a `Fastfile` to store the automation configuration
[19:45:11]: Within that, you'll see different lanes.
[19:45:11]: Each is there to automate a different task, like screenshots, code signing, or pushing new releases
[19:45:11]: Continue by pressing Enter ⏎

[19:45:35]: --------------------------------------
[19:45:35]: --- How to customize your Fastfile ---
[19:45:35]: --------------------------------------
[19:45:35]: Use a text editor of your choice to open the newly created Fastfile and take a look
[19:45:35]: You can now edit the available lanes and actions to customize the setup to fit your needs
[19:45:35]: To get a list of all the available actions, open https://docs.fastlane.tools/actions
[19:45:35]: Continue by pressing Enter ⏎

[19:45:39]: ------------------------------
[19:45:39]: --- Where to go from here? ---
[19:45:39]: ------------------------------
[19:45:39]:   Learn more about how to automatically generate localized App Store screenshots:
[19:45:39]:   https://docs.fastlane.tools/getting-started/ios/screenshots/
[19:45:39]: ‍✈️  Learn more about distribution to beta testing services:
[19:45:39]:   https://docs.fastlane.tools/getting-started/ios/beta-deployment/
[19:45:39]:   Learn more about how to automate the App Store release process:
[19:45:39]:   https://docs.fastlane.tools/getting-started/ios/appstore-deployment/
[19:45:39]: ‍⚕️  Learn more about how to setup code signing with fastlane
[19:45:39]:   https://docs.fastlane.tools/codesigning/getting-started/
[19:45:39]: 
[19:45:39]: To try your new fastlane setup, just enter and run
[19:45:39]: $ fastlane custom_lane
  1. 配置工程
    1)打开项目目录,确认有 fastlane 目录,且有 Appfile Fastfile 两个文件


    目录结构
  • Appfile: 用于配置应用信息(bundle id、team id 等) & 账号信息(apple id),文件内容
app_identifier("xxxx.xxxx.xxxx") # The bundle identifier of your app
apple_dev_portal_id "[email protected]" # Your Apple email address
itunes_connect_id "[email protected]" # 上传IPA包的账号,注1

itc_team_id("xxxxxxxxx") # App Store Connect Team ID
team_id("xxxxxxxxxx") # Developer Portal Team ID

# For more information about the Appfile, see:
#     https://docs.fastlane.tools/advanced/#appfile

获取 itc_team_id 方法,需要ruby环境

$ irb
irb> require "spaceship" 
irb> Spaceship::Tunes.login("iTunesConnect_username", "iTunesConnect_password") 
irb> Spaceship::Tunes.select_team
  • Fastfile: 构建信息,文件内容
ENV["FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT"] = "180" # fastlane编译超时时间,如果工程大且设备性能一般,可以设置这个变量
ENV["FASTLANE_XCODE_LIST_TIMEOUT"] = "180"
#ENV["FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD"] = "xxxxx" #app密码,可以在  申请,注1

default_platform :ios

platform :ios do
  before_all do
    # cocoapods
    # carthage
  end

  desc "Submit a new Beta Build to Apple TestFlight"
  desc "This will also make sure the profile is up to date"
  lane :store do # :store可以任意命名,可以包含多个lane
    scheme = "xxxxxx"
    xcodeproj = "xxxxxx.xcodeproj"
    base_output_directory = "~/Desktop/ios/xxxxx"

    # 生成 build & 并写入到 xcodeproj, 注2
    build = Time.now.strftime("%Y%m%d%H%M")
    increment_build_number(
      build_number: build,
      xcodeproj: xcodeproj
    )

    # 获取version
    version = get_version_number(xcodeproj: xcodeproj, target: scheme)

    tag = "v" + version + "-" + build
    # 输出版本号到控制台,Jenkins可以获取到
    sh "echo [version] #{tag}"

    # 成果物输出目录,可以自定
    output_directory = base_output_directory + "/" + version + "/" + build

    # 构建
    gym(
      scheme: scheme,
      clean: true,
      output_directory: output_directory
    )

    # 提交修改
    commit_version_bump(xcodeproj: "./" + xcodeproj)

    # 推送到服务器
    push_to_git_remote

    # 上传IPA到TestFlight
    testflight(skip_waiting_for_build_processing: true)
  end

  # You can define as many lanes as you want

  after_all do |lane|
    # This block is called, only if the executed lane was successful
  end

  error do |lane, exception|
    # 打包异常,恢复仓库
    reset_git_repo(force: true)
  end
end

# More information about multiple platforms in fastlane: https://docs.fastlane.tools/advanced/#control-configuration-by-lane-and-by-platform
# All available actions: https://docs.fastlane.tools/actions

# fastlane reports which actions are used. No personal data is recorded.
# Learn more at https://docs.fastlane.tools/#metrics
  1. 执行构建,(没有使用fastlane签名功能,需要提前下载好证书&描述文件)
$ fastlane store # store 是 Fastfile “lane :store do” 定义的,可以自定

另一种方式

$ bundle exec fastlane store

注0:gitignore添加如下信息,防止git 保存 fastlane报告

## fastlane
fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots
fastlane/test_output

注1:苹果开发者账号必须开启2FA功能,上传到TestFlight需要短信验证。网上有几种方案:
1)使用App专用密码(实际验证,本文不起作用, 申请https://appleid.apple.com)

申请App 密码

2)注册一个用于上传IPA的apple id,使用此账号上传IPA包(本文采纳此方案)Appfile itunes_connect_id
3)定期生成token (实际验证,需要定期操作,不太方便)
参考:https://docs.fastlane.tools/best-practices/continuous-integration/#two-step-or-two-factor-auth

注2:写入build 需要设置target,参考下图。


build setting

注3:fastlane可以通过 ENV["variable_name"] 方式获取环境变量,比如

$ export net_env = "00"

在 Fastfile 获取 & 使用

net_env = ENV["net_env"]
build = Time.now.strftime("%Y%m%d%H%M") + net_env

参考:
https://docs.fastlane.tools/

你可能感兴趣的:(fastlane)