使用fastlane自动化部署APP

fastlane运行所需要的环境:

  1. OS X 10.9以上
  2. Ruby 2.0 以上
  3. Xcode
  4. 拥有一个开发者账号

因为fastlane其实是一个Ruby脚本的集合,你必须安装正确的Ruby版本。可用一下命令确认

安装

1、ruby -v 查看ruby版本
2、然后检查Xcode命令行工具(CLT)是否安装。在终端中输入命令:

xcode-select --install

如果已经安装,则会报一下错误

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

如果没安装,终端会开始安装CLT。

3、通过rubygem安装fastlane

sudo gem install fastlane -v 2.57.2 --verbose

初始化

打开终端,cd到你的工程目录,然后执行fastlane init
运行结果如下

fastlane init
[15:35:29]: Seems like launching fastlane takes a while - please run
[15:35:29]:
[15:35:29]: $ [sudo] gem cleanup
[15:35:29]:
[15:35:29]: to uninstall outdated gems and make fastlane launch faster
[15:35:29]: Alternatively it's recommended to start using a Gemfile to lock your dependencies
[15:35:29]: To get started with a Gemfile, run
[15:35:29]:
[15:35:29]: $ bundle init
[15:35:29]: $ echo 'gem "fastlane"' >> Gemfile
[15:35:29]: $ bundle install
[15:35:29]:
[15:35:29]: After creating the Gemfile and Gemfile.lock, commit those files into version control
[15:35:29]: Get started using a Gemfile for fastlane https://docs.fastlane.tools/getting-started/ios/setup/#use-a-gemfile
[15:35:32]: Detected iOS/Mac project in current directory...
[15:35:32]: This setup will help you get up and running in no time.
[15:35:32]: fastlane will check what tools you're already using and set up
[15:35:32]: the tool automatically for you. Have fun!
[15:35:32]: Created new folder './fastlane'.
[15:35:32]: $ xcodebuild -list -project ./JenkinsDemo.xcodeproj
2017-12-07 15:35:33.629 xcodebuild[39937:3765993] [MT] PluginLoading: Required plug-in compatibility UUID DF11C142-1584-4A99-87AC-1925D5F5652A for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/FuzzyAutocomplete.xcplugin' not present in DVTPlugInCompatibilityUUIDs
[15:35:34]: $ xcodebuild -showBuildSettings -scheme JenkinsDemo -project ./JenkinsDemo.xcodeproj
2017-12-07 15:35:34.373 xcodebuild[39940:3766165] [MT] PluginLoading: Required plug-in compatibility UUID DF11C142-1584-4A99-87AC-1925D5F5652A for plug-in at path '~/Library/Application Support/Developer/Shared/Xcode/Plug-ins/FuzzyAutocomplete.xcplugin' not present in DVTPlugInCompatibilityUUIDs
[15:35:35]: Your Apple ID (e.g. [email protected]): 这里是你的Apple ID
[15:35:49]: Verifying that app is available on the Apple Developer Portal and iTunes Connect...
[15:35:49]: Starting login with user '这里是你的Apple ID'

+----------------+--------------------------------------+
|                    Detected Values                    |
+----------------+--------------------------------------+
| Apple ID       | 这里是你的Apple ID                   |
| App Name       | JenkinsDemo                          |
| App Identifier | com.jenkin.Debug                     |
| Project        | /Users/xyj/Desktop/test/JenkinsDemo  |
|                | .xcodeproj                           |
+----------------+--------------------------------------+

[15:36:00]: Please confirm the above values (y/n)
y
[15:36:11]: Created new file './fastlane/Appfile'. Edit it to manage your preferred app metadata information.
[15:36:11]: Loading up 'deliver', this might take a few seconds
[15:36:11]: Login to iTunes Connect ([email protected])
[15:36:14]: Login successful

+--------------------------------------+------------------------+
|                    deliver 2.68.2 Summary                     |
+--------------------------------------+------------------------+
| run_precheck_before_submit           | false                  |
| screenshots_path                     | ./fastlane/screenshots |
| metadata_path                        | ./fastlane/metadata    |
| username                             | 这里是你的Apple ID     |
| app_identifier                       | com.jenkin.Debug       |
| edit_live                            | false                  |
| platform                             | ios                    |
| skip_binary_upload                   | false                  |
| skip_screenshots                     | false                  |
| skip_metadata                        | false                  |
| skip_app_version_update              | false                  |
| force                                | false                  |
| submit_for_review                    | false                  |
| automatic_release                    | false                  |
| dev_portal_team_id                   | 99WG99HF75             |
| overwrite_screenshots                | false                  |
| precheck_default_rule_level          | warn                   |
| ignore_language_directory_validatio  | false                  |
| n                                    |                        |
| precheck_include_in_app_purchases    | true                   |
+--------------------------------------+------------------------+

