使用fastlane一键打包iOS项目并上传到蒲公英平台

平时打包一般过程是先在xcode上打好ipa包,然后上传到蒲公英上,最后把二维码发到群里给大家下载,这个过程都需要有人在旁边等着,一步一步点,比较麻烦。这两天试验了一下,可以通过fastlane,直接在终端中敲一个命令就可以把包自动发布到蒲公英上了,发布成功后微信上就收到推送消息。本文介绍下这个过程。

1. 安装fastlane准备工作

fastlane是ruby写的,这里安装的前提是你的ruby环境是2.0版本以上。然后还要安装好xcode-select,也就是xcode的命令行工具,因为fastlane的脚本里面都是通过命令来打包签名项目的。

安装xcode-select命令如下

xcode-select --install

如果没有安装的话会弹出一个对话框提示你安装,如果已经安装的话会显示如下信息:

➜  sky-forest-ios git:(zyzz_1.3.3) ✗ xcode-select --install
xcode-select: error: command line tools are already installed, use "Software Update" to install updates
➜  sky-forest-ios git:(zyzz_1.3.3) ✗ 

安装ruby

可以参考这篇文章
MAC机中安装RUBY环境

注意这里查看使用的ruby版本命令是
ruby -v
而不是
rvm -v
以下是我的环境:
➜  sky-forest-ios git:(zyzz_1.3.3) ✗ rvm -v
rvm 1.29.0 (latest) by Michal Papis, Piotr Kuczynski, Wayne E. Seguin [https://rvm.io/]
➜  sky-forest-ios git:(zyzz_1.3.3) ✗ ruby -v
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin16]
➜  sky-forest-ios git:(zyzz_1.3.3) ✗ 

关于cocoapods多说几句

这里使用了新的ruby环境以后,在新的环境上可能还要重新装pod,关于pod也有一些要注意的。安装cocoapods之前最好把之前的cocoapods卸载干净,先查看下已经安装的cocoapods组件,使用命令gem list,如下图



卸载如下图,后面写要卸载的组件名,不包括括号中的版本号哦



指定版本安装命令:
➜  ~ sudo gem install cocoapods --version 1.0.1

下图可以看出安装到的路径


2. 安装fastlane

命令如下
sudo gem install fastlane --verbose
如果报错的话
ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/commander
则使用如下命令
sudo gem install -n /usr/local/bin fastlane

可能持续的时间比较长,安装结束后可以查看版本,和安装路径

➜  sky-forest-ios git:(zyzz_1.3.3) ✗ fastlane -v
fastlane installation at path:
/Users/guohongwei719/.rvm/gems/ruby-2.4.0@global/gems/fastlane-2.52.0/bin/fastlane
-----------------------------
fastlane 2.52.0
➜  sky-forest-ios git:(zyzz_1.3.3) ✗ 

安装好fastlane以后,我们到项目根目录下面执行
fastlane init
这个感觉就跟git很像了。然后会输出很多信息,这些信息我写在后面了。有几个地方会需要你填一下,比如appId和密码之类,如下

➜  testFastlane fastlane init
[10:42:16]: For more information, check out https://docs.fastlane.tools/getting-started/ios/setup/#use-a-gemfile
[10:42:18]: Detected iOS/Mac project in current directory...
[10:42:18]: This setup will help you get up and running in no time.
[10:42:18]: fastlane will check what tools you're already using and set up
[10:42:18]: the tool automatically for you. Have fun! 
[10:42:18]: Created new folder './fastlane'.
[10:42:18]: $ xcodebuild -list -workspace ./zyzz.xcworkspace
[10:42:20]: $ xcodebuild -showBuildSettings -workspace ./zyzz.xcworkspace -scheme zyzz
[10:42:22]: Your Apple ID (e.g. fastlane@krausefx.com): admin@elab-plus.com
[10:43:17]: Verifying that app is available on the Apple Developer Portal and iTunes Connect...
[10:43:17]: Starting login with user 'admin@elab-plus.com'
-------------------------------------------------------------------------------------
Please provide your Apple Developer Program account credentials
The login information you enter will be stored in your macOS Keychain
You can also pass the password using the `FASTLANE_PASSWORD` environment variable
More information about it on GitHub: https://github.com/fastlane/fastlane/tree/master/credentials_manager
-------------------------------------------------------------------------------------
Password (for admin@elab-plus.com): ***********

