本文主要講解如何使用開源社區(qū)的一個(gè)CI工具Jenkins來(lái)搭建iOS的的CI環(huán)境
1.下載并運(yùn)行Jenkins
在Mac環(huán)境下抑片,我們需要先安裝JDK,然后在Jenkins的官網(wǎng).下載最新的war包。
下載完成后该默,打開終端案糙,進(jìn)入到war包所在目錄,執(zhí)行以下命令:
java -jar jenkins.war --httpPort=8080
進(jìn)行安裝
httpPort指的就是Jenkins所使用的http端口送粱,這里指定8080褪贵,可根據(jù)具體情況來(lái)修改。待Jenkins啟動(dòng)后,在瀏覽器頁(yè)面輸入以下地址:
http://localhost: 8080
安裝完成之后重新打開[http://localhost: 8080](http://localhost: 8080) 此時(shí)需要用戶輸入密碼
該密碼文件在路徑 /Users/Shared/Jenkins/Home/secrets
下無(wú)法直接打開 可以終端輸入
sudo cat /Users/Shared/Jenkins/Home/secrets/initialAdminPassword
獲取內(nèi)容
2.Jenkins自動(dòng)配置安裝
設(shè)置用戶名密碼
3.安裝插件
設(shè)置完畢后進(jìn)入[http://localhost: 8080](http://localhost: 8080) 再配置一些我們所需要的插件
通過(guò)過(guò)濾搜索脆丁,我們安裝幾個(gè)后續(xù)需要使用的插件(可根據(jù)自己實(shí)際需要安裝其他的輔助插件)
- GitLab Plugin
- Gitlab Hook Plugin
- Xcode integration
- Keychains and Provisioning Profiles Management
- Credentials Plugin
4.配置SSH與KeyChain
添加SSH.
在Jenkins管理頁(yè)面世舰,選擇“Credentials”,然后選擇“Global credentials”槽卫,點(diǎn)擊“Add Credentials”跟压,進(jìn)行添加SSH.
Private Key的獲取是在路徑 /Users/管理員用戶名/.ssh
里面,可以通過(guò)終端輸入 open ~/.ssh
打開文件夾,找到id_rsa (注意不是id_rsa.pub
) 將里面內(nèi)容全部復(fù)制 填寫時(shí)
開頭是-----BEGIN RSA PRIVATE KEY-----
結(jié)尾是-----END RSA PRIVATE KEY-----
iOS打包需要簽名文件和證書
在系統(tǒng)管理頁(yè)面歼培,選擇剛剛安裝好的插件“Keychains and Provisioning Profiles Management”震蒋。
上傳keychain
先將鑰匙串登錄里面的相關(guān)開發(fā)者證書復(fù)制到系統(tǒng)目錄下
然后
點(diǎn)擊選取文件 選擇路徑
/Users/管理員用戶名/Library/keychains/login.keychain
然后點(diǎn)擊Upload
Code Signing Identity 填寫復(fù)制的內(nèi)容
然后再點(diǎn)擊選取文件 選擇路徑/Users/管理員用戶名/Library/MobileDevice/Provisioning Profiles
下自己項(xiàng)目所需要的配置文件
成功后如下所示
理論上Jenkins會(huì)把這個(gè)Keychain拷貝到/Users/Shared/Jenkins/Library/keychains
這里,(Library是隱藏文件)躲庄。Provisioning Profiles文件也直接拷貝到/Users/Shared/Jenkins/Library/MobileDevice
文件目錄下查剖。
執(zhí)行上述操作后查看是否有路徑Users/Shared/Jenkins/Library/keychains
與Users/Shared/Jenkins/Library/MobileDevice
如果不存在這兩個(gè)文件夾,自己先創(chuàng)建兩條路徑 然后將login.keychain與login.keychain-db和配置文件夾復(fù)制到相應(yīng)的文件夾內(nèi)
否則容易出現(xiàn)問(wèn)題:
No profile matching 'getgetsetset5CommonTest' found: Xcode couldn't find a profile matching 'getgetsetset5CommonTest'. Install the profile (by dragging and dropping it onto Xcode's dock item) or select a different one in the General tab of the target editor
即 Jenkins 在指定的目錄下沒(méi)有找到對(duì)應(yīng)的profile文件
解決方案:
/Users/Shared/Jenkins/Library/MobileDevice/Provisioning Profiles 看看這個(gè)目錄下面噪窘,有沒(méi)有對(duì)應(yīng)的profile文件笋庄,如果沒(méi)有,可以手動(dòng)拷貝相應(yīng)的profile文件效览,如果中間某些目錄(例如 :MobileDevice)缺失无切,也可以自己手動(dòng)創(chuàng)建
或是下面問(wèn)題
FATAL: Unable to unlock the keychain. Will not set default: file /Users/Shared/Jenkins/Library/Keychains/login.keychain
10.12系統(tǒng)后 keychain變成keychain-db
所以最好將login.keychain與login.keychain-db 都復(fù)制過(guò)去
5.構(gòu)建
插件全部安裝完畢后,下面就可以正式開始自動(dòng)化構(gòu)建了Xね鳌6呒!
1)點(diǎn)擊新建進(jìn)行構(gòu)建新的項(xiàng)目
2)設(shè)置參數(shù)
為了方便舉例子瘦锹,這里使用GitHub上的項(xiàng)目示损,具體情況可以使用自己公司的項(xiàng)目地址
如果出現(xiàn)上述的紅色警告忽冻,請(qǐng)檢查SSH是否配置正確,配置成功如下圖
Poll SCM (poll source code management) 輪詢?cè)创a管理
需要設(shè)置源碼的路徑才能起到輪詢的效果,因?yàn)檫@個(gè)項(xiàng)目主要是用來(lái)測(cè)試符衔,因此時(shí)間設(shè)置為60分鐘才查詢一下
3)添加腳本
Xcode integration也是可以進(jìn)行打包測(cè)試的,但xcode8.0之后老是有一下問(wèn)題失暴,因此直接使用腳本構(gòu)建
點(diǎn)擊 增加構(gòu)建步驟 選擇 Execute shell 填寫
xcodebuild clean build test -workspace JenkinsWithKIFDemo.xcworkspace -scheme JenkinsWithKIFDemo -destination 'platform=iOS,name=coderOnePolo'
本來(lái)是在模擬器進(jìn)行測(cè)試的 但是老是報(bào)錯(cuò)
iPhoneSimulator: Could not launch simulator: -10810
邀层,即使按照SETTING UP JENKINS CI ON A MAC的設(shè)置依舊有問(wèn)題,所以現(xiàn)在先用真機(jī)進(jìn)行測(cè)試
到目前為止 基本的構(gòu)建已經(jīng)完成 后面可以根據(jù)需要再添加遠(yuǎn)程測(cè)試或者添加報(bào)表等需求椅挣。
3.使用KIF進(jìn)行測(cè)試
使用Cocoapod管理头岔,在項(xiàng)目的Podfile file中添加KIF
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
target 'JenkinsWithKIFDemo' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
pod 'AFNetworking','~>2.5.4'
end
target 'JenkinsWithKIFDemoTests' do
#inherit! :search_paths
pod 'KIF', :configurations => ['Debug']
# Pods for testing
end
如果是在舊的項(xiàng)目引入Test,則需要添加以下幾個(gè)配置
之后就是添加測(cè)試代碼了,簡(jiǎn)單的測(cè)試效果如下
使用Jenkins實(shí)現(xiàn)自動(dòng)化測(cè)試
接下來(lái)就是見(jiàn)證成果的時(shí)候了鼠证,可以自己馬上點(diǎn)擊立即構(gòu)建進(jìn)行測(cè)試
配置好后當(dāng)自己的代碼修改提交到遠(yuǎn)程倉(cāng)庫(kù)后峡竣,Jenkins就能馬上自動(dòng)化測(cè)試代碼。(__)
補(bǔ)充 配置時(shí)候的一些坑
qustion:1.A build only device cannot be used to run this target
I get this error when I trying to test an app.
A build only device cannot be used to run this target.
No supported iOS devices are available. Connect a device to run your application or choose a simulated device as the destination.
add -destination 'platform=iOS Simulator,name=iPhone 6,OS=9.3' to your xcodebuild
qustion:2.Could not launch simulator: -10810.
I had exact same issue. It happens because Mac OS X doesn't allow an iOS simulator to run in the BACKGROUND.
If you are constructing a Jenkins CI environment, and trying to unit test via the simulator, you can't run the simulator, because Jenkins is basically run as a DAEMON. This also happens if you're running the tests behind environments like tmux or screen.
Here is a great tutorial to read that can help you fix this.
Cheers!
qustion:3 The target … overrides the OTHER_LDFLAGS
build setting defined in `Pods/Pods.xcconfig
在那個(gè)target下就去設(shè)置哪個(gè)target的setting
This definitely works most of the time:
Go to your target Build Settings -> Other linker flags -> double click . Add $(inherited) to a new line.
If you have problem with "...target overrides the GCC_PREPROCESSOR_DEFINITIONS build setting defined in..." then you must add $(inherited) to your target Build Settings -> Preprocessor Macros
qustion:4 出現(xiàn)錯(cuò)誤 Failed to connect to repository : Command "git ls-remote -h
選擇SSH Username with private key
直接輸入私鑰量九,要完整復(fù)制 ~/.ssh/id_isa里面的內(nèi)容适掰。