Fastlane

635 阅读3分钟

应用上传方式

1. 苹果方法

  1. 直接通过 Xcode 上传 ipa
  2. 通过Xcode 自带开发工具 Application Loader (最新版本Xcode 已经不支持该方式)
  3. 通过altool上传ipa。
  4. 通过Transporter应用上传ipa。苹果新推出的应用
2. 其它方法
  1. Shell 脚本打包
  2. fastlane 快速打包
  3. Jenkins 可视化打包

Fastlane 打包

官方文档

参考文档

安装

cocoapods和fastlane一样,也是基于Ruby的开发脚本集合,所以看着很相似

  1. 首先安装正确的 Ruby 版本,在终端用命令行做确认
ruby -v
  1. 检查Xcode 命令行工具是否安装
xcode-select --install

  1. 以上依赖配置完成后就可以通过rubygem 进行安装
sudo gem install fastlane

  1. 安装成功后,cd 到你的工程目录,执行

fastlane init

基本文件介绍

初始化后./fastlane 文件 中有两个重要文件

1. Appfile

存放着 AppleID 或者 BundleID 等一些fastlane需要用到的信息。 基本上我们不需要改动这个文件的内容。

默认生成文件内容 :

app_identifier("[[APP_IDENTIFIER]]")  # The bundle identifier of your app
apple_id("[[APPLE_ID]]")              # Your Apple email address

2. 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
#

# 如果希望fastlane 自动更新取消下面一行注释
# Uncomment the line if you want fastlane to automatically update itself
 update_fastlane
default_platform(:ios)

platform :ios do

    #1.用于执行任务之前的操作
    before_all do 
    
    end

    #2.定义用户的主要任务流程,比如打包ipa,执行测试等
    desc "Runs all the tests" 
    lane :test do 
        scan
     end

    #3.用于执行任务之后的操作
    after_all do |lane|
    
    end 

    #4.用于发生错误的操作
    error do |lane, exception| 
        # slack( # message: "Error message" # )
    end
  
end

3. Fastlane
desc "Push a new beta build to TestFlight"   //该任务的描述
lane :beta do                                //定义名字为 beta 的任务

       //构建App
       build_app(
                 workspace: "expample.xcworkspace", 
                 scheme: "example")
       
       //上传到testfilght
       upload_to_testflight  
end

其中两个 Action

  • build_app 生成 ipa 文件
  • upload_to_testflight 把 ipa 文件上传到 TestFilght

在控制台执行

fastlane beta 

即可执行任务,按照上面的任务,会生成 ipa并上传到 TestFilght.

实践

编写自己的lane

desc "发布到Fir" 
lane :pulish_to_fir do

    cocoapods    # 运行 pod install
    
    # 构建和打包ipa
    gym( 
        clean            : true,           # 是否清空以前的编译信息
        output_directory : './firim',      # 打包文件导出目录
        scheme           : 'xxxx',
        silent           : false          # 隐藏没有必要的building信息
        configuration    : 'Debug',       # 指定打包方式,Release 或者 Debug
        export_xcargs    : "-allowProvisioningUpdates",    #访问钥匙串
        
        export_options   : {              # 导出选项列表或带有导出选项的散列路径
        
                             method               : 'development', 
                             provisioningProfiles : 
                             { 
                                "xxx.xxx.xxx"  : "match Development xxx.xxx.xxx" },
                              } 
         )
    
  #上传ipa到fir.im服务器,在fir.im获取firim_api_token 
  firim(firim_api_token: "fir_token")
  
  notification(title:   "发布成功!", 
    	       message: "已成功上传到蒲公英平台, 赶快联系测试人员开始测试吧!?", 
    	       open:    "https://www.pgyer.com")
              
  end
  desc "生产环境包"
  lane :pro do
  
  //时间函数
  currentTime = Time.new.strftime("%Y-%m-%d-%H-%M-")
  
  build_app(
            export_method    :  "ad-hoc",              # 用于导出归档的方法
            workspace        :  "EWDemo.xcworkspace",  # 工作区文件的路径
            scheme           :  "EWDemo",              # 确保它scheme被标记为Shared
            include_symbols  :  false,                 # ipa 包含符号
            output_directory :  "./fastlane"           # ipa 存储的目录
            
            output_name      : "#{currentTime}#{'EWDemo.ipa'}",
            
    
  system "open ../fastlane/build"   # 打开ipa的存放文件夹
  end

详解

cocoapods : Runs pod install for the project

gym : Alias for the build_app build_ios_app action 别名,而它本身则是xcodebuild工具的封装

注意
app-store,    #AppStore正式生产环境包
ad-hoc,       #生产测试包
enterprise,   #企业包(299美刀账号)
development   #开发测试包

Fastlane 打包上蒲公英

  1. 安装蒲公英的Fastlane 插件
fastlane add_plugin pgyer
  1. 进入项目目录 ,初始化Fastlane
fastlane init

  1. 修改fastfile 文件
lane :beta do
  build_app(export_method: "ad-hoc")
  pgyer(api_key: "7f15xxxxxxxxxxxxxxxxxx141", user_key: "4a5bcxxxxxxxxxxxxxxx3a9e")
end

lane :beta do
  build_app(export_method: "ad-hoc")
  
  api_key = "aaaaaaa"
  user_key = "bbbbbb"
  
  pgyer(api_key:"#{api_key}",user_key:"#{user_key}")
 
 end

Xcode 8.3 和 Xcode 8.3 以后的版本中, export_method 的值,需要根据开发者的打包类型进行设置,可选的值有app-store、ad-hoc、development、enterprise

  1. 执行fastlane 命令
fastlane beta

Fastlane 打包上传Firim

  1. 安装Firim的Fastlane 插件
fastlane add_plugin firim

gem install fir-cli
  1. 在Fastfile 文件中增上传
puts "**************** | 开始上传 firim | ****************"

   firim(firim_api_token: "1ea0d****0083740a1****f741****3b") 

 puts "**************** | 开始上传 firim | ****************"

注意事项

可以用fastlane actions [firim] 检查这个action是否安装有效,同时也会显示当前所有插件

iShot2021-06-19 17.06.28.png