+----------------+--------------------------------------+
|                    Detected Values                    |
+----------------+--------------------------------------+
| Apple ID       | admin@elab-plus.com                  |
| App Name       | zyzz                                 |
| App Identifier | com.elab.skyforest.chongqing         |
| Workspace      | /Users/guohongwei719/Desktop/testFa  |
|                | stlane/zyzz.xcworkspace              |
+----------------+--------------------------------------+

[10:44:03]: Please confirm the above values (y/n)

fastlane init执行完以后会在项目根目录下生成一个名叫fastlane的文件夹,如下图:


3. 编写脚本

我们的脚本都写在Fastfile文件中,现在直接把脚本代码贴出来吧

# Customise this file, documentation can be found here:
# https://github.com/fastlane/fastlane/tree/master/fastlane/docs
# 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:
# update_fastlane

# This is the minimum version number required.
# Update this, if you use features of a newer version
fastlane_version "2.52.0"

# 用到的
PROJECT_FILE_PATH = './zyzz.xcodeproj'
APP_NAME = 'skyforest'
SCHEME_NAME = 'zyzz'
IPA_PATH = '../build/'
PLIST_FILE_PATH = 'zyzz/Info.plist'

# 下面几个函数打包用到了
def set_info_plist_value(path,key,value)
    sh "/usr/libexec/PlistBuddy -c \"set :#{key} #{value}\" #{path}"
end
def set_channel_id(channelId)
    set_info_plist_value(
        ".././#{PLIST_FILE_PATH}",
        'ChannelID',
        "#{channelId}"
    )
end
def prepare_version(options)
    #say 'version number:'
    #say options[:version]
    increment_version_number(
        version_number: options[:version],
        xcodeproj: PROJECT_FILE_PATH,
    )
    #say 'build number:'
    #say options[:build]
    increment_build_number(
        build_number: options[:build],
        xcodeproj: PROJECT_FILE_PATH,
    )
end
def download_provision(typePrefix,isAdHoc)
    # We manual download the provision
    return
end
def generate_ipa(typePrefix,options)
    #say 'generate ipa'
    fullVersion = options[:version] + '.' + options[:build]
    channelId = options[:channel_id]
    #指定打包所使用的输出方式,目前支持app-store, package, ad-hoc, enterprise, development, 和developer-id,即xcodebuild的method参数
    if typePrefix == 'AdHoc'
      gym(
        scheme: "#{SCHEME_NAME}",
        workspace: './zyzz.xcworkspace',
        export_method: 'ad-hoc', 
        output_directory: './build/'
        )
    elsif typePrefix == 'InHouse'
      gym(
        scheme: "#{SCHEME_NAME}",
        workspace: './zyzz.xcworkspace',
        export_method: 'enterprise', 
        output_directory: './build/'
        )
    end
    #sh "mv .././build/#{APP_NAME}.app.dSYM.zip .././build/#{APP_NAME}_#{fullVersion}_#{typePrefix}.app.dSYM.zip"
end
# 下面几个函数打包完成后用到了
def rename_ipa()
    sh "mv '../build/#{SCHEME_NAME}.ipa' '../build/#{APP_NAME}.ipa'"
end
def upload_pgy()
  say 'upload pgy'
  # 下面要使用到蒲公英上传命令,自己去蒲公英网站查询
  sh "curl -F 'file=@../build/#{APP_NAME}.ipa' -F 'uKey=My_uKey' -F '_api_key=My_api_key' http://www.pgyer.com/apiv1/app/upload"
  # sh "~/Desktop/qrsctl login #{QINIU_NAME} #{QINIU_PWD}"
  # sh "~/Desktop/qrsctl cdn/refresh https://oh6p40stc.qnssl.com/zyzz.ipa"
  # sh "~/Desktop/qrsctl put -c elab #{SCHEME_NAME}.ipa ../build/#{SCHEME_NAME}.ipa"
end

