概述

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

以下是蒲公英渠道的阐明:

  1. 以上的 api_keyuser_key,请开发者在自己账号下的 运用管理App概述API 中能够找到,并替换到以上相应的位置。

  2. 在 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专用账号即可处理此问题。