平時(shí)打包一般過(guò)程是先在xcode上打好ipa包命斧,然后上傳到蒲公英上,最后把二維碼發(fā)到群里給大家下載嘱兼,這個(gè)過(guò)程都需要有人在旁邊等著国葬,一步一步點(diǎn),比較麻煩。這兩天試驗(yàn)了一下汇四,可以通過(guò)fastlane接奈,直接在終端中敲一個(gè)命令就可以把包自動(dòng)發(fā)布到蒲公英上了,發(fā)布成功后微信上就收到推送消息通孽。本文介紹下這個(gè)過(guò)程序宦。
1. 安裝fastlane準(zhǔn)備工作
fastlane是ruby寫(xiě)的,這里安裝的前提是你的ruby環(huán)境是2.0版本以上背苦。然后還要安裝好xcode-select互捌,也就是xcode的命令行工具,因?yàn)閒astlane的腳本里面都是通過(guò)命令來(lái)打包簽名項(xiàng)目的行剂。
安裝xcode-select命令如下
xcode-select --install
如果沒(méi)有安裝的話會(huì)彈出一個(gè)對(duì)話框提示你安裝秕噪,如果已經(jīng)安裝的話會(huì)顯示如下信息:
? 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機(jī)中安裝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) ?
關(guān)于cocoapods多說(shuō)幾句
這里使用了新的ruby環(huán)境以后,在新的環(huán)境上可能還要重新裝pod厚宰,關(guān)于pod也有一些要注意的腌巾。安裝cocoapods之前最好把之前的cocoapods卸載干凈,先查看下已經(jīng)安裝的cocoapods組件铲觉,使用命令gem list澈蝙,如下圖
卸載如下圖,后面寫(xiě)要卸載的組件名备燃,不包括括號(hào)中的版本號(hào)哦
指定版本安裝命令:
? ~ sudo gem install cocoapods --version 1.0.1
下圖可以看出安裝到的路徑
2. 安裝fastlane
命令如下
sudo gem install fastlane --verbose
如果報(bào)錯(cuò)的話
ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/commander
則使用如下命令
sudo gem install -n /usr/local/bin fastlane
可能持續(xù)的時(shí)間比較長(zhǎng)碉克,安裝結(jié)束后可以查看版本,和安裝路徑
? 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以后并齐,我們到項(xiàng)目根目錄下面執(zhí)行
fastlane init
這個(gè)感覺(jué)就跟git很像了漏麦。然后會(huì)輸出很多信息,這些信息我寫(xiě)在后面了况褪。有幾個(gè)地方會(huì)需要你填一下撕贞,比如appId和密碼之類(lèi),如下
? 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í)行完以后會(huì)在項(xiàng)目根目錄下生成一個(gè)名叫fastlane的文件夾测垛,如下圖:
3. 編寫(xiě)腳本
我們的腳本都寫(xiě)在Fastfile文件中捏膨,現(xiàn)在直接把腳本代碼貼出來(lái)吧
# 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'
# 下面幾個(gè)函數(shù)打包用到了
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參數(shù)
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
# 下面幾個(gè)函數(shù)打包完成后用到了
def rename_ipa()
sh "mv '../build/#{SCHEME_NAME}.ipa' '../build/#{APP_NAME}.ipa'"
end
def upload_pgy()
say 'upload pgy'
# 下面要使用到蒲公英上傳命令号涯,自己去蒲公英網(wǎng)站查詢(xún)
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
這個(gè)腳本中有一些注意的地方如下:
- 在after_all里面會(huì)執(zhí)行upload_pgy(),這個(gè)方法里面會(huì)使用蒲公英提供的命令來(lái)上傳包锯七,蒲公英的命令可以參考這里獲取蒲公英上傳命令链快;
- 這里面從platform :ios do這里開(kāi)始執(zhí)行,流程簡(jiǎn)化如下
# 打包前
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即可巨双,是不是感覺(jué)更簡(jiǎn)單一點(diǎn)呢!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. 開(kāi)始打包
腳本準(zhǔn)備好以后可以回到項(xiàng)目根目錄下直接執(zhí)行命令開(kāi)始打包了
? sky-forest-ios git:(zyzz_1.3.3) ? fastlane AdHoc version:1.3.3 build:200
如果用shell腳本包過(guò)一層筑累,則命令如下
? sky-forest-ios git:(zyzz_1.3.3) ? sh build.sh 1.3.3 200
打包過(guò)程中注意:
- 可能會(huì)出錯(cuò),說(shuō)fastlane版本太低丝蹭,這時(shí)候需要升級(jí)版本慢宗,根據(jù)提示命令升級(jí)即可;
- 能打包并上傳成功的前提是項(xiàng)目本身也是可以運(yùn)行的半夷;
-
我的版本是2.52.0婆廊,項(xiàng)目里面還要在配置好Build Settings里面的Versioning迅细,不光是要配置好General里面的Version和Build號(hào)巫橄,如下
最后就可以去吃飯了,打包好后可以收到蒲公英的微信推送:
執(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