身爲一名iOS開發人員,面臨必不可少的一個階段就是打包測試。雖然xcode可有實現打包,但在測試階段面臨測試人員不斷的向你催促須要新的包進行測試就會很讓人頭疼,打包又是一個重複性極高又很費時間的事情。因此研究了一下jenkins和fastlane的自動打包流程,但願對你們有所幫助,途中也遇到了衆多的問題和坑。html
如需幫助可直接 聯繫我java
Ruby:linux
fastlane的安裝須要Ruby2.0版本以上,雖然mac自帶ruby,可是版本可能較低,而且權限較少,因此推薦使用RVM管理ruby版本。ios
按照這篇文章安裝既能夠了 Mac安裝Ruby版本管理器RVMgit
xcode命令行工具github
xcode-select --install
複製代碼
若是已經安裝會提示 docker
若是沒有安裝會出現 shell
直接點擊安裝就能夠了,安裝完成後再次執行 xcode-select --install
以後出現已經安裝的提示了。swift
Java JDK (這個是Jenkins安裝用的,若是不須要jenkins可直接忽略)windows
可先查看 當前安裝的java版本
java -version
複製代碼
若是未安裝請先前往 JDK下載連接 下載
由於jenkins是用java語言編寫的,因此須要java的支持,建議使用1.8版本的JDK
須要先接受協議以後才能夠下載
下載完成以後直接安裝就能夠了,這裏就再也不贅述。
前期準備工做到這裏就完成了
fastlane是爲iOS和Android應用程序自動化beta部署和發佈最簡單的方法。🚀能夠處理各類繁瑣任務,例如生成屏幕截圖,處理代碼簽名和發佈應用該程序等.fastlane整合了一系列移動端開發中簽名,編譯,發佈等工具,堪稱打包神器。
Homebrew
brew cask install fastlane
複製代碼
安裝程序腳本
下載zip文件。後雙擊shell腳本便可
RubyGems
gem install fastlane -NV
複製代碼
這裏咱們在最後使用了 Jenkins上傳蒲公英,爲了獲取下載二維碼。
本文也提到了使用Fastlane上傳蒲公英的介紹
Fastlane 安裝成功以後 執行安裝蒲公英的插件
fastlane add_plugin pgyer
複製代碼
若是沒什麼意外就直接成功了
剛開始我使用的本機自帶的ruby, 失敗了。而後使用了rvm管理的ruby以後就成功了。
若是遇到了其餘問題請你們google一下吧,或者聯繫我咱們一塊兒探討。
cd /到本身的項目目錄中
複製代碼
初始化fastlane
fastlane init
複製代碼
若是以前的都沒有問題 會出現如下選項

複製代碼
這裏咱們選的是第3個,而後會出現一些選項
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
? 3
[10:37:53]: ----------------------------------------------------------
[10:37:53]: --- Setting up fastlane for iOS App Store distribution ---
[10:37:53]: ----------------------------------------------------------
[10:37:53]: Parsing your local Xcode project to find the available schemes and the app identifier
[10:37:53]: $ xcodebuild -showBuildSettings -workspace FastlaneDev.xcworkspace -scheme FastlaneDev
[10:37:55]: $ cd `你項目的路徑` && agvtool what-version -terse
[10:37:55]: --------------------------------
[10:37:55]: --- Login with your Apple ID ---
[10:37:55]: --------------------------------
[10:37:55]: To use App Store Connect and Apple Developer Portal features as part of fastlane,
[10:37:55]: we will ask you for your Apple ID username and password
[10:37:55]: This is necessary for certain fastlane features, for example:
[10:37:55]:
[10:37:55]: - Create and manage your provisioning profiles on the Developer Portal
[10:37:55]: - Upload and manage TestFlight and App Store builds on App Store Connect
[10:37:55]: - Manage your App Store Connect app metadata and screenshots
[10:37:55]:
[10:37:55]: Your Apple ID credentials will only be stored in your Keychain, on your local machine
[10:37:55]: For more information, check out
[10:37:55]: https://github.com/fastlane/fastlane/tree/master/credentials_manager
[10:37:55]:
[10:37:55]: Please enter your Apple ID developer credentials
[10:37:55]: Apple ID Username:
`你的Apple ID`
[10:38:16]: Logging in...
複製代碼

