iOS 自动打包

使用FastLane打包

安装 fastlane

  • 通过HomeBrew安装
    brew install fastlane

  • 通过Bundler安装

    1. 安装 bundler
    2. 然后在项目根目录下创建./Gemfile文件,编辑内容
1
2
3
4

// 安装 bundler
$ gem install bundler

1
2
3
4
5

//然后在项目根目录下创建./Gemfile文件,编辑内容
source "https://rubygems.org"
gem "fastlane"

编辑Gemfile文件:

1
2
3
4
5
6

source "https://rubygems.org"
gem "fastlane", "2.180.1"
# 如果使用Cocoapods,需要添加下面这行
gem "cocoapods"

  • 通过ruby gems安装
1
sudo gem install fastlane

fastlane安装成功后,安装两个插件,用于版本号管理和打包成功后上传到对应的第三方平台

1
2
3
4
5
6
7
8
9
10
// 添加fastlane插件
// versioning使用参考,https://github.com/SiarheiFedartsou/fastlane-plugin-versioning,用于版本号获取和修改
// firim是fir平台插件

fastlane add_plugin versioning
fastlane add_plugin fir_cli # https://github.com/FIRHQ/fastlane-plugin-fir_cli


// pgyer是蒲公英平台
// fastlane add_plugin pgyer

fastlane内容编辑

fir平台的firim相关参数参考:传送门,最少需要firim_api_token参数,可以从自己注册的firim中获取到,也可以配置如下参数

  • firim_api_token
  • app_name
  • app_desc
  • app_passwd
  • app_version
  • app_build_version
  • app_changelog
  • 等等

编辑Fastfile,定义一个Action,名字为TestFir,指定输出包名为(版本号+时间),打包后包到目录为./build目录下,打包完成后上传到fir。如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

default_platform(:ios)

platform :ios do
desc "Description of what the lane does"

lane :TestFir do
time = Time.new.strftime("%Y%m%d%H%M") # 获取时间格式,格式参考https://www.runoob.com/python/att-time-strftime.html
# verion = get_version_number_from_list() # 获取版本号

version = get_version_number_from_xcodeproj(build_configuration_name: 'Release') # 使用参考GitHub链接,https://github.com/SiarheiFedartsou/fastlane-plugin-versioning

ipaName = "Release_#{version}_#time.ipa" # 生成ipa包的名字格式

gym(
clean: true, # 打包前清理项目
silent: true, # 隐藏没有必要的信息
scheme: "Your Scheme Name", # 指定项目的scheme名称
export_method: "ad-hoc", # 打包的类型,有:ad-hoc, enterprise, app-store, development
configuration: "Release", # scheme: 默认为Release,还有Debug
output_name: "#{ipaName}", # 输出的报名
output_directory: "./build" # 输出的位置
)
# 自己的fir账号,可配置内容参考https://github.com/FIRHQ/fastlane-plugin-fir_cli
fir_cli api_token: "xxx", changelog: "My First Fir Upload"
# 蒲公英的配置 替换为自己的api_key和user_key
# pgyer(api_key: "******", user_key: "******",update_description: options[:update_info])
end

end

使用时,在命令行输入fastlane TestFir即可

1
2
fastlane TestFir

如果想要在执行命令时从外部传入参数,则可以按照下面的方式使用,在do后面添加 |options|,使用时,options[:optionName]这种方式来获取从外面传入的值

1
2
3
4
5
6
7
8
9
lane :ActionName do |options|
gym(
configuration: options[:configuration],#环境
)

# 自己的fir账号
fir_cli api_token: "xxx", changelog: options[:changelog]
end

从外面调用的方式如下:

1
fastlane ActionName configuration:"adhoc" changelog:"first submit"

使用Jenkins打包,未完待续

参考

iOS 之 自动打包 fastlane + fir + pgy 【进阶使用】