使用fastlane一鍵打包iOS項目并上傳到蒲公英平臺

平時打包一般過程是先在xcode上打好ipa包,然后上傳到蒲公英上,最后把二維碼發(fā)到群里給大家下載,這個過程都需要有人在旁邊等著,一步一步點,比較麻煩。這兩天試驗了一下,可以通過fastlane,直接在終端中敲一個命令就可以把包自動發(fā)布到蒲公英上了,發(fā)布成功后微信上就收到推送消息。本文介紹下這個過程。

1. 安裝fastlane準備工作

fastlane是ruby寫的,這里安裝的前提是你的ruby環(huán)境是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環(huán)境

注意這里查看使用的ruby版本命令是
ruby -v
而不是
rvm -v
以下是我的環(huán)境:
?  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環(huán)境以后,在新的環(huán)境上可能還要重新裝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

可能持續(xù)的時間比較長,安裝結束后可以查看版本,和安裝路徑

?  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以后,我們到項目根目錄下面執(zhí)行
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執(zhí)行完以后會在項目根目錄下生成一個名叫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里面會執(zhí)行upload_pgy(),這個方法里面會使用蒲公英提供的命令來上傳包,蒲公英的命令可以參考這里獲取蒲公英上傳命令
  2. 這里面從platform :ios do這里開始執(zhí)行,流程簡化如下
# 打包前
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 后面打包我們只需要執(zhí)行命令fastlane AdHoc version:1.3.3 build:200即可,也可以用shell腳本再封裝一層,這樣只需要執(zhí)行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. 開始打包

腳本準備好以后可以回到項目根目錄下直接執(zhí)行命令開始打包了

?  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號,如下



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


執(zhí)行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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容