iOS 利用fastlane打包上传到TestFlight

4,341 阅读4分钟

作为一个iOS开发一定会经历的阶段就是提测时打包所带来的折磨,无论是打包到App Store connect还是蒲公英这样的分发平台。尤其是在测试反馈阶段,为了一个bug可能要反复打包好多次。那现在就用fastlane来搞一下,实现一键上传。

未解决大家的实际应用需求,不做过多介绍,直奔主题。

fastlane的安装

fastlane的安装方式有很多种,官方网站中也有介绍。

配置Xcode

Xcode command line tools (macOS)

xcode-select --install

安装 fastlane

安装fastlane有很多方式,官方推荐用Bundler。也可以用Homebrew,不过由于对于系统Ruby环境的依赖,可能存在一些比较难处理的冲突。不过我还是用了这种方式安装,毕竟现在几乎所有的问题都能在网络上找到好的解决办法,就看你自己喜欢吧。

Managed Ruby environment + Bundler (macOS/Linux/Windows)

Ruby

官方是不推荐这种方式的,fastlane支持Ruby 2.5以上版本。

查看Ruby版本

$ ruby --version
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]

Bundler

推荐使用Bundler 和 Gemfile 定义对fastlane的依赖关系。定义要使用的fastlane版本及其依赖项,快速执行fastlane

  • 安装 Bundler  gem install bundler
  • 在项目目录下创建./Gemfile 文件,内容如下
source "https://rubygems.org"

gem "fastlane"
  • 运行 bundle update 将 ./Gemfile 和 ./Gemfile.lock 添加到版本控制中
  • 运行fastlane时使用 bundle exec fastlane [lane]
  • 在你的CI机器上第一次运行时添加bundle install命令
  • 运行bundle update fastlane更新fastlane

Homebrew (macOS)

这种方式安装不需要单独安装Ruby,homebrew会为fastlane安装合适的Ruby。

brew install fastlane

System Ruby + RubyGems (macOS/Linux/Windows)

sudo gem install fastlane

配置 fastlane

在项目根目录下执行

fastlane init

执行中会有一些相关的配置包括要上传的Apple ID和密码,这里通过这种方式有一个麻烦的点在于如果你开启了二次认证,会通过预留的手机号发送验证码,这个就比较麻烦,那我们不用这种方式。

执行完毕会在目录下生成fastlane文件夹

其中 fastlane/Fastfile就是我们将要配置的文件了,待会会讲。

设置一下环境变量

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

到目前为止基本的安装配置就完成了,下面进行Fastfile的配置,通过配置来完成打包和上传工作,这里以上传到App Store connect为例。

配置falstfile

# fastlane版本
fastlane_version = "2.199.0"

default_platform :ios


ENV["FASTLANE_XCODEBUILD_SETTINGS_TIMEOUT"] = "30"

ENV["FASTLANE_XCODEBUILD_SETTINGS_RETRIES"] = "20"



platform :ios do


    desc 'Deploy a new version to the App Store'
    # release为使用fastlane时的命令,调用 `fastlane release`开始执行
    # options为参数,当前未传入参数
    lane :release do |options|
    # gym:Alias for the `build_app` action,build_app的别名
    gym(
        # 每次打包之前clean一下
        clean: true,
        # 打包出ipa文件路径,后边上传的时候也会用到
        output_directory: './fastlane/release',
        # 打包的名称,换成自己的
        output_name: 'IPAName.ipa',
        # 项目的scheme,换成自己的
        scheme: "yourproscheme",
        # 默认Release,Release or Debug
        configuration: 'Release',
        # 是否包含bitcode
        include_bitcode: true,
        # 是否包含symbols
        include_symbols: true,
        # 打包导出方式,包含app-store, validation, ad-hoc, package, enterprise, development, developer-id and mac-application
        export_method: 'app-store',
        # 这个设置是为了设置xcode自动配置证书和配置文件,当然也可以手动配置,可以参考文档
        export_xcargs: '-allowProvisioningUpdates'

    )
    # mac上的通知弹窗,通知打包完毕
    notification(app_icon: './fastlane/icon.png', title: 'manager', subtitle: '打包成功,已导出安装包', message: '准备发布中……')
    # 配置上传到App Store connect的api_key
    # 通过这种设置可以绕过二次认证等验证操作,实现一键打包上传
    api_key = app_store_connect_api_key(
        # 通过苹果申请的key id,申请同时会生成issuer_id和一个授p8权文件,就是以下参数,文件只能下载一次,注意保存,申请方式https://appleid.apple.com/account/manage中的专用密钥
        key_id: 'your key_id',

        issuer_id: 'your issuer id',
        # 授权文件路径
        key_filepath: './fastlane/AuthKey_yours.p8',

        duration: 1200,

        in_house: false

    )

    # 上传到testflight
    upload_to_testflight(
        # 上边设置的授权信息
        api_key: api_key,

        skip_waiting_for_build_processing: true,
        # 打包好要上传的文件
        ipa: './fastlane/release/IPAName.ipa',

        skip_submission: true

    )
    # 通知上传成功
    notification(app_icon:"icon.png",title:"LoanManager",subtitle: "IPA上传成功", message: "自动打包完成!")

    

    end


end

使用当前命令打包 fastlane release等待就好了,不过因为配置问题可能会存在不同的问题,但问题不大,都可以找到对应的解决方法,领附fastlane在github上的issues

除了当前的使用方法之外,还包含版本控制相关的actions,比如一键commit


    desc 'git add . and git commit -m message'

    lane :commit do |options|

    message = options[:message]

    git_add(path:'.')

    git_commit(path: '.', message: message)

    UI.message("完成commit")

    end

简单的git add 和 git commit操作,其中message就可以通过 fastlane commit message: 'your commit message'来传递,可以有多个参数。

这就是一些简单的操作,可以通过更多的组合来实现更多的自动化命令,比如commit后合并到master分支并push到远程仓库等,建议多看看官方文档给出的actions.

本文只做入门学习之用,作者也是个小白,希望大家可以多多交流共同进步。