[15:36:17]: Writing to './fastlane/metadata/zh-Hans/description.txt'
[15:36:17]: Writing to './fastlane/metadata/zh-Hans/keywords.txt'
[15:36:17]: Writing to './fastlane/metadata/zh-Hans/release_notes.txt'
[15:36:17]: Writing to './fastlane/metadata/zh-Hans/support_url.txt'
[15:36:17]: Writing to './fastlane/metadata/zh-Hans/marketing_url.txt'
[15:36:17]: Writing to './fastlane/metadata/zh-Hans/promotional_text.txt'
[15:36:17]: Writing to './fastlane/metadata/zh-Hans/name.txt'
[15:36:17]: Writing to './fastlane/metadata/zh-Hans/subtitle.txt'
[15:36:17]: Writing to './fastlane/metadata/zh-Hans/privacy_url.txt'
[15:36:17]: Writing to './fastlane/metadata/copyright.txt'
[15:36:17]: Writing to './fastlane/metadata/primary_category.txt'
[15:36:17]: Writing to './fastlane/metadata/secondary_category.txt'
[15:36:17]: Writing to './fastlane/metadata/primary_first_sub_category.txt'
[15:36:17]: Writing to './fastlane/metadata/primary_second_sub_category.txt'
[15:36:17]: Writing to './fastlane/metadata/secondary_first_sub_category.txt'
[15:36:17]: Writing to './fastlane/metadata/secondary_second_sub_category.txt'
[15:36:17]: Writing to './fastlane/metadata/trade_representative_contact_information/trade_name.txt'
[15:36:17]: Writing to './fastlane/metadata/trade_representative_contact_information/address_line1.txt'
[15:36:17]: Writing to './fastlane/metadata/trade_representative_contact_information/city_name.txt'
[15:36:17]: Writing to './fastlane/metadata/trade_representative_contact_information/country.txt'
[15:36:17]: Writing to './fastlane/metadata/trade_representative_contact_information/postal_code.txt'
[15:36:17]: Writing to './fastlane/metadata/trade_representative_contact_information/is_displayed_on_app_store.txt'
[15:36:17]: Writing to './fastlane/metadata/review_information/first_name.txt'
[15:36:17]: Writing to './fastlane/metadata/review_information/last_name.txt'
[15:36:17]: Writing to './fastlane/metadata/review_information/phone_number.txt'
[15:36:17]: Writing to './fastlane/metadata/review_information/email_address.txt'
[15:36:17]: Writing to './fastlane/metadata/review_information/demo_user.txt'
[15:36:17]: Writing to './fastlane/metadata/review_information/demo_password.txt'
[15:36:17]: Writing to './fastlane/metadata/review_information/notes.txt'
[15:36:17]: Successfully created new configuration files.
[15:36:17]: Downloading all existing screenshots...
[15:36:19]: Successfully downloaded all existing screenshots
[15:36:19]: Successfully created new Deliverfile at path './fastlane/Deliverfile'
[15:36:19]: 'snapshot' not enabled.
[15:36:19]: 'cocoapods' not enabled.
[15:36:19]: 'carthage' not enabled.
[15:36:19]: Created new file './fastlane/Fastfile'. Edit it to manage your own deployment lanes.
[15:36:19]: fastlane will collect the number of errors for each action to detect integration issues
[15:36:19]: No sensitive/private information will be uploaded
[15:36:19]: Learn more at https://github.com/fastlane/fastlane#metrics
[15:36:19]: Successfully finished setting up fastlane

上述过程可能要输密码,将Apple ID的密码正确输入就可以了。

初始化完成之后,工程目录下就多了个fastlane文件夹,内容如下


使用fastlane自动化部署APP_第1张图片
sdf.png

这里肯定会被创建的是Appfile和Fastfile。如果Deliverfile,screenshots和metadata目录没被创建,可以运行deliver init来创建。

  • Appfile :用来存储一些公共信息,比如app_identifierapple_idteam_iditc_team_id等。

  • Fastfile:用来定义所有的lane任务
    一开始的内容如下:

