前言简介
fastlane是一款使用ruby实现的跨平台的持续集成工具,支持安卓和iOS平台项目的持续集成实践,fastalne处理提供包含:初始设置、屏幕截图、打包、上传到测试平台、部署等功能。本篇主要介绍fastlane的集成,以及配合jenkins自动化的搭建
iOS fastlane安装及使用:
(一)安装升级bundle
-
fastalne需要在ruby环境下才能安装,并且系统ruby版本需要高于2.6.2。如果bundle版本过低,需要先升级bundle
-
安装升级bundle 执行命令行
sudo gem install bundler -n/usr/local/bin
-
cd到项目根目录输入bundle init 生成Gemfile文件,在此文件写入
source "[https://gems.ruby-china.com](https://gems.ruby-china.com/)" gem "fastlane"
-
安装依赖库, 执行命令行
bundle install
-
更新Gemfile.lock文件的版本和依赖 执行命令行
bundle update
(二)Fastlane安装、初始化项目
-
使用HomeBrew安装(管理各种包) 执行命令行
brew install fastlane
进行安装。可以用命令fastlane --version
来查看是否安装成功
-
安装成功以后cd到工程目录下初始化 执行命令行
fastlane init
出现下图选择你所需要的功能模块⬇️
- 1、自动化截图 2、自动化beta分发至TestFlight 3、自动化App Store 分发 4、手动写自动化脚本(自定义发布平台如pgyer,firm等)。
- 本篇介绍的是iOS上传到TestFLight,所以直接选择2
- 输入苹果账号,等待一会儿,然后一直回车就可以了
- 构建完成会自动生成如下文件:⬇️
(三)使用fastlane自动化构建
- 初始化完成后,在Appfile文件内设置对应的开发者账号以及bundle id。fastFile中添加绕开二次验证的脚本。
注:(如果选择是2自动化beta分发至testFlight则不需要手动填写)
- 注:keyid和issuer_id还有.p8文件是通过登陆苹果开发者中心-用户与访问-密钥,需账户持有者去创建并生成,生成一次可以所有项目通用
- 终端输入命令行
fastlane beta
,等待上传成功即可
(四)项目配置
-
电脑如已经安装fastalne,直接cd到项目跟目录下,运行
fastalne init
,即可生成对应文件,直接复制公用脚本文件,修改SCHEME
名称,拷贝.p8文件到fastlane文件夹下即可
iOS jenkins配合fastlane自动化出包:
-
在jenkins中创建单独的任务,用来专门上传到testFlight分发,将脚本写到jenkins本地。将项目名称、git地址、代码分支,开发环境写成变量在脚本中去适配不同的项目,配置好了以后如下⬇️
-
项目需要分发时直接切换项目名称,和对应的选项,即可直接构建,等待上传成功即可
问题总结:
- Check out the few lines of raw
xcodebuild
output above for potential hints
- 描述文件配置问题,项目内的描述文件需要配置正确,均使用distribution描述文件
-
jenkins构建时候报错
13:57:17: Transporter transfer failed.
13:57:17: Please sign in with an app-specific password. You can create one at appleid.apple.com. (-22910)
13:57:17: Your account has 2 step verification enabled
13:57:17: Please go to appleid.apple.com/account/man…
13:57:17: and generate an application specific password for
13:57:17: the iTunes Transporter, which is used to upload builds
13:57:17: To set the application specific password on a CI machine using
13:57:17: an environment variable, you can set the
13:57:17: FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD variable
- 是因为苹果的双重身份验证导致的问题,需要在苹果开发者中心,用户中心,获取到对应的密钥
key_id
、issuer_id
、还有.p8
文件,注:(p8文件需要保存到本地进行备份)放到对应的位置上,获取方式如上。获取完成的.p8文件放在fastlane脚本文件同目录下,然后在Fastfile文件中添加脚本
Fastlane相关脚本:
# 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
# update_fastlane
SCHEME = "项目名称"
Method = "app-store"
default_platform(:ios)
def updateBuild
currentTime = Time.new.strftime("%Y%m%d%H%M%S")
increment_build_number(
build_number: "#{currentTime}"
)
end
api_key = app_store_connect_api_key(
key_id: "xxx",
issuer_id: "xxx",
key_filepath: "./fastlane/xxx.p8",
duration: 1200,
in_house: false
)
platform :ios do
desc "Description of what the lane does"
lane :beta do
updateBuild
gym(
scheme: SCHEME,
workspace: "#{SCHEME}.xcworkspace",
output_directory: "./fastlane/release",
output_name: "#{SCHEME}.ipa",
export_method: Method,
export_xcargs: "-allowProvisioningUpdates",
silent: true,
clean: true
)
upload_to_testflight(
api_key: api_key,
ipa: "./fastlane/release/#{SCHEME}.ipa",
skip_waiting_for_build_processing: true,
skip_submission: true
)
end
end