def clean_derivedData()
  sh "rm -fr #{IPA_PATH}*"
end

default_platform :ios
platform :ios do
  before_all do
    # ENV["SLACK_URL"] = "https://hooks.slack.com/services/..."
    clean_derivedData()
    # pod_install()
    # git()
    APP_NAME =  "#{SCHEME_NAME}_#{Time.new.to_i}"    
  end

  desc "AdHoc"
  lane:AdHoc do |options|
    typePrefix = 'AdHoc'
    set_channel_id(typePrefix)
    prepare_version(options)
    download_provision(typePrefix,true)
    # update_app_identifier("#{OTHER_IDENTIFIER}")
    # update_provision(typePrefix)
    generate_ipa(typePrefix,options)
  end

  # You can define as many lanes as you want

  after_all do |lane|
    rename_ipa()
    upload_pgy()
    clean_derivedData()

    # 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://github.com/fastlane/fastlane/blob/master/fastlane/docs/Platforms.md
# All available actions: https://docs.fastlane.tools/actions

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

这个脚本中有一些注意的地方如下:

  1. 在after_all里面会执行upload_pgy(),这个方法里面会使用蒲公英提供的命令来上传包,蒲公英的命令可以参考这里获取蒲公英上传命令
  2. 这里面从platform :ios do这里开始执行,流程简化如下
# 打包前
clean_derivedData()
APP_NAME =  "#{SCHEME_NAME}_#{Time.new.to_i}" 

# 打包中
typePrefix = 'AdHoc'
set_channel_id(typePrefix)
prepare_version(options)
download_provision(typePrefix,true)
generate_ipa(typePrefix,options)

# 打包后
rename_ipa()
upload_pgy()
clean_derivedData()

3 后面打包我们只需要执行命令fastlane AdHoc version:1.3.3 build:200即可,也可以用shell脚本再封装一层,这样只需要执行sh build.sh 1.3.3 200即可,是不是感觉更简单一点呢!shell脚本如下,此脚本命名为build.sh

#!/bin/sh

#
# usage:
# > sh build.sh 1.0.0 100
#

versionNumber=$1
buildNumber=$2

#basicLanes="AdHoc AppStore Develop InHouse"
basicLanes="AdHoc"
for laneName in $basicLanes
do
    fastlane $laneName version:$versionNumber build:$buildNumber
done

4. 开始打包

脚本准备好以后可以回到项目根目录下直接执行命令开始打包了

➜  sky-forest-ios git:(zyzz_1.3.3) ✗ fastlane AdHoc version:1.3.3 build:200

如果用shell脚本包过一层,则命令如下

➜  sky-forest-ios git:(zyzz_1.3.3) ✗ sh build.sh 1.3.3 200

打包过程中注意:

  1. 可能会出错,说fastlane版本太低,这时候需要升级版本,根据提示命令升级即可;
  2. 能打包并上传成功的前提是项目本身也是可以运行的;
  3. 我的版本是2.52.0,项目里面还要在配置好Build Settings里面的Versioning,不光是要配置好General里面的Version和Build号,如下



    最后就可以去吃饭了,打包好后可以收到蒲公英的微信推送:


执行fastlane init打印信息如下:

➜  testFastlane fastlane init
[10:42:16]: For more information, check out https://docs.fastlane.tools/getting-started/ios/setup/#use-a-gemfile
[10:42:18]: Detected iOS/Mac project in current directory...
[10:42:18]: This setup will help you get up and running in no time.
[10:42:18]: fastlane will check what tools you're already using and set up
[10:42:18]: the tool automatically for you. Have fun! 
[10:42:18]: Created new folder './fastlane'.
[10:42:18]: $ xcodebuild -list -workspace ./zyzz.xcworkspace
[10:42:20]: $ xcodebuild -showBuildSettings -workspace ./zyzz.xcworkspace -scheme zyzz
[10:42:22]: Your Apple ID (e.g. fastlane@krausefx.com): admin@elab-plus.com
[10:43:17]: Verifying that app is available on the Apple Developer Portal and iTunes Connect...
[10:43:17]: Starting login with user 'admin@elab-plus.com'
-------------------------------------------------------------------------------------
Please provide your Apple Developer Program account credentials
The login information you enter will be stored in your macOS Keychain
You can also pass the password using the `FASTLANE_PASSWORD` environment variable
More information about it on GitHub: https://github.com/fastlane/fastlane/tree/master/credentials_manager
-------------------------------------------------------------------------------------
Password (for admin@elab-plus.com): ***********