# Customize this file, documentation can be found here:
# https://docs.fastlane.tools/actions/
# All available actions: https://docs.fastlane.tools/actions
# can also be listed using the `fastlane actions` command

# Change the syntax highlighting to Ruby
# All lines starting with a # are ignored when running `fastlane`

# If you want to automatically update fastlane if a new version is available:
# 自动更新fastlane
# update_fastlane

# This is the minimum version number required.
# Update this, if you use features of a newer version
# 需要的fastlane的最小版本,在每次执行之后会检查是否有新版本,如果有会在最后末尾追加新版本提醒
fastlane_version "2.68.2"

# 默认使用平台是ios,也就是说可以定义多个平台
default_platform :ios

# 执行所有的lane语句之前要做的事情,
platform :ios do
  before_all do
    # 自定义变量
    # ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."
    # 是否使用cocoapods构建项目
    # cocoapods
    # carthage
  end

  # 跑一遍测试
  desc "Runs all the tests"
  lane :test do
    run_tests
  end

  # 提交一个新的beta版本到 Apple TestFlight
  desc "Submit a new Beta Build to Apple TestFlight"
  desc "This will also make sure the profile is up to date"
  lane :beta do
    # match(type: "appstore") # more information: https://codesigning.guide
    build_app(scheme: "JenkinsDemo") # more options available
    upload_to_testflight

    # sh "your_script.sh"
    # You can also use other beta testing services here (run `fastlane actions`)
  end

  # 部署一个新版本到App store
  desc "Deploy a new version to the App Store"
  lane :release do
    # sync_code_signing(type: "appstore")
    capture_screenshots
    build_app(scheme: "JenkinsDemo") # Build your app - more options available
    upload_to_app_store(force: true)
    # frame_screenshots
  end

  # 你也可以定义自己的lane
  # You can define as many lanes as you want

  # 执行lane之后的回调
  after_all do |lane|
    # This block is called, only if the executed lane was successful

    # slack(
    #   message: "Successfully deployed new App Update."
    # )
  end
  
  # 流程异常,结束流程并输出错误信息
  error do |lane, exception|
    # slack(
    #   message: exception.message,
    #   success: false
    # )
  end
end


# More information about multiple platforms in fastlane: https://docs.fastlane.tools/advanced/#control-configuration-by-lane-and-by-platform
# All available actions: https://docs.fastlane.tools/actions

# fastlane reports which actions are used. No personal data is recorded.
# Learn more at https://docs.fastlane.tools/#metrics

在 fastlane 这个大家庭中,主要但包括不尽于下列工具:

  • scan 自动化测试工具,很好的封装了 Unit Test
  • sigh: 创建、更新、下载和修复 provisioning profiles。
  • match 同步团队每个人的证书和 Provision file 的超赞工具
  • cert: 自动创建和维护 iOS 代码签名证书。
  • gym: 创建和打包 iOS app。
  • deliver: 上传屏幕截图、元数据和 App 到 App 商店。
  • produce: 创建可用于 iTunes Connect 和 Apple Developer Portal 的 iOS app。
  • snapshot: 自动将 App屏幕截图本地化到每种设备上。
  • frameit: 将屏幕截图适配到适当的设备屏幕大小。
  • PEM: 自动创建和更新 Push 通知的 profile。

fastlane 实战

一、打一个测试包,并上传到蒲公英交付测试.

1、创建一个匹配开发证书的lane

说明:其中一个lane就是一个任务,里面是一个个的action组成的工作流

打开fastlane文件夹下的Fastfile文件(最好用xcode打开)。

创建一个lane:格式如下

desc "匹配开发证书"
lane :matchDev do
match(type: "development", keychain_password: "git")
end

说明:主要用到了match工具,其中type是指定环境,keychain_password,服务器钥匙串密码,match工具当然不止这些参数,想了解更多参数可以在终端执行fastlane action match,或者fastlane match --help来查看文档。

创建好了,在终端执行如下:

fastlane matchDev
[17:55:34]: Get started using a Gemfile for fastlane https://docs.fastlane.tools/getting-started/ios/setup/#use-a-gemfile
[17:55:38]: -------------------------------------------------
[17:55:38]: --- Step: Verifying required fastlane version ---
[17:55:38]: -------------------------------------------------
[17:55:38]: Your fastlane version 2.68.2 matches the minimum requirement of 2.68.2  ✅
[17:55:38]: ------------------------------
[17:55:38]: --- Step: default_platform ---
[17:55:38]: ------------------------------
[17:55:38]: Driving the lane 'ios matchDev' 
[17:55:38]: -------------------
[17:55:38]: --- Step: match ---
[17:55:38]: -------------------