複製代碼
若是你帳號下有多個team會讓你選擇一下
而後等待安裝
在這期間我遇到了bundle update這句話一直不動,我從新init了一遍就行了
成功以後 打開咱們的項目目錄發現多了這個文件
恭喜你 初始化成功了 !!!
文件名 | 描述 |
---|---|
Appfile | 從 Apple Developer Portal 獲取和項目相關的信息 詳情 |
Fastfile | 核心文件,存放lane任務 |
Deliverfile | deliver的配置文件,從 iTunes Connect 獲取和項目相關的信息詳細 |
metadata | 同步iTC中的元數據 |
screenshots | 同步iTC中的截圖 |
這裏給打家推薦一篇配置的文章Fastfile參考配置
下面是個人配置僅供參考,沒有用fastlane上傳蒲公英,由於想要在jenkins裏面獲取二維碼暫時的解決辦法使用jenkins上傳蒲公英 若是須要fastlane上傳直接打開的註釋進行了
# 導出路徑
$ipa_output_directory = "/Users/xx/xx/fastlaneBuild"
# 上傳蒲公英的包 路徑
$ipa_output_upload_directory = "/Users/xx/.jenkins/workspace/xx/ipa"
# jenkins匹配的包名
$jenkins_name = "項目名"
default_platform(:ios)
# 應用名詞
scheme_name = "項目名字"
# 獲取version
version = get_info_plist_value(path: "./#{scheme_name}/Info.plist", key: "CFBundleShortVersionString")
# 獲取build版本號
build = get_info_plist_value(path: "./#{scheme_name}/Info.plist", key: "CFBundleVersion")
# 指定xcode打包 這裏可選擇不要,咱們的項目須要用10.1版本xcode
xcode_select("/Applications/Xcode_10.1.app")
# 默認內測打包方式,目前支持app-store, package, ad-hoc, enterprise, development
ipa_exportMethod = "ad-hoc"
ipa_exportMethod_appStore = "app-store"
# 計算buildNumber
def updateProjectBuildNumber
currentTime = Time.new.strftime("%Y%m%d")
scheme_name = "項目名字"
build = get_info_plist_value(path: "./#{scheme_name}/Info.plist", key: "CFBundleVersion").to_s
if build.include?"#{currentTime}."
# 爲當天版本 計算迭代版本號
lastStr = build[build.length-2..build.length-1]
lastNum = lastStr.to_i
lastNum = lastNum + 1
lastStr = lastNum.to_s
if lastNum < 10
lastStr = lastStr.insert(0,"0")
end
build = "#{currentTime}.#{lastStr}"
else
# 非當天版本 build 號重置
build = "#{currentTime}.01"
end
puts("*************| 更新build #{build} |*************")
# 更改項目 build 號
increment_build_number(
build_number: "#{build}"
)
end
# 打包成功 jenkins 開始提示上傳log
# 使用fastlane上傳蒲公英拿不到二維碼展現, 因此使用jenkins上傳蒲公英
def archiveSuccessLog(outputName,buildConfig)
# 打好的包 移動到 jenkins的 workspace的目錄下,用於jenkins上傳蒲公英匹配包內容。永遠只保留一份
# FileUtils.cp_r("#{$ipa_output_directory}" + "/#{outputName}", "#{$ipa_output_upload_directory}" + "/#{$jenkins_name}.ipa")
puts" --------------------------------------------------------------------------"
puts"| |"
puts"| |"
puts"| 🎉🎉🎉 --------->>>>> #{buildConfig}版本_打包成功 <<<<<-----------🎉🎉🎉 |"
puts"| |"
puts"| 🎉🎉🎉 --------->>>>> Jenkins 能夠開始上傳蒲公英了 <<<<<-----------🎉🎉🎉 |"
puts"| |"
puts"| |"
puts" --------------------------------------------------------------------------"
end
#
# 全部任務腳本
#
platform :ios do
#
# *************| 上傳到測試版本到蒲公英_測試包 |*************
#
desc "上傳到測試版本到蒲公英_測試包"
lane :topgyer_debug do|option|
puts "*************| 開始打包.ipa文件... |*************"
# 自動增長build
updateProjectBuildNumber
# 自動生成證書
cert
# 導出名稱
output_name = "#{scheme_name}_#{version}_#{build}_#{option[:desc]}_#{Time.now.strftime('%Y%m%d%H%M%S')}.ipa"
gym(
# 指定輸出的ipa名稱
output_name: "#{output_name}",
# 指定項目的scheme
scheme: "xxx",
# 是否清空之前的編譯信息 true:是
clean: true,
export_method: "#{ipa_exportMethod}",
export_options: {iCloudContainerEnvironment: 'Development'},
# 指定打包方式,Release 或者 Debug 按需修改
configuration:"Debug",
# 包導出的位置
output_directory: "#{$ipa_output_directory}",
)
# 使用fastlane上傳蒲公英拿不到二維碼展現, 因此使用jenkins上傳蒲公英
archiveSuccessLog(output_name," Debug ")
# 這裏是 fastlane 直接上傳蒲公英
# 若是須要請打開一下注釋,並註釋掉archiveSuccessLog(output_name," Debug ")
# puts "*************| 開始上傳__測試版__本到蒲公英... |*************"
# 配置蒲公英 api_key 和 user_key
# pgyer(api_key: "你的api_key", user_key: "你的user_key", update_description: "#{option[:desc]}")
# puts "*************| 上傳__測試版__到蒲公英🎉🎉🎉成功🎉🎉🎉 |*************"
end
#
# *************| 上傳App Store |*************
#
lane :toappstore do
project_identifier = ""
puts "*************| 開始上傳AppStore... |*************"
# 自動增長build
updateProjectBuildNumber
# 自動生成證書
# cert
# 導出名稱
codesigning_identity = "證書名字"
output_name = "#{scheme_name}_#{version}_#{build}_#{ipa_exportMethod_appStore}_#{Time.now.strftime('%Y%m%d%H%M%S')}.ipa"
gym(
# 指定輸出的ipa名稱
output_name:output_name,
# 指定項目的scheme
scheme:"xxx",
# 是否清空之前的編譯信息 true:是
clean:true,
# 指定打包方式,Release 或者 Debug
configuration:"Release",
# 指定打包方式,目前支持app-store, package, ad-hoc, enterprise, development
export_method:"app-store",
# 上傳的環境
export_options: {iCloudContainerEnvironment: 'Production'},
# 指定輸出文件夾
output_directory:"#{$ipa_output_directory}",
)
deliver(
# 選擇跳過圖片和元數據上傳,本身去配置
skip_screenshots:true,
skip_metadata:true,
# 上傳完成後提交新版本進行審查
submit_for_review: false,
force:true,
)
puts "*************| 上傳AppStore成功🎉 |*************"
puts "*************| 開始上傳__測試版__本到蒲公英... |*************"
# 配置蒲公英 api_key 和 user_key
pgyer(api_key: "你的api_key", user_key: "你的user_key", update_description: "#{option[:desc]}")
puts "*************| 上傳__測試版__到蒲公英🎉🎉🎉成功🎉🎉🎉 |*************"
# 發佈testflight測試
# pilot
end
end
複製代碼
在本身的項目目錄中執行
fastlane topgyer_debug desc:測試
複製代碼
這句話根據本身的lane :topgyer_debug do|option|
修改
成功以後能夠看見 , 若是有上傳蒲公英給的操做 則會告知蒲公英上傳成功,前往蒲公英查看就有新版本了。
17:58:59: --------------------------------------------------------------------------
17:58:59: | |
17:58:59: | |
17:58:59: | 🎉🎉🎉 --------->>>>> Debug 版本_打包成功 <<<<<-----------🎉🎉🎉 |
17:58:59: | |
17:58:59: | 🎉🎉🎉 --------->>>>> Jenkins 能夠開始上傳蒲公英了 <<<<<-----------🎉🎉🎉 |
17:58:59: | |
17:58:59: | |
17:58:59: --------------------------------------------------------------------------
複製代碼
接下來思考的問題是: 不能仍是由咱們開發人員執行 腳本任務來打包上傳蒲公英。
有沒有辦法可以讓測試人員本身去打包呢? 因此接下來咱們要作的就是 讓jenkins執行咱們的fastlane腳本
而且自動生成二維碼展現。測試人員須要測試包的時候開發人員只須要把代碼上傳到對應的分支就行了。
我選擇了一臺專門的機器做爲服務器 來作這件事情, 畢竟在本身電腦上多少會有點影響的。
Jenkins是一個獨立的開源軟件項目,是基於Java開發的一種持續集成工具,用於監控持續重複的工做,旨在提供一個開放易用的軟件平臺,使軟件的持續集成變成可能。前身是Hudson是一個可擴展的持續集成引擎。可用於自動化各類任務,如構建,測試和部署軟件。Jenkins能夠經過本機系統包Docker安裝,甚至能夠經過安裝Java Runtime Environment的任何機器獨立運行。
前面咱們已經安裝了 java JDK 若未安裝請前往上方查看安裝步驟
安裝方式
Jenkins官網地址:jenkins-ci.org/ 下載最新的war包雙擊安裝
使用brew安裝
// 使用brew安裝
brew install jenkins
// 安裝完成後啓動,直接運行jenkins便可啓動服務
jenkins
複製代碼
默認訪問http://localhost:8080/, 可進入jenkins配置頁面。 不要關閉終端不然jenkins會停掉
第一次運行的時候會出現如下界面
根據提示的路徑去 將密碼輸入進去 而後點擊 Continue
會進入:

