fastlane + iOS 自动打包

fastlane_text.png

概述

APP自动化打包常见的主流工具有JenkinsfastlaneJenkins功能强大,但是需要的配置也比较多,团队较大的可以优先考虑,fastlane是用Ruby语言编写的一套自动化工具集,比较轻便,配置简单,使用起来也很方便

fastlane安装

  • 第一步
    安装Ruby开发环境
ruby -v
  • 第二步
    安装Xcode命令行工具
xcode-select --install
  • 第三步
    安装fastlane
sudo gem install -n /usr/local/bin fastlane

fastlane 配置

进入到你的iOS项目目录下,执行fastlane初始化命令

fastlane init

命令执行完毕后,fastlane 给我们提示让我们选择需要执行哪种操作

[✔] 🚀 
[✔] Looking for iOS and Android projects in current directory...
[16:54:04]: Created new folder './fastlane'.
[16:54:04]: Detected an iOS/macOS project in the current directory: 'xxxx.xcworkspace'
[16:54:04]: -----------------------------
[16:54:04]: --- Welcome to fastlane 🚀 ---
[16:54:04]: -----------------------------
[16:54:04]: fastlane can help you with all kinds of automation for your mobile app
[16:54:04]: We recommend automating one task first, and then gradually automating more over time
[16:54:04]: 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
?  
  • 第一种获取App StoreApp预览照片。
  • 第二种打包上传至TestFlight工具上。
  • 第三种打包上传到App Store
  • 第四种自定义打包方式。
    这里我们选择第3种 或者第4种,执行后,会提示我们输入 appleID 开发者账号密码验证码等操作,然后一直到结束。

我们会在项目的根目录看到一个fastlane的文件夹,里面包含AppfileFastfile 文件,外部还有GemfileGemfile.lock 等新增文件;

Appfile: 存储有关开发者账号相关信息
Fastfile: 核心文件,用于命令行调用和处理具体的流程,lane相对于一个action方法或函数 
Gemfile 类似于cocopods 的Podfile文件

Appfile 文件配置如下:

基本上通过终端输入苹果开发者账号密码等一系列操作后,这个文件的配置基本不需要修改;

app_identifier("com.****.****") # The bundle identifier of your app
apple_id("********@qq.com") # Your Apple email address

itc_team_id("*******") # App Store Connect Team ID
team_id("*******") # Developer Portal Team ID

# For more information about the Appfile, see:
#     https://docs.fastlane.tools/advanced/#appfile

Fastlane + pgyer 配置

因为这里需要用到一个第三方的应用分发平台蒲公英,所以需要通过fastlane添加其它插件。

fastlane add_plugin pgyer
fastlane add_plugin versioning

这里先配置一个 Release版本的打包,然后通过 pgyer 上传的配置

 定义打包平台
default_platform :ios

   # 参数定义
   app_scheme = "[scheme]"    #这里双引号内填写自己的参数 ([]只是提示)    
   app_workspace = "[project.xcworkspace]"
   app_identifier = "[app_identifier]"
   # 打包日期拼写版本号
   dateTime = Time.new.strftime("%Y%m%d")
   version = get_version_number #获取版本号
   buildNumber = get_build_number  # 获取build号
   fileName = "#{version}_#{buildNumber}_#{dateTime}.ipa"

   platform :ios do
      before_all do
      git_pull
      last_git_commit
      sh "rm -f ./Podfile.lock"
      cocoapods(use_bundle_exec: false)
   end

   # 以下是Release版本的配置
   lane :release_pgyer do |options|
    
   sigh(app_identifier: app_identifier) #项目的bundle identifler    
   build_app(
      workspace: app_workspace,
      configuration: "Release",
      scheme: app_scheme,   
      silent: true, #在构建时隐藏不必要输出的信息
      clean: true, #在构建前是否clean工程
      include_bitcode:false,  #是否开启bitecode
      output_directory:"./packets" , #ipa的输出路径
      output_name:fileName,  #ipa的文件名(上面👆定义)
      export_method: "app-store",  #打包类型
      export_options: {
         # 描述文件匹配哪个 identifier 和 profile文件名
         provisioningProfiles: { "com.*****.live" => "recoder_release_profile"}
         }
      )

      pgyer(api_key: "30ef1623**********07fbd79") # 开始上传蒲公英及相关配置
   end

end

蒲公英API 参数的获取地址 https://www.pgyer.com/account/api

image.png

执行命令小试牛刀
fastlane release_pgyer

执行结果如下:

21:42:44]: Upload success. Visit this URL to see: https://www.pgyer.com/v9GR

+------+----------------------+-------------+
|             fastlane summary              |
+------+----------------------+-------------+
| Step | Action               | Time (in s) |
+------+----------------------+-------------+
| 1    | default_platform     | 0           |
| 2    | get_version_number   | 0           |
| 3    | get_build_number     | 0           |
| 4    | last_git_commit      | 0           |
| 5    | rm -f ./Podfile.lock | 0           |
| 6    | cocoapods            | 1           |
| 7    | sigh                 | 5           |
| 8    | build_app            | 47          |
| 9    | pgyer                | 8           |
+------+----------------------+-------------+

