fastlane自动化打包iOS APP过程示例

概述 APP自动化打包常见的主流工具有Jenkins、fastlane。Jenkins功能强大,但是需要的配置也比较多,团队较大的可以优先考虑,fastlane

概述

APP自动化打包常见的主流工具有JenkinsfastlaneJenkins功能强大,但是需要的配置也比较多,团队较大的可以优先考虑,fastlane是用Ruby语言编写的一套自动化工具集,比较轻便,配置简单,使用起来也很方便。本文会详细的介绍fastlane从安装到上传APP到蒲公英的整个流程。

fastlane的安装

第一步

因为fastlane是用Ruby语言编写的工具,所以必须保证已经配置好了Ruby开发环境。可以使用如下命令行查看是否安装了Ruby:

ruby -v

如果有以下提示说明,你已经安装了Ruby:

ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-darwin21]

如果没有安装需要先安装ruby,本文对安装ruby不作教程说明。

第二步

安装Xcode命令行工具

xcode-select --install

如果没有安装,命令会有提示框,根据提示一步一步安装即可。 如果出现以下命令提示,说明已经安装成功:

xcode-select: error: command line tools are already installed, use "Software Update" to install updates

第三步:

Ruby和Xcode环境都配置好之后,执行以下命令来安装fastlane:

sudo gem install -n /usr/local/bin fastlane

安装完成之后,输入以下命令查看是否安装成功:

fastlane --version

出现以下提示说明你已经安装成功了:

fastlane installation at path:
/Users/xxxxxx/.rvm/gems/ruby-2.7.0/gems/fastlane-2.206.1/bin/fastlane
-----------------------------
[✔]  
fastlane 2.206.1

fastlane的配置

到你的iOS项目下,执行初始化命令:

fastlane init

命令执行完成之后会给出我们如下几个提示:

[✔]  
[✔] 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
?  

命令执行到最后有What would you like to use fastlane for?提示,此时fastlane列出几个选项,需要我们告诉它使用fastlane需要执行哪种操作:

  • 第一种获取App Store的App预览照片。
  • 第二种打包上传至TestFlight工具上。
  • 第三种打包上传到App Store。
  • 第四种自定义打包方式。

以上四种方式根据自己的需要自行选择,本文主要介绍打包上传至第三方平台,所以选择4自定义打包方式。选择完成之后,fastlane会在我们项目中创建fastlane文件

[16:54:32]: ------------------------------------------------------------
[16:54:32]: --- Setting up fastlane so you can manually configure it ---
[16:54:32]: ------------------------------------------------------------
[16:54:32]: Installing dependencies for you...
[16:54:32]: $ bundle update
[16:54:40]: --------------------------------------------------------
[16:54:40]: --- ✅  Successfully generated fastlane configuration ---
[16:54:40]: --------------------------------------------------------
[16:54:40]: Generated Fastfile at path `./fastlane/Fastfile`
[16:54:40]: Generated Appfile at path `./fastlane/Appfile`
[16:54:40]: Gemfile and Gemfile.lock at path `Gemfile`
[16:54:40]: Please check the newly generated configuration files into git along with your project
[16:54:40]: This way everyone in your team can benefit from your fastlane setup
[16:54:40]: Continue by pressing Enter ⏎

创建好fastlane文件夹之后,Appfile是编辑我们相关App和开发者账号信息的,一般不需要我们去手动修改。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

打包并自动上传 App 到蒲公英

安装蒲公英的 fastlane 插件

fastlane add_plugin pgyer

编辑Fastlane 的配置文件 fastlane/Fastfile

lane :develop do
   target = "demo"
   configuration = "Debug"
   gym(scheme: target, configuration: configuration, export_method:"development")
   pgyer(api_key: "7f15xxxxxxxxxxxxxxxxxx141", user_key: "4a5bcxxxxxxxxxxxxxxx3a9e")
end

以下是蒲公英平台的说明:

  • 以上的 api_keyuser_key,请开发者在自己账号下的 应用管理 - App概述 - API 中可以找到,并替换到以上相应的位置。
  • 在 Xcode 8.3 和 Xcode 8.3 以后的版本中,对于 build_appexport_method 的值,需要根据开发者的打包类型进行设置,可选的值有:app-storead-hocdevelopmententerprise。对于 Xcode 8.3 以下的版本,则不需要设置 export_method

经过以上配置后,就可以使用 Fastlane 来打包 App,并自动上传到蒲公英了。在终端下,定位到项目所在目录,输入以下命令即可:

fastlane develop

在成功的情况下,可以看到类似下面的信息:

[18:37:22]: Successfully exported and compressed dSYM file
[18:37:22]: Successfully exported and signed the ipa file:
[18:37:22]: /Users/xxx/Documents/workCode/xxxxx.ipa
[18:37:22]: -------------------
[18:37:22]: --- Step: pgyer ---
[18:37:22]: -------------------
[18:37:22]: The pgyer plugin is working.
[18:37:22]: build_file: /Users/dddd/Documents/workCode/xxxx.ipa
[18:37:22]: Start upload /Users/dddd/Documents/workCode/xxx.ipa to pgyer...
[18:37:43]: Upload success. Visit this URL to see: https://www.pgyer.com/xxxxx

+------+------------------+-------------+
|           fastlane summary            |
+------+------------------+-------------+
| Step | Action           | Time (in s) |
+------+------------------+-------------+
| 1    | default_platform | 0           |
| 2    | gym              | 61          |
| 3    | pgyer            | 20          |
+------+------------------+-------------+

[18:37:43]: fastlane.tools finished successfully

  • 设置一个版本更新时的描述信息:
lane :develop do
  build_app(export_method: "development")
  pgyer(api_key: "7f15xxxxxxxxxxxxxxxxxx141", user_key: "4a5bcxxxxxxxxxxxxxxx3a9e", update_description: "update by fastlane")
end

打包上传到Testflight

在Fastfile文件中加入下面命令:

desc "打包上传到Testflight"
  lane :beta do
    # add actions here: https://docs.fastlane.tools/actions
   target = "demo"
   configuration = "Release"
   gym(scheme: target, configuration: configuration, export_method:"app-store")
   upload_to_testflight(
 	    username: "xxxx@dd.com",
            app_identifier: "com.xxxx",
          )

在命令行输入:

fastlane beta

打包上传的过程中会让你输入苹果账号的密码,并且有可能会报下面的错:

[00:20:28]: Login successful
[00:20:31]: Ready to upload new build to TestFlight (App: fffff)...
[00:20:40]: Transporter transfer failed.
[00:20:40]: 
[00:20:40]: Please sign in with an app-specific password. You can create one at appleid.apple.com. (-22910)
[00:20:41]: 
[00:20:41]: Your account has 2 step verification enabled
[00:20:41]: Please go to https://appleid.apple.com/account/manage
[00:20:41]: and generate an application specific password for
[00:20:41]: the iTunes Transporter, which is used to upload builds
[00:20:41]: 
[00:20:41]: To set the application specific password on a CI machine using
[00:20:41]: an environment variable, you can set the
[00:20:41]: FASTLANE_APPLE_APPLICATION_SPECIFIC_PASSWORD variable

这是因为你的APPLE账号开启了双重验证,注意这句提示Please sign in with an app-specific password用app专用密码登录,提示你创建一个app专用密码登录Transporter,关于怎么创建app专用密码本人不做讲解,可以自行百度或者谷歌,使用app专用账号即可解决此问题。

以上就是fastlane自动化打包iOS APP过程示例的详细内容,更多关于fastlane打包iOS APP的资料请关注好代码网其它相关文章!