+-----------------------+------------------------+
|            Summary for match 2.68.2            |
+-----------------------+------------------------+
| type                  | development            |
| keychain_password     | ********               |
| git_branch            | master                 |
| app_identifier        | com.jenkin.Debug       |
| username              | 这里显示的是开发者账号 |
| keychain_name         | login.keychain         |
| readonly              | false                  |
| team_id               | 99WG99HF75             |
| verbose               | false                  |
| force                 | false                  |
| skip_confirmation     | false                  |
| shallow_clone         | false                  |
| clone_branch_directly | false                  |
| force_for_new_devices | false                  |
| skip_docs             | false                  |
| platform              | ios                    |
+-----------------------+------------------------+

[17:55:38]: To not be asked about this value, you can specify it using 'git_url'
[17:55:38]: URL to the git repo containing all the certificates: [email protected]:repositories/test.git
[17:57:00]: Cloning remote git repo...
[17:57:00]: If cloning the repo takes too long, you can use the `clone_branch_directly` option in match.
[17:57:00]:   Successfully decrypted certificates repo
[17:57:00]: Verifying that the certificate and profile are still valid on the Dev Portal...
[17:57:05]: Couldn't find a valid code signing identity in the git repo for development... creating one for you now

+-------------------+------------------------------------------------+
|                      Summary for cert 2.68.2                       |
+-------------------+------------------------------------------------+
| development       | true                                           |
| force             | true                                           |
| username          | 这里显示的是开发者账号                         |
| team_id           | 99WG99HF75                                     |
| keychain_path     | /Users/xyj/Library/Keychains/login.keychain-db |
| keychain_password | ********                                       |
| platform          | ios                                            |
+-------------------+------------------------------------------------+

[17:57:05]: Starting login with user '这里显示的是开发者账号'
[17:57:08]: Successfully logged in
[17:57:15]: Successfully generated FQV3SGXTZ9 which was imported to the local machine.
[17:57:17]: Verifying the certificate is properly installed locally...
[17:57:17]: Successfully installed certificate FQV3SGXTZ9

+-------------------------------------+------------------------------------+
|                         Summary for sigh 2.68.2                          |
+-------------------------------------+------------------------------------+
| app_identifier                      | com.jenkin.Debug                   |
| username                            | 这里显示的是开发者账号             |
| force                               | true                               |
| cert_id                             | FQV3SGXTZ9                         |
| provisioning_name                   | match Development com.jenkin.Debug |
| ignore_profiles_with_different_name | true                               |
| team_id                             | 99WG99HF75                         |
| platform                            | ios                                |
| development                         | true                               |
| adhoc                               | false                              |
| skip_install                        | false                              |
| skip_fetch_profiles                 | false                              |
| skip_certificate_verification       | false                              |
| readonly                            | false                              |
+-------------------------------------+------------------------------------+

[17:57:19]: Starting login with user '这里显示的是开发者账号'
[17:57:21]: Successfully logged in
[17:57:21]: Fetching profiles...
[17:57:22]: Verifying certificates...
[17:57:25]: No existing profiles found, that match the certificates you have installed locally! Creating a new provisioning profile for you
[17:57:32]: The name 'match Development com.jenkin.Debug' is already taken, using another one.
[17:57:32]: Creating new provisioning profile for 'com.jenkin.Debug' with name 'match Development com.jenkin.Debug 1512640652' for 'ios' platform
[17:57:37]: Downloading provisioning profile...
[17:57:38]: Successfully downloaded provisioning profile...
[17:57:38]: Installing provisioning profile...
/var/folders/w9/q16q5ckx2k3b3vmskgh3ksx00000gn/T/d20171207-41012-161tif9/profiles/development/Development_com.jenkin.Debug.mobileprovision
[17:57:38]: Installing provisioning profile...
[17:57:40]:   Successfully encrypted certificates repo
[17:57:40]: Pushing changes to remote git repo...