[21:42:44]: fastlane.tools finished successfully 🎉

经过一堆log日志输出后,会显示succes成功的提示;
在项目的 packets/路径下,能看到我们的ipa包已经打包成功了,ipa的命名方式是 版本号+ build号 +日期的格式;

image.png

在蒲公英平台上可以查看我们的ipa是否上传成功,这里我们贴一下通过 fastlane + pgyer 的方式上传成功的截图;

image.png

Fastlane + TestFlight 配置

除了蒲公英,我们绝大部分时候需要通过 App Connect网站上传Release版本的ipa,或者通过TestFlight分发内测包,这里我们提供一下 Fastlane + TestFlight 的配置, 当然这里还有一个坑点,就是有的苹果开发者账号开启了双重认证,需要额外做一些操作;

配置如下:

# 定义打包平台
default_platform :ios

   # 参数定义
   app_scheme = "[****scheme]"   
   app_workspace = "****.xcworkspace"
   app_identifier = "com.****.www"
   # 打包日期拼写版本号
   dateTime = Time.new.strftime("%Y%m%d")
   version = get_version_number #获取版本号
   buildNumber = get_build_number  # 获取build号
   fileName = "#{version}_#{buildNumber}_#{dateTime}.ipa"


   platform :ios do
      before_all do
      git_pull
      last_git_commit
      sh "rm -f ./Podfile.lock"
      cocoapods(use_bundle_exec: false)
   end
   
   # 提交一个新的Beta版本
   lane :beta do |options|
      
      sigh(app_identifier: app_identifier) #项目的bundle identifler    
      build_app(
         workspace: app_workspace,
         configuration: "Release",
         scheme: app_scheme,   
         silent: true,  #在构建时隐藏不必要输出的信息
         clean: true,  #在构建前是否clean工程
         include_bitcode:false,  #是否开启bitecode
         output_directory:"./packets" ,  #ipa的输出路径
         output_name:fileName,  #ipa的文件名(上面👆定义)
         export_method: "app-store",  #打包方式
         export_options: {
             # 描述文件匹配哪个 identifier 和 profile文件名 
            provisioningProfiles: { "com.****.www" => "描述文件名称"}
         }
      ) 
      
      #苹果账号鉴权
      api_key = app_store_connect_api_key(
        key_id: "C5P****KHG",
        issuer_id: "8a88fec8*************5ce96430a21",
        key_filepath: "./certificate/AuthKey_C5PG*****HG.p8",
        duration: 1200, # optional (maximum 1200)
        in_house: false # optional but may be required if using match/sigh
      )
   
      #上传到TestFlight
      upload_to_testflight(
         api_key: api_key,
         skip_waiting_for_build_processing: true,
         ipa: "./packets/#{fileName}",
         skip_submission:true
      )
   end

执行命令:

fastlane beta
执行结果:

在终端看到 执行success后,能在苹果开发者后台 https://appstoreconnect.apple.com/ 网站看到 我们刚才自动打包并上传的ipa;并且在TestFlight内测的用户会收到相应的通知;

image.png

苹果账号双重认证问题

关于苹果账号的双重认证问题,需要我们登陆Developer后台中生成一个API密钥,在App Store Connect -> 用户与访问 中进行配置;并把xxxxxxxx.p8 文件保存下来(xxxxxxxx.p8文件只能下载一次)

image.png

Question1 工程版本号 和build number 导致的问题

[17:33:41]: Before being able to increment and read the version number from your Xcode project, you first need to setup your project properly. Please follow the guide at https://developer.apple.com/library/content/qa/qa1827/_index.html

(CURRENT_PROJECT_VERSION)表示使用build Setting中的Version的(MARKETING_VERSION):表示使用build Setting中的Build的值;

配置如下:


image.png

Question2

+---------------------+-----------------------------------------------------+
|                               Lane Context                                |
+---------------------+-----------------------------------------------------+
| DEFAULT_PLATFORM    | ios                                                 |
| VERSION_NUMBER      | 23                                                  |
| BUILD_NUMBER        | 23                                                  |
| PLATFORM_NAME       | ios                                                 |
| LANE_NAME           | ios debug_pgyer                                     |
| CERT_FILE_PATH      | /Users/pengchao/Desktop/zheshi/ios_recoder/25KJ9FK  |
|                     | Q2Q.cer                                             |
| CERT_CERTIFICATE_ID | 25KJ9FKQ2Q                                          |
+---------------------+-----------------------------------------------------+
[18:03:59]: Could not find a matching code signing identity for type 'Development'. It is recommended to use match to manage code signing for you, more information on https://codesigning.guide. If you don't want to do so, you can also use cert to generate a new one: https://fastlane.tools/cert

解决办法:

总结+后续

关于一些不清楚的参数可以参考 fastlane 官网的文档,项目中首先保证自己这台Mac 的配置 是可以正常打包的,否则很容易遇到问题;

参考文章

https://docs.fastlane.tools
iOS Fastlane 使用文档
Fastlane的集成和使用 包括 ios/mac 项目示例
fastlane自动化打包iOS APP

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

推荐阅读更多精彩内容