+----------------+--------------------------------------+
|                    Detected Values                    |
+----------------+--------------------------------------+
| Apple ID       | admin@elab-plus.com                  |
| App Name       | zyzz                                 |
| App Identifier | com.elab.skyforest.chongqing         |
| Workspace      | /Users/guohongwei719/Desktop/testFa  |
|                | stlane/zyzz.xcworkspace              |
+----------------+--------------------------------------+

[10:44:03]: Please confirm the above values (y/n)
y
[10:44:24]: Created new file './fastlane/Appfile'. Edit it to manage your preferred app metadata information.
[10:44:24]: Loading up 'deliver', this might take a few seconds
[10:44:25]: Running precheck before submitting to review, if you'd like to disable this check you can set run_precheck_before_submit to false
[10:44:25]: Login to iTunes Connect (admin@elab-plus.com)
[10:44:28]: Login successful

+-----------------------------+------------------------------+
|                   deliver 2.52.0 Summary                   |
+-----------------------------+------------------------------+
| screenshots_path            | ./fastlane/screenshots       |
| metadata_path               | ./fastlane/metadata          |
| username                    | admin@elab-plus.com          |
| app_identifier              | com.elab.skyforest.chongqing |
| 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          | 45NCV9F7PJ                   |
| overwrite_screenshots       | false                        |
| run_precheck_before_submit  | true                         |
| precheck_default_rule_level | warn                         |
+-----------------------------+------------------------------+

