iOS Fastlane 自动化打包初探

2023年6月21日更新~

一、前言

每次打包上传总是要耗费我们一些时间,并且都是些没有技术含量的重复性工作,如果能实现自动打包,我们岂不是可以节省很多时间做其他的事情,心里美滋滋,那么今天我们就来做一做这件事情,实现一个命令完成打包上传工作。
学习完本文,您还可以继续阅读 iOS Fastlane 自动化打包进阶

fastlane.png

二、Fastlane 简介

Fastlane是一款为 iOS 和 Android 开发者提供的自动化构建工具,它可以帮助开发者将 App 打包、签名、测试、发布、信息整理、提交 App Store 等工作完整的连接起来,实现完全自动化的工作流,如果使用得当,可以显著的提高开发者的开发效率。

三、环境搭建

请参考:官方安装指南

  • 1、检查ruby版本 要求大于2.0.0
$ ruby -v
  • 2、检查 Xcode CLT 是否安装
$ xcode-select --install
  • 3、安装fastlane
$ gem install fastlane -NV

安装成功后就可以为项目配置fastlane 了

四、项目配置

  • 1、为项目配置 fastlane
$ cd 项目目录
$ fastlane init

初始化时,会出现4个选项:
Automate screenshots(自动化截图)
Automate beta distribution to TestFlight(TestFlight)
Automate App Store distribution (AppStore发布版本)
Manual setup - manually setup your project to automate your tasks(自定义)

直接输入1、2、3、4 选择你想创建的类型
中间会让输入苹果开发者账号和密码,之后会在你项目工程的目录下生成一个fastlane文件夹,里面有Fastlane的配置文件,一个是Appfile文件,一个是Fastfile文件(如果要上传AppStore的话还有Deliverfile文件)。

  • Appfile保存苹果开发者的相关信息、项目的相关信息等。
  • Fastfile是运行脚本。


    init

    生成的文件信息


    执行结束后产生的文件

五、上传蒲公英

  • 1、创建好faselane文件之后,安装蒲公英插件,注意:是在项目目录下安装该插件
//在项目目录下执行 
$ fastlane add_plugin pgyer
  • 2、修改Fastfile 内容
    打开自动生成的Fastfile 我们可以看到
# 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
# update_fastlane

default_platform(:ios)

platform :ios do
  desc "Description of what the lane does"
  lane :custom_lane do
    # add actions here: https://docs.fastlane.tools/actions
  end
end

将对应的地方更改如下

lane :beta do
  build_app(export_method: "ad-hoc")
  pgyer(api_key: "7f15xxxxxxxxxxxxxxxxxx141")
end

注意:
以上的 api_key ,在蒲公英自己账号下的个人头像 - API信息 中可以找到,并替换到以上相应的位置。
在 Xcode 8.3 和 Xcode 8.3 以后的版本中,对于 build_app 的 export_method 的值,需要根据开发者的打包类型进行设置,可选的值有:app-store、ad-hoc、development、enterprise。对于 Xcode 8.3 以下的版本,则不需要设置 export_method。

  • 3、打包并自动上传 App 到蒲公英
//在项目目录下执行
$ fastlane beta
执行结果

等待一段时间后上传蒲公英成功,大功告成!!!

六、 自定义lane,实现更多功能✨

这里推荐Sublime 选择ruby语言进行编辑,并且掌握一些简单的ruby语法为好,这里我们使用gym(iOS和Android应用程序自动化测试版部署和发布的最简单方法),gym和上面的build_app功能一样,是他的别名。
关于gym一些Parameters的介绍请参考gym

# 增加一个超时时长,要不然老报请求超时
ENV["FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT"] = "120"
default_platform(:ios)

platform :ios do 
  desc "上传蒲公英" 
  lane : topgyer do #topgyer 为lane 名称,我们执行时就是执行这个方法
 scheme_name = "TestDemo1" 

  #导出路径  我们可以桌面创建IPA_Info(没有的话会自动创建) 文件夹来集中管理生成的ipa等文件
  output_directory = File.expand_path("~/Desktop/IPA_Info")

  #导出名称 
  output_name = "#{scheme_name}_#{Time.now.strftime('%Y%m%d%H%M%S')}.ipa"
 
 gym(
    export_method: "ad-hoc", #这里填写导出方式 ad-hoc、enterprise、app-store  
   #Xcode 9 默认不允许访问钥匙串的内容,必须要设置此项才可以
    export_xcargs: "-allowProvisioningUpdates", 
    scheme: scheme_name,# target的名字
    clean: true, # 在构建前先clean
    output_directory: output_directory, #ipa输出目录
    output_name: output_name#ipa名字
) 
  # 上传蒲公英
  pgyer(api_key: "e4xxxxxxxd2863db4136227b2f3ba4") 
 end
end

// 在项目根目录执行
$ fastlane topgyer

祝你成功,更多获取lane可跳转。

七、ReactNative 项目增加iOS端自动打包指令整理✨

# 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
# update_fastlane

# 增加一个超时时长,要不然老报请求超时
ENV["FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT"] = "120"
# 蒲公英账号的api_key
pyger_api_key = "xxxxxxxxfa85174586b165aexxxxxxxx" 
# 要打包的应用的scheme名称
scheme_name = "xxxxx"

default_platform(:ios)

platform :ios do
  desc "打包adhoc,并上传蒲公英"
  lane :pgy do
    # 重新生成jsbundle包,RN专用,这个bundle-ios指令应该是项目中配置的
   # 原始指令是:react-native bundle --entry-file index.js --platform ios --dev false --bundle-output ios/schemeName/main.jsbundle --assets-dest ios/schemeName/
    sh("yarn bundle-ios")

    # 上传地址,这里是测试包导出,所以选择ad-hoc
    export_method = "ad-hoc"

    project = "./" + scheme_name + ".xcodeproj"
    workspace = "./" + scheme_name + ".xcworkspace"

    # build加1
    # increment_build_number(xcodeproj: project)
    # 获取最新的build号码
    # build_number = get_build_number(xcodeproj: project)
    # 获取应用版本号
    versoin_number = get_version_number(
      xcodeproj: project,
      target: scheme_name
    )
    # 拼接打包文件名称
    output_name = "#{scheme_name}_#{versoin_number}_#{Time.now.strftime('%Y%m%d%H%M%S')}.ipa"

    #导出路径  我们可以桌面创建IPA_Info(没有的话会自动创建) 文件夹来集中管理生成的ipa等文件
    output_directory = File.expand_path("~/Desktop/IPA_Info")

    # 打包
    gym( # build_app的别名
      workspace: workspace, 
      scheme: scheme_name, 
      export_method: export_method, 
      output_directory: output_directory,
      output_name: output_name,
      clean: true, # 每次打包前清理项目
      suppress_xcode_output: true, # 打包过程中不显示xcode的输出信息
    )
    pgyer(api_key: pyger_api_key)
  end
end

gym部分参数

image.png

学习完本文,您还可以继续阅读 iOS Fastlane 自动化打包进阶

文章参考

  • 1、fastlane官方文档
  • 2、iOS自动化打包发布Jenkins + Fastlane + GitLab + 蒲公英
  • 3、Mac下使用Jenkins踩坑 Fastlane自动化iOS打包
  • 4、持续集成-第一篇 fastlane
  • 5、自动化工具Fastlane笔记一: 安装, 打包,上传

你可能感兴趣的:(iOS Fastlane 自动化打包初探)