複製代碼
選擇第一個就能夠了

複製代碼
等待安裝完成

複製代碼
本身設置帳戶密碼
在主頁選擇 manage Jenkins --> Manage Plugins 去下載插件
在這個選項中下載須要的插件,一些基本插件在安裝jenkins的時候都已經下好了,如今只須要下載須要的就能夠了
```ruby
GitLab
Xcode integration // 這個其實能夠不用
Keychains and Provisioning Profiles Management // 配置文件管理
/// 上面這三個 若是須要在jenkins中打包 證書添加須要導入
Upload to pgyer // 上傳蒲公英
Build Name and Description Setter // 設置打好包的名字
description setter // 設置圖片
```
複製代碼
插件安裝好了以後,開始建立項目
回到主頁點擊新建Item

`選擇: 自由風格的項目`

General --> Discard old builds 構建的天數的保持最大的個數。

General --> This project is parameterized --> 添加參數 --> Choice Parameter
這個就是 你在構建的時候能夠先擇 構建環境和分支,(按需添加!)

```ruby
名稱: BUILD_TYPE
選項: debug
release
描述: 打包環境
----------------------------------------------
名稱: GIT_BRANCH
選項: develop
master
3.1.0
3.0.0
2.9.1
2.9.0
描述: 拉取代碼的 git分支
```
源碼管理 --> Git
使用Git管理源碼 我這裏用的是gitlab 的SSH方式管理