+---------------------+------------------------------------------------+---------------------------------------------------------+
|                                                 Installed Provisioning Profile                                                 |
+---------------------+------------------------------------------------+---------------------------------------------------------+
| Parameter           | Environment Variable                           | Value                                                   |
+---------------------+------------------------------------------------+---------------------------------------------------------+
| App Identifier      |                                                | com.jenkin.Debug                                        |
| Type                |                                                | development                                             |
| Platform            |                                                | ios                                                     |
| Profile UUID        | sigh_com.jenkin.Debug_development              | 8d811267-9a90-4186-84ff-849270910564                    |
| Profile Name        | sigh_com.jenkin.Debug_development_profile-name | match Development com.jenkin.Debug 1512640652           |
| Profile Path        | sigh_com.jenkin.Debug_development_profile-path | /Users/xyj/Library/MobileDevice/Provisioning            |
|                     |                                                | Profiles/8d811267-9a90-4186-84ff-849270910564.mobilepr  |
|                     |                                                | ovision                                                 |
| Development Team ID | sigh_com.jenkin.Debug_development_team-id      | 99WG99HF75                                              |
+---------------------+------------------------------------------------+---------------------------------------------------------+

[17:57:40]: All required keys, certificates and provisioning profiles are installed 
[17:57:40]: Setting Provisioning Profile type to 'development'

+------+-------------------------------------+-------------+
|                     fastlane summary                     |
+------+-------------------------------------+-------------+
| Step | Action                              | Time (in s) |
+------+-------------------------------------+-------------+
| 1    | Verifying required fastlane version | 0           |
| 2    | default_platform                    | 0           |
| 3    | match                               | 122         |
+------+-------------------------------------+-------------+

[17:57:40]: fastlane.tools finished successfully 

这里主要说一下几点:

1、git_url:git_url是git服务器地址,我没指定git_url, ,匹配证书的时候,要求我输入服务器地址。输入完成后就会clone git服务器的代码,

2、验证证书:keychain_password就是为这步而存在的,这里由于我没在git服务器下载证书,所以没找到,但是fastlane为我重新创建了一个

3、描述文件:其实我已经在证书(之前就创建的证书)上生成了描述文件,但是fastlane重新生成了证书,所以他没找到描述文件,又自动给我重新生成了一个。

上面三种情况是fastlane的正常流程,但是我之前都创建好了,也就是说他现在的操作不是我想看到的。接下来我们将匹配开发证书的lane改成下面这样

desc "匹配开发证书"
lane :matchDev do
match(type: "development", keychain_password: "git", git_url: "[email protected]:repositories/test.git")
end

并且登录开发者账号将新生成的证书和描述文件都删掉,登录git服务器,将证书下载并安装好

再次在终端运行,结果如下:

fastlane matchDev
[11:12:18]: Get started using a Gemfile for fastlane https://docs.fastlane.tools/getting-started/ios/setup/#use-a-gemfile
[11:12:21]: -------------------------------------------------
[11:12:21]: --- Step: Verifying required fastlane version ---
[11:12:21]: -------------------------------------------------
[11:12:21]: Your fastlane version 2.68.2 matches the minimum requirement of 2.68.2  ✅
[11:12:21]: ------------------------------
[11:12:21]: --- Step: default_platform ---
[11:12:21]: ------------------------------
[11:12:21]: Driving the lane 'ios matchDev' 
[11:12:21]: -------------------
[11:12:21]: --- Step: match ---
[11:12:21]: -------------------

+-----------------------+-----------------------------------------+
|                    Summary for match 2.68.2                     |
+-----------------------+-----------------------------------------+
| type                  | development                             |
| git_url               | [email protected]:repositories/test.git |
| force                 | true                                    |
| git_branch            | master                                  |
| app_identifier        | com.jenkin.Debug                        |
| username              | 这里显示的是开发者账号                  |
| keychain_name         | login.keychain                          |
| readonly              | false                                   |
| team_id               | 99WG99HF75                              |
| verbose               | false                                   |
| skip_confirmation     | false                                   |
| shallow_clone         | false                                   |
| clone_branch_directly | false                                   |
| force_for_new_devices | false                                   |
| skip_docs             | false                                   |
| platform              | ios                                     |
+-----------------------+-----------------------------------------+

[11:12:21]: Cloning remote git repo...
[11:12:21]: If cloning the repo takes too long, you can use the `clone_branch_directly` option in match.
[11:12:22]:   Successfully decrypted certificates repo
[11:12:22]: Verifying that the certificate and profile are still valid on the Dev Portal...
[11:12:26]: Installing certificate...

