搭建Jenkins前铸磅,請(qǐng)確認(rèn)mac系統(tǒng)上已經(jīng)搭建好了Java環(huán)境筷弦。
從零開(kāi)始一步一步構(gòu)建,遇到了很多坑术唬,好在最終success了薪伏。
一、 搭建Jenkins
1. 安裝Jenkins
從官網(wǎng)上下載pkg安裝包
這里以jenkis-2.73.3.pkg包為例
安裝完成后粗仓,Safari可能會(huì)自動(dòng)打開(kāi)嫁怀,如果沒(méi)有自動(dòng)打開(kāi),打開(kāi)瀏覽器借浊,輸入http://localhost:8080
如果頁(yè)面提示不能連接服務(wù)器的錯(cuò)誤塘淑,重啟電腦。然后再輸入http://localhost:8080進(jìn)入登錄頁(yè)面蚂斤。
根據(jù)提示存捺,找到/Users/Shared/Jenkins/Home/ 這個(gè)目錄,找到secrets文件曙蒸,顯示簡(jiǎn)介捌治,設(shè)置所有人都可以讀與寫(xiě)的權(quán)限,并找到initialAdminPassword文件纽窟,同樣放開(kāi)權(quán)限肖油,復(fù)制出密碼,登錄進(jìn)去臂港,一路安裝過(guò)來(lái)森枪,輸入用戶(hù)名视搏、密碼、郵件等疲恢。最后點(diǎn)擊Save and Finish凶朗。
注意:如果安裝之后發(fā)現(xiàn)密碼忘記了,從initialAdminPassword文件中復(fù)制輸入也錯(cuò)誤显拳,試了很多方法也不管用棚愤,那就卸載重裝Jenkis然后重啟電腦吧。
2. 拷貝文件
2.1
將/Users/用戶(hù)名/Library的MobileDevice文件夾拷貝到/Users/Shared/Jenkins/Library下
注意:是將MobileDevice這個(gè)文件夾拷貝過(guò)去杂数,而不是MobileDevice里的文件宛畦,/Users/Shared/Jenkins/Library 目錄下是沒(méi)有MobileDevice這個(gè)文件夾,所以先創(chuàng)建一個(gè)MobileDevice文件夾揍移。
在終端輸入:
sudo mkdir /Users/Shared/Jenkins/Library/MobileDevice
sudo cp -r /Users/xxx/Library/MobileDevice/ /Users/Shared/Jenkins/Library/MobileDevice/
注意:曾遇到/Users/xxx/Library/下沒(méi)有MobileDevice文件夾次和,原來(lái)是新機(jī)上沒(méi)有安裝Xcode。
2.2
將/Users/用戶(hù)名/Library/Keychains/下的login.keychain及l(fā)ogin.keychain-db這兩個(gè)文件拷貝到/Users/Shared/Jenkins/Library/Keychains文件夾下面
注意:Mac OS 10.12以下的沒(méi)有l(wèi)ogin.keychain-db這個(gè)文件那伐,只需要拷貝login.keychain文件踏施。
在終端輸入:
cd /Users/Shared/Jenkins/Library/
sudo mkdir Keychains
sudo cp /Users/xxx/Library/Keychains/login.keychain ./Keychains/
sudo cp /Users/xxx/Library/Keychains/login.keychain-db ./Keychains/
再將這個(gè)login.keychain拷貝到桌面上
在終端輸入:
sudo cp /Users/jiekangwei/Library/Keychains/login.keychain ~/Desktop/
注意:如果/Users/xxx/Library/Keychains/目錄下沒(méi)有l(wèi)ogin.keychain這個(gè)文件,就復(fù)制login.keychain-db文件到指定目錄罕邀,然后將復(fù)制在桌面上的該文件改后綴為.keychain畅形。
3. 鑰匙串權(quán)限、~/Library權(quán)限以及電腦用戶(hù)管理權(quán)限
3.1
打開(kāi)鑰匙串诉探,找到相應(yīng)的證書(shū)
右鍵密鑰顯示簡(jiǎn)介日熬,在訪(fǎng)問(wèn)控制那一項(xiàng)里勾選允許所有應(yīng)用程序訪(fǎng)問(wèn)此項(xiàng)目,并存儲(chǔ)更改肾胯。
3.2
找到Library資源庫(kù)文件夾竖席,設(shè)置共享與訪(fǎng)問(wèn)權(quán)限,everyone讀與寫(xiě)都可以
3.3
進(jìn)入mac系統(tǒng)偏好設(shè)置 — 用戶(hù)與群組 — 其他用戶(hù) — jenkins 敬肚,勾選允許用戶(hù)管理這臺(tái)電腦毕荐,并右鍵選擇高級(jí)選項(xiàng),將名字改為jenkins
4. Jenkins插件及系統(tǒng)設(shè)置
進(jìn)入管理插件艳馒,在可選插件里所有關(guān)鍵字东跪,下載安裝插件Keychains and Provisioning ProfilesManagement和 Xcode integration
4.1
選取拷 在桌上login.keychain以及項(xiàng) 相對(duì)應(yīng)的Provisioning Profile 并upload
注意:開(kāi)發(fā)證書(shū)和provision profile 一定要對(duì)應(yīng)正確,不然命令行打包會(huì)認(rèn)證不了而失敗
password 一定要填寫(xiě)正確的鑰匙串的密碼
Code Signing Identity 是從鑰匙 找到相應(yīng)的證書(shū)鹰溜,然后顯示簡(jiǎn)介復(fù)制 來(lái)虽填,這 Identities 少要填 兩項(xiàng),是因?yàn)檫@ 放 個(gè)的話(huà)后 的設(shè)置 Code Signing Identity 能 動(dòng)對(duì)應(yīng)上曹动。
Provisioning Profiles Directory Path 這 項(xiàng) 填寫(xiě)/Users/用戶(hù)名/Library/ MobileDevice/Provisioning Profiles
注意:路徑里Provisioning Profiles是Provisioning\ Profiles
點(diǎn)擊Save
4.2
安裝完Xcode integration后進(jìn) 系統(tǒng)設(shè)置斋日。
Xcode Builder
填寫(xiě)鑰匙串密碼
5. 新建
5.1
填寫(xiě)名字,選擇構(gòu)建自由風(fēng)格墓陈,點(diǎn)擊ok
進(jìn)入Project恶守,進(jìn)入配置
5.2
General
項(xiàng)目名稱(chēng)第献,這里可以和git項(xiàng)目名稱(chēng)一樣,也可以另起一個(gè)名稱(chēng)兔港。
5.3
源碼管理
選擇git庸毫,然后在Repository URL 上填寫(xiě)git倉(cāng)庫(kù)地址
選擇add
選擇SSH Username with private key
Username 為上傳到git的用戶(hù)名,Private Key從id_rsa文件里復(fù)制出來(lái)
5.4
構(gòu)建觸發(fā)器衫樊,可選填
5.5
構(gòu)建環(huán)境
選擇Keychains andProvisioning Profiles Management和Mobile Provisioning Profiles
Code Signing Identity 第一次不能自動(dòng)顯示出來(lái)沒(méi)關(guān)系飒赃,保存,然后第二次進(jìn)來(lái)就自動(dòng)選上了科侈。
5.6
構(gòu)建
有兩種打包方式载佳,一是用Xcode打包,而是用Shell腳本打包臀栈,當(dāng)Xcode版本為9或者以上蔫慧,應(yīng)該使用Shell腳本打包。
因?yàn)?Xcode 9 默認(rèn)不允許訪(fǎng)問(wèn)鑰匙串的內(nèi)容权薯,必須要設(shè)置 allowProvisioningUpdates 才會(huì)允許姑躲,但是由于 Xcode integration 插件封閉,并不能對(duì)其進(jìn)行修改加上這個(gè)屬性盟蚣,所以使用 Shell 腳本代替插件黍析。
5.6.1
這里先介紹Xcode打包,項(xiàng)目使用的是Xcode 8.1
General build settings
這里Target請(qǐng)于Xcode項(xiàng)目中Target的名字對(duì)應(yīng)
Clean before build設(shè)置為YES
Configuration選擇Release)
Output directory為.ipa的輸出路徑刁俭,可選填
Code signing & OS X keychain options
teamId 為對(duì)應(yīng)證書(shū)括號(hào)里面有,如果沒(méi)找到韧涨,請(qǐng)?jiān)谔O(píng)果開(kāi)發(fā)網(wǎng)站登陸進(jìn)去牍戚,從Membership上查看。選擇Unlock Keychain虑粥,填寫(xiě)鑰匙串密碼
Advanced Xcode build options
Xcode Schema Files 填寫(xiě)Xcode項(xiàng)目的schema
因?yàn)轫?xiàng)目含有Workspace如孝,所以填寫(xiě)Xcode Workspace File,填寫(xiě)該項(xiàng)目xcworkspace文件的絕對(duì)路徑
Build output directory 填寫(xiě)${WORKSPACE}/build/ 娩贷,這里注意是大括號(hào)
打開(kāi)工程第晰,mange schemes,勾選項(xiàng)目schemes對(duì)應(yīng)的shared彬祖,該scheme為填寫(xiě)的Xcode Schema Files的內(nèi)容
ok茁瘦,點(diǎn)擊保存,使用Xcode構(gòu)建配置完畢
5.6.2
選擇Execute shell 打包
command 寫(xiě)入腳本:
xcodebuild clean -workspace 工程名稱(chēng).xcworkspace -scheme 工程名稱(chēng) -configuration Release
xcodebuild archive -workspace 工程名稱(chēng).xcworkspace -scheme 工程名稱(chēng) -archivePath 工程名稱(chēng).xcarchive
xcodebuild -exportArchive -archivePath 工程名稱(chēng).xcarchive -exportOptionsPlist /Users/用戶(hù)名/ExportOptions.plist -allowProvisioningUpdates -exportPath ./
ExportOptions.plist 注意大小寫(xiě)储笑,先打包工程甜熔,將ExportOptions.plist文件拷貝到/Users/用戶(hù)名/或者指定的目錄,記得腳本中/Users/用戶(hù)名/ExportOptions.plist 對(duì)應(yīng)的是該文件的路徑突倍。
Next 腔稀,然后Export
6. 上傳
這里把項(xiàng)目上傳到fir.im平臺(tái)為例
之前選擇使用命令行
但遺憾的是用終端安裝fir-cli總是失敗盆昙,于是就選擇使用fir.im插件
這里選擇使用fir.im插件, 進(jìn)入 官網(wǎng)下載焊虏,下載jenkins插件
然后進(jìn)入Jenkins淡喜,選擇插件管理 — 高級(jí)
選擇下載好的fir-plugin-1.9.5.hpi,上傳并安裝完后
進(jìn)入項(xiàng)目的配置頁(yè)面诵闭,在構(gòu)建后操作里選擇Upload to fir.im悦污,填寫(xiě)fir.im token
好了,點(diǎn)擊保存陷猫,立即構(gòu)建吧
如果構(gòu)建時(shí)后臺(tái)打印出以下信息硝逢,恭喜你,你構(gòu)建成功了
二润歉、配置Jenkins錯(cuò)誤
1模狭、 拷貝文件錯(cuò)誤
Failed to copy /Users/Shared/Jenkins/Home/kpp_upload/xxxx.mobileprovision to /Users/xxx/Library/MobileDevice/Provisioning Profiles/xxxx.mobileprovision
解決:文件拷貝失敗。1.查看Library讀寫(xiě)權(quán)限是否開(kāi)放踩衩;2. 查看系統(tǒng)管理 — Keychains and Provisioning Profiles Management嚼鹉,查看Provisioning Profiles Directory path 是否有誤
2、 證書(shū)文件找不到
Check dependencies Code Sign error: No matching provisioning profile found: Your build settings specify a provisioning profile with the UUID “75025357-245d-448b-9343-96148a0xxxxx”, however, no such provisioning profile was found.
解決:將/Users/管理員用戶(hù)名/Library下的MobileDevice文件夾拷貝到/Users/Shared/Jenkins/Library下面
security: SecKeychainUnlock login.keychain: The user name or passphrase you entered is not correct. FATAL: Unable to unlock the keychain.
解決:確認(rèn)鑰匙串密碼是正確的
4驱富、 pod沒(méi)有權(quán)限
/Users/xxx/workspace/xxxx/Pods/Target Support Files/Pods-xxxx/Pods-xxxx-resources.sh: line 7: /Users/xxx/workspace/xxxx/Pods/resources-to-copy-DiscoverPanjin.txt: Permission denied
解決:在終端打開(kāi)項(xiàng)目目錄并輸入 sudo chmod 777 Pods
5锚赤、 使用fir插件上傳失敗
ERROR: Can not found mapping or dysm file in /Users/Shared/Jenkins/Home/jobs/xxxx/workspace. Deployment failed : Error while get details for update id = xxxxx Build step 'Upload to fir.im' marked build as failure
解決:fir插件版本過(guò)低,使用最新fir插件試試
6褐鸥、使用命令行上傳失敗
fir p/Users/Shared/Jenkins/Home/jobs/xxx/workspace/build/xxx.ipa -T 4dc6d4c3ebe1969b3c76353514xxxxx /Users/Shared/Jenkins/tmp/jenkins3233517671203652013.sh: line 2: fir: command not found Build step 'Execute shell' marked build as failure
解決:沒(méi)有安裝fir-cli命令
7线脚、
Will not set default: file /Users/Shared/Jenkins/Library/Keychains/login.keychain-db is owned by UID=0, but we have UID=262 security: SecKeychainSetDomainDefault user: write permissions error
解決:進(jìn)入mac 系統(tǒng)偏好設(shè)置 — 用戶(hù)與群組 — 其他用戶(hù) — jenkins ,勾選允許用戶(hù)管理這臺(tái)電腦
8叫榕、
FATAL: Failed to mkdirs: /Users/xxx/Desktop/ipa java.io.IOException: Failed to mkdirs: /Users/xxx/Desktop/ipa
解決:Output directory 路徑錯(cuò)誤
9浑侥、
FATAL: Build directory does not exist at /$(WORKSPACE)/build. Potential configuration issue. Build step 'Xcode' marked build as failure
解決:Build output directory 填寫(xiě)錯(cuò)誤,正確為${WORKSPACE}/build
10晰绎、
== Available schemes [workspace] $ /usr/bin/xcodebuild -list -workspace /Users/xxx/workspace/xxxx.xcworkspace xcodebuild: error: '/Users/xxx/workspace/xxxx.xcworkspace' does not exist.
解決:Xcode Workspace File 路徑錯(cuò)誤
11寓落、
rror Domain=IDEProfileLocatorErrorDomain Code=1 "No profiles for 'xxxxxxx' were found" UserInfo={NSLocalizedDescription=No profiles for 'xxxxxxx' were found, NSLocalizedRecoverySuggestion=Xcode couldn't find any iOS Ad Hoc provisioning profiles matching 'xxxxxxx'. Automatic signing is disabled and unable to generate a profile. To enable automatic signing, pass -allowProvisioningUpdates to xcodebuild.
解決:打包工程導(dǎo)出ipa時(shí)選擇手動(dòng)選擇證書(shū)
12、
+ xcodebuild -exportArchive -archivePath /xxx.xcarchive -exportPath -exportOptionsPlist /Users/xxx/exportOptionsPlist.plist
xcodebuild: error: The flag -exportOptionsPlist is required when specifying -exportArchive.
Build step 'Execute shell' marked build as failure
解決:拷貝到指定路徑的ExportOptions.plist 名稱(chēng)大小寫(xiě)錯(cuò)誤
13荞下、
create and update profiles, app IDs, and certificates. For manually signed targets, xcodebuild will download missing or updated provisioning profiles. Requires a developer account to have been added in Xcode's Accounts preference pane. -allowProvisioningDeviceRegistration Allow xcodebuild to register your destination device on the developer portal if necessary. This flag only takes effect if -allowProvisioningUpdates is also passed. Build step 'Xcode' marked build as failure
解決:腳本上缺少allowProvisioningUpdates這一句
14伶选、
$ /usr/bin/xcodebuild -scheme xxxx -workspace /Users/用戶(hù)名/workSpace/xxxx/xxx.xcworkspace -configuration Release clean archive -archivePath "/Users/Shared/Jenkins/Home/workspace/xxx/build/xxxx.xcarchive" "BUILD_DIR=/Users/Shared/Jenkins/Home/workspace/xxx/build/" DEVELOPMENT_TEAM=EFL2T3NT3W -exportArchive -exportPath /Users/Shared/Jenkins/Home/workspace/xxx/build/ -exportOptionsPlist /Users/xxx/options.plist
xcodebuild: error: The flag -scheme cannot be specified along with -exportArchive.
或者
[iOS Panjin] $ /bin/sh -xe /Users/Shared/Jenkins/tmp/jenkins410183735701877848.sh
+ echo '================= Clean一下 ================='
================= Clean一下 =================
+ xcodebuild clean -workspace .xcworkspace -configuration -target
xcodebuild: error: If you specify a workspace then you must also specify a scheme. Use -list to see the schemes in this workspace.
Build step 'Execute shell' marked build as failure
Build step 'Xcode' marked build as failure
或者:
[iOS xxx] $ /bin/sh -xe /Users/Shared/Jenkins/tmp/jenkins8239787762841783385.sh
+ xcodebuild clean -workspace .xcworkspace -scheme -configuration
xcodebuild: error: '.xcworkspace' does not exist.
Build step 'Execute shell' marked build as failure
或者:
+ xcodebuild -exportArchive -archivePath /xxx.xcarchive -exportOptionsPlist /Users/xxx/ExportOptions.plist -allowProvisioningUpdates -exportPath ./
error: archive not found at path '/xxx.xcarchive'
Build step 'Execute shell' marked build as failure
解決:用腳本構(gòu)建時(shí),腳本輸寫(xiě)有誤
15尖昏、
+ xcodebuild -exportArchive -archivePath xxxx.xcarchive -exportOptionsPlist /Users/xxx/ExportOptions.plist -allowProvisioningUpdates -exportPath ./
error: archive not found at path '/Users/Shared/Jenkins/Home/workspace/xxx/xxx.xcarchive'
** EXPORT FAILED **
Build step 'Execute shell' marked build as failure
解決: /Users/Shared/Jenkins/Home/workspace/ 下.xcarchive 文件名稱(chēng)有誤仰税,或者.xcarchive文件路徑有誤
16、
Error Domain=IDECodesignResolverErrorDomain Code=1 "No certificate for team 'xxxxx' matching 'DB8C944F71D475DE773696BD637xxxxxxx' found" UserInfo={IDEProvisioningError_UserInfoKey_IDEProvisioningErrorSpecifier=DB8C944F71D475DE773696BD6374C5xxxxx, NSLocalizedDescription=No certificate for team 'xxxxx' matching 'DB8C944F71D475DE773696BD6374C5BBxxxxxxx' found, NSLocalizedRecoverySuggestion=Specify a different signing certificate for "signingCertificate" in your Export Options property list., IDEProvisioningError_UserInfoKey_IDEProvisioningErrorTeam=<IDEProvisioningBasicTeam 0x7fee800853e0: teamID: xxxxxx, teamName: (null)>, IDEProvisioningError_UserInfoKey_IDEProvisioningErrorPlatform=com.apple.platform.iphoneos, IDEProvisioningError_UserInfoKey_IDEProvisioningErrorAction=5}
解決:keychains下的provisioning profile 對(duì)應(yīng)不上 certificate證書(shū)抽诉,把鑰匙串相應(yīng)的證書(shū)刪掉肖卧,重新從蘋(píng)果商店下載證書(shū)和provisioning profile,在Jenkis —系統(tǒng)設(shè)置— Keychains and Provisioning Profiles Management插件里全部刪除掸鹅,執(zhí)行前文 2塞帐、拷貝文件 操作拦赠,然后upload新的login.keychain和provisioning profile
關(guān)注微信公眾號(hào):iOSSir,每日更新蘋(píng)果資訊葵姥、技術(shù)干貨荷鼠!