fastlane

自動(dòng)構(gòu)建iOS包,主要有2中方式,
1)shell腳本,使用xctool、xcodebuild等工具打包、提取成果物。
2)fastlane
本文描述fastlane構(gòu)建程序的使用方法。

環(huán)境:

  • macOS High Sierra 10.14.6 (因?yàn)橐虬黫OS需要使用Xcode工具鏈,所以采用macOS)
  • Xcode 10.3 (10G8)
  • brew (macOS 安裝三方軟件工具)
  1. 安裝
  • 使用brew安裝
$ brew cask install fastlane
  • 使用RubyGems安裝(未驗(yàn)證)
$ sudo gem install fastlane -NV
  1. 初始化工程
$ cd path/to/project
$ fastlane init
fastlane init

選擇用途,本文選擇4,手動(dòng)配置。


卡在bundle update

如果卡在這里,可以關(guān)掉窗口,刪掉項(xiàng)目目錄下的fastlane目錄,然后重新執(zhí)行 fastlane init 命令,如果出現(xiàn)下面內(nèi)容,代表初始化成功。

[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)打開項(xiàng)目目錄,確認(rèn)有 fastlane 目錄,且有 Appfile Fastfile 兩個(gè)文件


    目錄結(jié)構(gòu)
  • Appfile: 用于配置應(yīng)用信息(bundle id、team id 等) & 賬號(hào)信息(apple id),文件內(nèi)容
app_identifier("xxxx.xxxx.xxxx") # The bundle identifier of your app
apple_dev_portal_id "xxx@xxx.xxxx" # Your Apple email address
itunes_connect_id "xxx@xxx.xxx" # 上傳IPA包的賬號(hào),注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環(huán)境

$ irb
irb> require "spaceship" 
irb> Spaceship::Tunes.login("iTunesConnect_username", "iTunesConnect_password") 
irb> Spaceship::Tunes.select_team
  • Fastfile: 構(gòu)建信息,文件內(nèi)容
ENV["FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT"] = "180" # fastlane編譯超時(shí)時(shí)間,如果工程大且設(shè)備性能一般,可以設(shè)置這個(gè)變量
ENV["FASTLANE_XCODE_LIST_TIMEOUT"] = "180"
#ENV["FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD"] = "xxxxx" #app密碼,可以在  申請(qǐng),注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可以任意命名,可以包含多個(gè)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
    # 輸出版本號(hào)到控制臺(tái),Jenkins可以獲取到
    sh "echo [version] #{tag}"

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

    # 構(gòu)建
    gym(
      scheme: scheme,
      clean: true,
      output_directory: output_directory
    )

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

    # 推送到服務(wù)器
    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|
    # 打包異常,恢復(fù)倉庫
    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. 執(zhí)行構(gòu)建,(沒有使用fastlane簽名功能,需要提前下載好證書&描述文件)
$ fastlane store # store 是 Fastfile “l(fā)ane :store do” 定義的,可以自定

另一種方式

$ bundle exec fastlane store

注0:gitignore添加如下信息,防止git 保存 fastlane報(bào)告

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

注1:蘋果開發(fā)者賬號(hào)必須開啟2FA功能,上傳到TestFlight需要短信驗(yàn)證。網(wǎng)上有幾種方案:
1)使用App專用密碼(實(shí)際驗(yàn)證,本文不起作用, 申請(qǐng)https://appleid.apple.com

申請(qǐng)App 密碼

2)注冊(cè)一個(gè)用于上傳IPA的apple id,使用此賬號(hào)上傳IPA包(本文采納此方案)Appfile itunes_connect_id
3)定期生成token (實(shí)際驗(yàn)證,需要定期操作,不太方便)
參考:https://docs.fastlane.tools/best-practices/continuous-integration/#two-step-or-two-factor-auth

注2:寫入build 需要設(shè)置target,參考下圖。


build setting

注3:fastlane可以通過 ENV["variable_name"] 方式獲取環(huán)境變量,比如

$ 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/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容