简介
fastlane是一个命令行工具,帮助开发者完成一些自动化任务,比如打包应用,发布应用,管理证书配置文件等等。
安装
brew install fastlane
sudo gem install fastlane
fastlane init
fastlane init之后,会在当前目录下创建一个fastlane文件夹,以及./Gemfile、./Gemfile.lock文件。
fastlane文件夹下会有Appfile、Fastfile、Matchfile、Pluginfile。接下来就详细说下,这4个file里面的内容。
Appfile
Appfile 存储了应用相关的信息,比如App id、bundle id等。
app_identifier "com.app.xxx" # The bundle identifier of your app
apple_dev_portal_id "xxxx@xx.com" # Apple Developer Account
itunes_connect_id "xxxx@xx.com" # App Store Connect Account
apple_id "xxxx@xx.com" # Your Apple email address
team_id "xxxx" # Developer Portal Team ID
itc_team_id "xxxxx" # App Store Connect Team ID
# For more information about the Appfile, see:
# https://docs.fastlane.tools/advanced/#appfile
Fastfile
Fastfile存储着fastlane运行的自动化配置。比如自定义打包的lane、配置环境变量、以及通过执行一些Action或者插件去执行任务,比如打包上传Fir、AppStore以及打包lane结果通过企业微信通知。接下来讲下Fastfile里面的相关内容。
Lanes
自定义lane
一个lane可以看成是一个函数方法。在终端命令行fastlane my_lane param1:xxx param2:xxx,传递了参数param1、param2。在lane里面,options接收参数。
lane :my_lane do |options|
# Whatever actions you like go in here.
value1 = options[:param1]
value2 = options[:param2]
end
before_all block
before_all在所有lane执行之前会先执行一次
before_all do |lane, options|
cocoapods
end
after_all block
after_all在所有lane执行之完之后执行一次
after_all do |lane, options|
say("Successfully finished deployment (#{lane})!")
slack(
message: "Successfully submitted new App Update"
)
sh("./send_screenshots_to_team.sh") # Example
end
error block
lane执行失败,会走error
error do |lane, exception, options|
slack(
message: "Something went wrong with the deployment.",
success: false,
payload: { "Error Info" => exception.error_info.to_s }
)
end
导入其他的Fastlane
- import
导入本地的Fastlane
import "../GeneralFastfile"
override_lane :from_general do
# ...
end
- import_from_git
从Git远程仓库导入Fastlane
default_platform(:ios)
platform :ios do
xcversion(version: "~> 10.2")
github_url = "git@github.com"
import_from_git(url: "#{github_url}:XXX/FastlaneConfig.git",
branch: "master",
path: "fastlane/Fastfile")
end
Actions
官网Actions
action对应每一个任务,底层都是调用其他的工具或者工具,都是对一些插件功能的封装。
常用的action
Action | 描述 |
---|---|
gym | build_app 的别名 |
build_app | 构建应用 |
xcodebuild | 构建应用 |
cocoapods | 运行 cocoapods |
xcclean | 清理工程 |
increment_build_number | 增加版本号 |
match | sync_code_signing 的别名,同步证书和配置描述文件 |
upload_to_testflight | 上传到 TestFlight |
打包配置
# This file contains the fastlane.tools configuration
# You can find the documentation at https://docs.fastlane.tools
#
# For a list of all available actions, check out
#
# https://docs.fastlane.tools/actions
#
# For a list of all available plugins, check out
#
# https://docs.fastlane.tools/plugins/available-plugins
#
# Uncomment the line if you want fastlane to automatically update itself
default_platform(:ios)
platform :ios do
before_all do
ENV["FASTLANE_ITC_TEAM_ID"] = "xxx"
ENV["FASTLANE_USER"] = "xxx@xxx.com"
ENV["FASTLANE_PASSWORD"] = "***"
ENV["FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD"] = "****-****"
end
desc "Description of what the lane does"
lane :deploy do |options|
# add actions here: https://docs.fastlane.tools/actions
distribute = options[:distribute]
target = options[:target]
export = options[:exports] #导出方式
symbols = false
bitcode = false
bundleName = "com.xxx.app"
extensionName = "com.xxx.app.extension"
provisioning = "match AdHoc #{bundleName}" #App 签名
extension_provisioning = "match AdHoc #{extensionName}" #测试 extension 签名
# extension_product_provisioning = "match AdHoc com.app.wisecart.ServiceExtensionProduct" #正式 extension 签名
configuration = options[:configuration]
if target == "Product"
end
if distribute == "Fir"
else
# export = "app-store"
# configuration = "Release"
if distribute == "AppStore"
provisioning = "match AppStore #{bundleName}"
extension_provisioning = "match AppStore #{extensionName}"
# extension_product_provisioning = "match AppStore com.app.wisecart.ServiceExtensionProduct"
symbols = true
bitcode = false
end
end
# the name of the ipa file
time = Time.new
dateTimeStr = "#{time.year}-#{time.month}-#{time.day}_#{time.hour}-#{time.min}"
ipaName = "#{dateTimeStr}_#{target}.ipa"
directoryPath = "../../../Output_ipa"
fullPath = "../../../Output_ipa/#{ipaName}"
xcode_select("/Applications/Xcode.app")
# 编译
gym(
scheme: target, # 指定需要打那个 scheme 的包
export_method: export, # 打包的方式
clean: true, # 打包前是否 clean 项目
configuration: configuration,
silent: false, # 隐藏不必要信息
include_bitcode: bitcode, #是否开启bitcode
include_symbols: symbols, #包含符号
output_directory: directoryPath,
output_name: ipaName,
export_options: {
stripDebugSymbols: true,
provisioningProfiles: {
bundleName => provisioning,
extensionName => extension_provisioning
}
}
)
# 上传
description = options[:description]
if distribute == "Fir"
# 插件打包上传Fir
gofir(description: description, path: fullPath, branch: options[:branch], target: target, buildName: options[:buildName], jobName: options[:jobName])
elsif distribute == "Pgyer"
# 插件打包上传蒲公英
goPgyer(description: description, path: fullPath, configuration: configuration, scheme: target, export_method: export_method)
else
# 插件打包上传苹果
upload_testflight
end
end
lane :gofir do |options|
desc "上传 beta 到 fir"
description = options[:description]
path = options[:path]
branch = options[:branch]
# gym export_method: "ad-hoc"
# 多个参数 可以使用逗号(, )分离
info = fir_cli(api_token:"api_token", specify_file_path:"#{path}", changelog:"#{description}", skip_update_icon: "true", need_release_id: "true")
notice(description: description,branch: branch, release_id: info[:release_id],short: info[:short],target: options[:target],buildName: options[:buildName],jobName: options[:jobName])
# download_url = "http://d.firim.top/#{info[:short]}?release_id=#{info[:release_id]}"
end
lane :notice do |options|
desc "企业微信通知"
description = options[:description]
branch = options[:branch]
release_id = options[:release_id]
short = options[:short]
target = options[:target]
path = options[:path]
buildName = options[:buildName]
jobName = options[:jobName]
bundleName = get_ipa_info_plist_value(ipa: path, key: "CFBundleName")
qrCodeImagePath = File.expand_path("../../../../Output_ipa/fir-#{bundleName}.png")
md5 = sh("md5sum #{qrCodeImagePath}|awk '{print $1}'").gsub(/\s+/,'')
qrcodeBase64Text = sh("base64 #{qrCodeImagePath} |sed ‘/^$/d'").gsub(/\s+/,'')
# 接入企业微信插件,通知消息
work_wechat(webhook_URL: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxx-xxxx-xxxx",
image_md5: md5,
image_base64: qrcodeBase64Text)
work_wechat(webhook_URL: "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxx-xxxx-xxxx",
markdown_content: "👆👆👆美女帅锅们,二维码在上面\n打包项目:#{jobName}\n打包分支:#{branch}\n打包人:#{buildName}\n链接:[http://d.3appstore.com/#{short}?release_id=#{release_id}](http://d.3appstore.com/#{short}?release_id=#{release_id})\n更新内容:#{description}")
end
lane :goPgyer do |options|
desc "上传 pgyer"
description = options[:description]
path = options[:path]
configuration = options[:configuration]
target = options[:scheme]
export = options[:export]
# gym export_method: "ad-hoc"
gym(
configuration: configuration,
clean:true,
scheme: target, #工程下要打包的项目,如果一个工程有多个项目则用[项目1,项目2]
export_method: export, #打包的方式, development/adhoc/enterprise/appstore
output_directory: path, #指定ipa最后输出的目录
)
# 多个参数 可以使用逗号(, )分离
pgyer(api_key: "api_key", user_key: "user_key",password: "****", install_type: "2", update_description: description)
# download_url = "http://d.firim.top/#{info[:short]}?release_id=#{info[:release_id]}"
end
lane :upload_testflight do
desc "上传 testflight"
api_key = app_store_connect_api_key(
key_id: "team_id",
issuer_id: "xxxxx",
key_filepath: "/AuthKey_xxxx.p8",
duration: 1200, # optional (maximum 1200)
in_house: false # optional but may be required if using match/sigh
)
upload_to_testflight(
skip_waiting_for_build_processing: true
)
# 上传符号文件
upload_symbols_to_crashlytics(
gsp_path: "./Targets/Mitra/GoogleService-Info.plist",
binary_path: "../upload-symbols"
)
end
end
以上打包配置可根据项目自身实际情况,稍作修改。
Matchfile管理iOS证书
Fastlane通过match来生成证书配置以及描述文件,通过加密上传到Git仓库中,为这种团队开发提供了方便。
接入
fastlane match init
命令会在./fastlane文件夹里生成一个Matchfile的文件。在Matchfile中,配置下远程git仓库地址、远程分支、bundle id、git账号密码等。
git_url("git@xxxx:xxxx/xxxx.git")
storage_mode("git")
# 如果有多个项目App的情况下,可以用不同的分支
git_branch("master")
# 证书的类型
type("development","adhoc","appstore") # The default type, can be: appstore, adhoc, enterprise or development
api_key_path("api_key.json")
# 可以配置多个bundle id
app_identifier(["com.app.xxxxx"])
username("xxxx@xxxx.com")
team_id("team_id")
keychain_password("password")
match
使用
# 配置开发,测试,苹果证书以及描述文件
fastlane match development
fastlane match adhoc
fastlane match appstore
参数
# 在控制台可以打印详细信息
--verbose
# 在新机器上不会创建新的证书或配置文件
--readonly
# 设备发生了变化,更新配置文件
--force_for_new_devices
文件夹结构
第一次运行match后,会有2个文件夹上传到git仓库。还有一个README.md,方便团队新成员加入
- certs 包含了所有的证书及其私钥
- profiles 包含所有的配置文件
更新证书
- 先删掉远程的certs/development/.cer文件。
- 执行fastlane match unke development,会清理开发者账号下的development证书和配置文件。
- 执行fastlane match development会自动更新。
Pluginfile
# Autogenerated by fastlane
#
# Ensure this file is checked in to source control!
gem 'fastlane-plugin-fir_cli'
gem 'fastlane-plugin-apphoster'
gem 'fastlane-plugin-firim'
gem 'fastlane-plugin-work_wechat'
gem 'fastlane-plugin-pgyer'
gem 'fastlane-plugin-emerge'
在add_plugin添加插件之后,会自动生成fastlane/Pluginfile文件,安装好的插件会添加插件到该文件里。
搜索插件
# 控制台打印出所有的插件列表
fastlane search_plugins
添加插件
fastlane add_plugin fir_cli
其他机器安装插件
# 安装插件,确保所有的插件已经安装在本机器中
fastlane install_plugins
# 更新插件
fastlane update_plugins
Gemfile
source "https://rubygems.org"
gem "fastlane"
plugins_path = File.join(File.dirname(__FILE__), 'fastlane', 'Pluginfile')
eval_gemfile(plugins_path) if File.exist?(plugins_path)
在fastlane init命令初始化之后,会自动生成该文件以及Gemfile.lock。在fastlane add_plugin执行成功之后,会自动修改该文件。