+-------------------+----------------------------------------------------------+
|                            Installed Certificate                             |
+-------------------+----------------------------------------------------------+
| User ID           | BED2DYAVHS                                               |
| Common Name       | 这里显示证书名字
|
| Organisation Unit | 99WG99HF75                                               |
| Organisation      | ******** |
| Country           | US                                                       |
| Start Datetime    | Dec  8 02:58:55 2017 GMT                                 |
| End Datetime      | Dec  8 02:58:55 2018 GMT                                 |
+-------------------+----------------------------------------------------------+


+-------------------------------------+------------------------------------+
|                         Summary for sigh 2.68.2                          |
+-------------------------------------+------------------------------------+
| app_identifier                      | com.jenkin.Debug                   |
| username                            | 这里显示的是开发者账号             |
| force                               | true                               |
| cert_id                             | SP54MR68FC                         |
| provisioning_name                   | match Development com.jenkin.Debug |
| ignore_profiles_with_different_name | true                               |
| team_id                             | 99WG99HF75                         |
| platform                            | ios                                |
| development                         | true                               |
| adhoc                               | false                              |
| skip_install                        | false                              |
| skip_fetch_profiles                 | false                              |
| skip_certificate_verification       | false                              |
| readonly                            | false                              |
+-------------------------------------+------------------------------------+

[11:12:27]: Starting login with user '这里显示的是开发者账号'
[11:12:30]: Successfully logged in
[11:12:30]: Fetching profiles...
[11:12:31]: Verifying certificates...
[11:12:32]: Found 1 matching profile(s)
[11:12:32]: Recreating the profile
[11:12:34]: Creating new provisioning profile for 'com.jenkin.Debug' with name 'match Development com.jenkin.Debug' for 'ios' platform
[11:12:35]: Downloading provisioning profile...
[11:12:35]: Successfully downloaded provisioning profile...
[11:12:36]: Installing provisioning profile...
/var/folders/w9/q16q5ckx2k3b3vmskgh3ksx00000gn/T/d20171208-50529-1yh4aue/profiles/development/Development_com.jenkin.Debug.mobileprovision
[11:12:36]: Installing provisioning profile...
[11:12:37]:   Successfully encrypted certificates repo
[11:12:37]: Pushing changes to remote git repo...

+---------------------+------------------------------------+------------------------------------+
|                                Installed Provisioning Profile                                 |
+---------------------+------------------------------------+------------------------------------+
| Parameter           | Environment Variable               | Value                              |
+---------------------+------------------------------------+------------------------------------+
| App Identifier      |                                    | com.jenkin.Debug                   |
| Type                |                                    | development                        |
| Platform            |                                    | ios                                |
| Profile UUID        | sigh_com.jenkin.Debug_development  | 63075bad-3ee8-449e-9ebb-251c50ea7  |
|                     |                                    | f0c                                |
| Profile Name        | sigh_com.jenkin.Debug_development  | match Development                  |
|                     | _profile-name                      | com.jenkin.Debug                   |
| Profile Path        | sigh_com.jenkin.Debug_development  | /Users/xyj/Library/MobileDevice/P  |
|                     | _profile-path                      | rovisioning                        |
|                     |                                    | Profiles/63075bad-3ee8-449e-9ebb-  |
|                     |                                    | 251c50ea7f0c.mobileprovision       |
| Development Team ID | sigh_com.jenkin.Debug_development  | 99WG99HF75                         |
|                     | _team-id                           |                                    |
+---------------------+------------------------------------+------------------------------------+

[11:12:37]: All required keys, certificates and provisioning profiles are installed 
[11:12:37]: Setting Provisioning Profile type to 'development'

+------+------------------------------+-------------+
|                 fastlane summary                  |
+------+------------------------------+-------------+
| Step | Action                       | Time (in s) |
+------+------------------------------+-------------+
| 1    | Verifying required fastlane  | 0           |
|      | version                      |             |
| 2    | default_platform             | 0           |
| 3    | match                        | 16          |
+------+------------------------------+-------------+

[11:12:37]: fastlane.tools finished successfully 

完成上面的步骤后,clone下你的代码。工程下面多了两个文件夹certsprofiles

  • certs文件夹下面包含一个.cer文件和一个.p12文件。命名都是以证书id,这两个文件分别用于自己开发和授权团队的小伙伴开发的。
  • profiles文件夹下面存放的是描述文件