```ruby
*/${GIT_BRANCH}
```
git管理就是爲了讓jenkins知道咱們的源碼在哪裏
在第一次構建的時候他回去拉取代碼放到.jenkins的workspace中進行管理
填寫完項目地址,咱們點擊添加按鈕。(branch Specifier 不要忘記修改 這裏上面設置的選擇參數,拉取代碼的分支)
點擊添加按鈕

密鑰獲取: 首先咱們要保證 在git中配置了本身的祕鑰。配置方法 --> [Mac 配置ssh祕鑰](https://www.jianshu.com/p/17fa8d34beb2)
- 類型SSH
- userName 就是git中的名字 能知道是誰上傳下載的就能夠
- PrivateKey ssh中的私鑰,/Users/用戶名/.ssh/id_rsa
- Passphrase git密碼。
- 點擊添加 既能夠了。
若是你的項目很大,須要的clone時間較長的話,須要設置一下超時時間.
jenkins默認的超時時間是10分鐘,根據本身項目設置時間,
這裏有一個[git clone 太慢解決方案](git clone 太慢)。


構建環境 --> Set build Name 設置打出來的包的展現名字 ,若是沒有添加插件 是沒有這個選項的

```ruby
iOS_XXX_${GIT_BRANCH}_${BUILD_TYPE}_build(${BUILD_NUMBER})
```
構建 --> 增長構建步驟 --> Execute shell
複製代碼
fastlane topgyer_debug desc:測試版_Debug
```
而後 保存退出就能夠了
回到首頁 --> build with paramters --> 選擇環境和分支,開始構建


第一次構建須要從Git拉去代碼 若是超時了 更改一下 超時時間 上面有介紹
若是提示 fastlaneCommand not found 點擊此連接 [jenkins執行shell腳本時候出現 fastlaneCommand not found解決方案](jenkins執行shell腳本時候出現 fastlaneCommand not found)
**靜候佳音 Jenkins打包操做 搞定了!!!**
打包成功了以後發現 每次仍是要點擊去蒲公英的連接而後才能下載
經研究了一下怎麼直接把蒲公英的連接放在構建挖成以後
如: 醬事兒~

複製代碼
最開始我使用的是 fastlane 上傳蒲公英的操做,可是想要拿到二維碼不是很容易
最後想到了直接讓 jenkins哪去fastlane打的包而後上傳蒲公英就行了,而後成功了!
下面介紹一下操做步驟
上傳蒲公英
仍是進入到咱們當前項目的配置裏面去 --> 到最後的構建後操做 --> 增長構建後操做步驟
pgyer api_key : 你的蒲公英api_key
// 這個位置本身決定就行了,和fastfile文件中的ipa_output_upload_directory地址 是同樣就行
Scandir: 打的api包的地址
file wildcard: 包名字.ipa
注意你須要 吧 fastlane中的fastfile文件中
###### 這句話的註釋打開 #####
FileUtils.cp_r("#{$ipa_output_directory}" + "/#{outputName}", "#{$ipa_output_upload_directory}" + "/#{$jenkins_name}.ipa")
這個是講打出來的包拷貝一份到 workspace中,保證ipa文件夾的包只有一個最新的。這個包用於上傳蒲公英
複製代碼
獲取二維碼操做:
構建後操做 --> 增長構建後操做步驟 -->Set build description
直接拷貝
<a href="${appBuildURL}"><img src="${appQRCodeURL}" width="118" height="118"/></a>
從上傳蒲公英的信息中獲取圖片
複製代碼
最後一步 回到首頁 --> manager Jenkins --> Configure Floable Security
OK!! 大功告成!!!去首頁去 構建吧! !!
介紹了一下 fastlane 和 Jenkins 的結合使用的過程。比較流水線,若是哪裏不對 還望各位指出,
而後咱們拿到本身電腦的ip http://電腦ip:8080/ 交給測試就行了 要包本身去點構建把,
建議使用網線,這樣ip不容易變。
以前也研究過 所有使用Jenkins實現打包上傳等過程, 可是jenkins須要配置的東西真的不少,證書、xcode、一系列操做,配置錯誤就會失敗 並且還不容易找出問題,因此選擇了fastlane進行打包上傳蒲公英,以後爲了獲取到二維碼直接使用選擇了 jenkins上傳蒲公英。
暫時的解決方法 若是有更好的方法 但願你們多多指出,我也會繼續研究更好的方法。
感謝!
若是你在輸入pod命令的時候出現如下錯誤
/Library/Ruby/Site/2.3.0/rubygems.rb:289:in `find_spec_for_exe': can't find gem cocoapods (>= 0.a) with executable pod (Gem::GemNotFoundException) from /Library/Ruby/Site/2.3.0/rubygems.rb:308:in `activate_bin_path' from /usr/local/bin/pod:23:in `<main>'
複製代碼
緣由1:
緣由2:
若是肯定了都沒有問題 那麼執行一下命令
gem install cocoapods
複製代碼
pod install
複製代碼
以後會發現 pod 已經能夠正常使用了
查找域名所對應的ip 地址,並修改host文件
獲取 github/gitlab.global.ssl.fastly.Net的ip地址
nslookup github.global.ssl.fastly.Net
複製代碼
獲取github.com 的ip地址
nslookup github.com
複製代碼
在etc/hosts 文件中添加
151.101.76.249 github.global.ssl.fastly.net
192.30.253.112 github.com
複製代碼
強制更新DNS緩存
windows
ipconfig /flushdns
複製代碼
linux
sudo /etc/init.d/networking restart
複製代碼
mac
sudo killall -HUP mDNSResponder
複製代碼
在安裝完fastkane以後執行 fastlane xxx desc:xxx 能夠成功打包執行 在jenkins中的shell腳本沒法執行出現如下錯誤
16:20:30 xxx/xxx/xxx/xxx.sh: line 4: fastlane: command not found
16:20:30 Build step 'Execute shell' marked build as failure
16:20:30 New run name is 'iOS_xxx_develop_debug_build(17)'
16:20:30 [UPLOAD TO PGYER] - The build FAILURE, so the file was not uploaded.
16:20:30 [description-setter] Description set:
16:20:30 Finished: FAILURE
複製代碼
解決方法:
在jenkins的shell腳本命令前添加 #!/bin/bash -ilex
命令 eg:
#!/bin/bash -ilex
cd /Users/xxxx/xxxx # 你的項目地址
fastlane topgyer_debug desc:測試版_Debug
複製代碼
**再去執行構建就能夠了。 **
複製代碼
參考:
再次感謝!!