[10:44:35]: Writing to './fastlane/metadata/zh-Hans/description.txt'
[10:44:35]: Writing to './fastlane/metadata/zh-Hans/keywords.txt'
[10:44:35]: Writing to './fastlane/metadata/zh-Hans/release_notes.txt'
[10:44:35]: Writing to './fastlane/metadata/zh-Hans/support_url.txt'
[10:44:35]: Writing to './fastlane/metadata/zh-Hans/marketing_url.txt'
[10:44:35]: Writing to './fastlane/metadata/zh-Hans/promotional_text.txt'
[10:44:35]: Writing to './fastlane/metadata/zh-Hans/name.txt'
[10:44:35]: Writing to './fastlane/metadata/zh-Hans/subtitle.txt'
[10:44:35]: Writing to './fastlane/metadata/zh-Hans/privacy_url.txt'
[10:44:35]: Writing to './fastlane/metadata/copyright.txt'
[10:44:35]: Writing to './fastlane/metadata/primary_category.txt'
[10:44:35]: Writing to './fastlane/metadata/secondary_category.txt'
[10:44:35]: Writing to './fastlane/metadata/primary_first_sub_category.txt'
[10:44:35]: Writing to './fastlane/metadata/primary_second_sub_category.txt'
[10:44:35]: Writing to './fastlane/metadata/secondary_first_sub_category.txt'
[10:44:35]: Writing to './fastlane/metadata/secondary_second_sub_category.txt'
[10:44:35]: Writing to './fastlane/metadata/trade_representative_contact_information/trade_name.txt'
[10:44:35]: Writing to './fastlane/metadata/trade_representative_contact_information/first_name.txt'
[10:44:35]: Writing to './fastlane/metadata/trade_representative_contact_information/last_name.txt'
[10:44:35]: Writing to './fastlane/metadata/trade_representative_contact_information/address_line1.txt'
[10:44:35]: Writing to './fastlane/metadata/trade_representative_contact_information/address_line2.txt'
[10:44:35]: Writing to './fastlane/metadata/trade_representative_contact_information/city_name.txt'
[10:44:35]: Writing to './fastlane/metadata/trade_representative_contact_information/state.txt'
[10:44:35]: Writing to './fastlane/metadata/trade_representative_contact_information/country.txt'
[10:44:35]: Writing to './fastlane/metadata/trade_representative_contact_information/postal_code.txt'
[10:44:35]: Writing to './fastlane/metadata/trade_representative_contact_information/phone_number.txt'
[10:44:35]: Writing to './fastlane/metadata/trade_representative_contact_information/email_address.txt'
[10:44:35]: Writing to './fastlane/metadata/trade_representative_contact_information/is_displayed_on_app_store.txt'
[10:44:35]: Writing to './fastlane/metadata/review_information/first_name.txt'
[10:44:35]: Writing to './fastlane/metadata/review_information/last_name.txt'
[10:44:35]: Writing to './fastlane/metadata/review_information/phone_number.txt'
[10:44:35]: Writing to './fastlane/metadata/review_information/email_address.txt'
[10:44:35]: Writing to './fastlane/metadata/review_information/demo_user.txt'
[10:44:35]: Writing to './fastlane/metadata/review_information/demo_password.txt'
[10:44:35]: Writing to './fastlane/metadata/review_information/notes.txt'
[10:44:35]: Successfully created new configuration files.
[10:44:40]: Successfully downloaded large app icon
[10:44:40]: Downloading all existing screenshots...
[10:44:43]: Downloading existing screenshot '1_iphone6Plus_1.1.png' for language 'zh-Hans'
[10:45:08]: Downloading existing screenshot '2_iphone6Plus_2.2.png' for language 'zh-Hans'
[10:45:21]: Downloading existing screenshot '3_iphone6Plus_3.3.png' for language 'zh-Hans'
[10:45:45]: Downloading existing screenshot '4_iphone6Plus_4.4.png' for language 'zh-Hans'
[10:46:10]: Downloading existing screenshot '5_iphone6Plus_5.1.3.2_5_ios.jpg' for language 'zh-Hans'
[10:46:19]: Successfully downloaded all existing screenshots
[10:46:19]: Successfully created new Deliverfile at path './fastlane/Deliverfile'
[10:46:19]: 'snapshot' not enabled.
[10:46:19]: 'cocoapods' enabled.
[10:46:19]: 'carthage' not enabled.
[10:46:19]: Created new file './fastlane/Fastfile'. Edit it to manage your own deployment lanes.
[10:46:19]: fastlane will collect the number of errors for each action to detect integration issues
[10:46:19]: No sensitive/private information will be uploaded
[10:46:19]: Learn more at https://github.com/fastlane/fastlane#metrics
[10:46:19]: Successfully finished setting up fastlane

#######################################################################
# fastlane 2.53.1 is available. You are on 2.52.0.
# You should use the latest version.
# Please update using `sudo gem install fastlane`.
#######################################################################

2.53.1 Bugfix
* Fix dup crash on nil for gym on older versions of Ruby (#10031) via Felix Krause
* [Gym] Ignore unnecessary Provisioning Profiles when generating mapping (#9885) via Takashi Hasegawa
* Update spaceship docs to indicate profiles is an array (#10025) via Felix Krause

2.53.0 Improve automatic code signing detection
* Drastically improve Xcode 9 provisioning profile detection (#9952) via Felix Krause
* [match] Add support for provisioning profile mapping for multiple app identifiers (#10020) via Felix Krause
* Added chomp to deliver upload metadata review information values (#10021) via Yusuke Fujiki
* Don't underline printed commands (#10018) via Felix Krause
* Add support for frameit titles below images (#10012) via Claes Jacobsson
* Limit user access when creating new App in iTunes (#9986) via Javier Agüera Cazorla
* New fastlane action to modify services (produce) (#10010) via Bhimsen Padalkar

Please update using `sudo gem install fastlane`

[10:46:21]: RubyGems is not listed as your Gem source
[10:46:21]: You can run `gem sources` to see all your sources
[10:46:21]: Please run the following command to fix this:
[10:46:21]: $ gem sources --add https://rubygems.org

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,738评论 5 472
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,377评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,774评论 0 333
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,032评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,015评论 5 361
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,239评论 1 278
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,724评论 3 393
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,374评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,508评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,410评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,457评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,132评论 3 316
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,733评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,804评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,022评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,515评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,116评论 2 341

推荐阅读更多精彩内容