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 "xxx@xxx.xxxx" # Your Apple email address
itunes_connect_id "xxx@xxx.xxx" # 上传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/

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,026评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,655评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,726评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,204评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,558评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,731评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,944评论 2 314
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,698评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,438评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,633评论 2 247
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,125评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,444评论 3 255
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,137评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,103评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,888评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,772评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,669评论 2 271

推荐阅读更多精彩内容