当你的仓库里面没这两个,fastlane会为你重新创建证书和描述文件。如果有了。那就是上面这种情况,fastlane会直接帮你匹配好证书和描述文件。

至此,创建一个匹配开发证书的lane就算完成了。

2.创建一个打测试包的lane

创建一个lane,格式如下:

desc "打测试包"
  desc "This will also make sure the profile is up to date"
  lane :expIpa do
  gym(clean: true, export_method: "development", configuration: "Debug")
  end

这里主要涉及的工具为gym,命令包含clean,export_method,configuration三个参数,分别的含义为编译之前是否clean、导出archive包的方式、编译哪个configuration。

想了解更多参数,可在终端输入fastlane action gymfastlane gym --help查看文档。

执行上面的命令后,在工程目录下可以看到导出的ipa包。没指定名字的话,ipa包的名字就是你的工程名。

3.创建一个上传到测试包到第三方分发平台的lane,这里以蒲公英为例

上传测试包到蒲公英、firim等第三方平台,都要到相应的第三方平台注册账号,这些平台一般都会制作fastlane的插件来支持fastlane上传测试包,这里做简单的介绍,查看详细文档请查看蒲公英官方文档。

1、查看、安装fastlane插件

在终端输入 fastlane search_plugins可以查看当前版本所有可用的插件,可以在里面看到pgyer插件,想知道如何使用请异步蒲公英官网查看。

安装pgyer插件:

fastlane add_plugin pgyer

安装完成后你就可以使用fastlane action pgyer来查看相关的参数以及使用了。

官网给出的例子是:

lane :beta do
  gym(export_method: "ad-hoc")
  pgyer(api_key: "7f15xxxxxxxxxxxxxxxxxx141", user_key: "4a5bcxxxxxxxxxxxxxxx3a9e")
end

以上的 api_key 和 user_key,请开发者在自己账号(蒲公英)下的 应用管理 - App概述 - API 中可以找到,并替换到以上相应的位置。

上面,我们已经打好测试包了,下面我们就上传测试到蒲公英。

lane如下:

desc "上传到蒲公英"
lane :uploadPGY do
pgyer(api_key: "75284caf4b30813b45b557ca7d121b2d", user_key: "fa8d179bb6b6cf382931ee2e487e2321",ipa: "JenkinsDemo.ipa")
end

执行结果如下:

➜  test git:(master) ✗ fastlane uploadPGY
+-----------------------+---------+--------+
|               Used plugins               |
+-----------------------+---------+--------+
| Plugin                | Version | Action |
+-----------------------+---------+--------+
| fastlane-plugin-pgyer | 0.2.1   | pgyer  |
+-----------------------+---------+--------+

[10:48:01]: -------------------------------------------------
[10:48:01]: --- Step: Verifying required fastlane version ---
[10:48:01]: -------------------------------------------------
[10:48:01]: Your fastlane version 2.68.2 matches the minimum requirement of 2.68.2  ✅
[10:48:01]: ------------------------------
[10:48:01]: --- Step: default_platform ---
[10:48:01]: ------------------------------
[10:48:01]: Driving the lane 'ios uploadPGY' 
[10:48:01]: -------------------
[10:48:01]: --- Step: pgyer ---
[10:48:01]: -------------------
[10:48:01]: The pgyer plugin is working.
[10:48:01]: build_file: JenkinsDemo.ipa
[10:48:01]: Start upload JenkinsDemo.ipa to pgyer...
[10:48:45]: Upload success. Visit this URL to see: https://www.pgyer.com/LnYN

+------+-------------------------------------+-------------+
|                     fastlane summary                     |
+------+-------------------------------------+-------------+
| Step | Action                              | Time (in s) |
+------+-------------------------------------+-------------+
| 1    | Verifying required fastlane version | 0           |
| 2    | default_platform                    | 0           |
| 3    | pgyer                               | 43          |
+------+-------------------------------------+-------------+

[10:48:45]: fastlane.tools finished successfully 

可以看到,上传成功后会给出一个链接,测试只需要打开这个链接就能扫码安装测试包了

小结:

上面几个步骤,其实是可以在一个lane里面完成的。我改进后的lane如下

desc "打测试包并上传蒲公英"
lane :dev do
# 调用匹配开发证书的lane
matchDev
expIpa
uploadPGY
end

当然,你可以这样:

desc "打测试包并上传蒲公英"
lane :dev do
match(type: "development", git_url: "[email protected]:repositories/test.git")
gym(clean: true, export_method: "development", configuration: "Debug")
pgyer(api_key: "75284caf4b30813b45b557ca7d121b2d", user_key: "fa8d179bb6b6cf382931ee2e487e2321",ipa: "JenkinsDemo.ipa")
end

这两种方法,本质上是一样的。上面只不过是lane之间的相互调用。

二、上传一个TestFlight的测试包,

命令如下

desc "打beta包上传TestFlight"
lane :beta do
match(type: "appstore", git_url: "[email protected]:repositories/test.git")
build_app(clean: true, output_name: "appstore", configuration: "Release", include_bitcode: true)
testflight(skip_submission: true)# 上传testflight
end

上传之前请做好前期的准备工作,在开发者账号上创建APP,xcode上添加好appicon等。

首先:前期的匹配证书、导出ipa文件和打测试包差不多,改一下相应的参数就行了。只是增加了上传到TestFlight,并且只是上传一个ipa包并没有将app相关的信息也上传到TestFlight。

我在测试过程中遇到的问题:

1、因为是测试,所以一开始没有添加相应尺寸的appicon,导致失败。

2、在成功导出ipa包,准备上传的时候报错,如下图 :


使用fastlane自动化部署APP_第2张图片
报错

原因:上次上传的动作被被记录在UploadToken文件夹下面了,我们要进入user/.itmstransporter/UploadTokens该目录下将里面的文件删除,再次运行fastlane beta命令,就能上传成功了,上传过程会比较久,但终端会显示上传的进度,大概如下

DEBUG [2017-12-09 17:39:46.83]: [Transporter]: INFO:      File: 9a0450527b7b0dd213f32d8ad7a043d2.ipa 1880200/93265945, 2% completed
DEBUG [2017-12-09 17:40:07.92]: [Transporter]: INFO:      File: 9a0450527b7b0dd213f32d8ad7a043d2.ipa 3744600/93265945, 4% completed
DEBUG [2017-12-09 17:40:16.55]: [Transporter]: INFO:      File: 9a0450527b7b0dd213f32d8ad7a043d2.ipa 4676800/93265945, 5% completed
DEBUG [2017-12-09 17:40:24.74]: [Transporter]: INFO:      File: 9a0450527b7b0dd213f32d8ad7a043d2.ipa 5609000/93265945, 6% completed
DEBUG [2017-12-09 17:40:36.80]: [Transporter]: INFO:      File: 9a0450527b7b0dd213f32d8ad7a043d2.ipa 6541200/93265945, 7% completed
DEBUG [2017-12-09 17:40:55.42]: [Transporter]: INFO:      File: 9a0450527b7b0dd213f32d8ad7a043d2.ipa 8405600/93265945, 9% completed
DEBUG [2017-12-09 17:41:09.38]: [Transporter]: INFO:      File: 9a0450527b7b0dd213f32d8ad7a043d2.ipa 9337800/93265945, 10% completed
DEBUG [2017-12-09 17:41:20.30]: [Transporter]: INFO:      File: 9a0450527b7b0dd213f32d8ad7a043d2.ipa 10270000/93265945, 11% completed
DEBUG [2017-12-09 17:41:39.45]: [Transporter]: INFO:      File: 9a0450527b7b0dd213f32d8ad7a043d2.ipa 12134400/93265945, 13% completed
DEBUG [2017-12-09 17:41:49.63]: [Transporter]: INFO:      File: 9a0450527b7b0dd213f32d8ad7a043d2.ipa 13066600/93265945, 14% completed
DEBUG [2017-12-09 17:41:58.10]: [Transporter]: INFO:      File: 9a0450527b7b0dd213f32d8ad7a043d2.ipa 13998800/93265945, 15% completed

如果要直接上传appstore,将上面的testflight命令换成pload_to_app_store命令就可以了。(因为这是制作demo,所以没具体操作)。请自行尝试!

结语

这篇博客只是提及了比较常用,也是比较实用的功能。起到抛砖引玉的作用fastlane还有很多强大的功能,这里没有一一提及,有兴趣的小伙伴可以多多研究

相关的知识:
atool
xcodebuild 自动化构建
苹果下使用Gitosis的搭建git的服务器
fastlane文档

你可能感兴趣的:(使用fastlane自动化部署APP)