iOS持續集成—Jenkins(最新最全)

搭建Jenkins前,請確認mac系統上已經搭建好了Java環境。
從零開始一步一步構建,遇到了很多坑,好在最終success了。

一、 搭建Jenkins

1. 安裝Jenkins

官網上下載pkg安裝包

這裏以jenkis-2.73.3.pkg包爲例

安裝完成後,Safari可能會自動打開,如果沒有自動打開,打開瀏覽器,輸入http://localhost:8080
如果頁面提示不能連接服務器的錯誤,重啓電腦。然後再輸入http://localhost:8080進入登錄頁面。
根據提示,找到/Users/Shared/Jenkins/Home/ 這個目錄,找到secrets文件,顯示簡介,設置所有人都可以讀與寫的權限,並找到initialAdminPassword文件,同樣放開權限,複製出密碼,登錄進去,一路安裝過來,輸入用戶名、密碼、郵件等。最後點擊Save and Finish。

注意:如果安裝之後發現密碼忘記了,從initialAdminPassword文件中複製輸入也錯誤,試了很多方法也不管用,那就卸載重裝Jenkis然後重啓電腦吧。

2. 拷貝文件

2.1

將/Users/用戶名/Library的MobileDevice文件夾拷貝到/Users/Shared/Jenkins/Library下

注意:是將MobileDevice這個文件夾拷貝過去,而不是MobileDevice裏的文件,/Users/Shared/Jenkins/Library 目錄下是沒有MobileDevice這個文件夾,所以先創建一個MobileDevice文件夾。

在終端輸入:

sudo mkdir /Users/Shared/Jenkins/Library/MobileDevice
sudo cp -r /Users/xxx/Library/MobileDevice/ /Users/Shared/Jenkins/Library/MobileDevice/

注意:曾遇到/Users/xxx/Library/下沒有MobileDevice文件夾,原來是新機上沒有安裝Xcode。

2.2

將/Users/用戶名/Library/Keychains/下的login.keychain及login.keychain-db這兩個文件拷貝到/Users/Shared/Jenkins/Library/Keychains文件夾下面

注意:Mac OS 10.12以下的沒有login.keychain-db這個文件,只需要拷貝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/

再將這個login.keychain拷貝到桌面上

在終端輸入:

sudo cp /Users/jiekangwei/Library/Keychains/login.keychain ~/Desktop/

注意:如果/Users/xxx/Library/Keychains/目錄下沒有login.keychain這個文件,就複製login.keychain-db文件到指定目錄,然後將複製在桌面上的該文件改後綴爲.keychain。

3. 鑰匙串權限、~/Library權限以及電腦用戶管理權限

3.1

打開鑰匙串,找到相應的證書

右鍵密鑰顯示簡介,在訪問控制那一項裏勾選允許所有應用程序訪問此項目,並存儲更改。

3.2

找到Library資源庫文件夾,設置共享與訪問權限,everyone讀與寫都可以


3.3

進入mac系統偏好設置 — 用戶與羣組 — 其他用戶 — jenkins ,勾選允許用戶管理這臺電腦,並右鍵選擇高級選項,將名字改爲jenkins

4. Jenkins插件及系統設置

進入管理插件,在可選插件裏所有關鍵字,下載安裝插件Keychains and Provisioning ProfilesManagement和 Xcode integration

4.1

選取拷 在桌上login.keychain以及項 相對應的Provisioning Profile 並upload

注意:開發證書和provision profile 一定要對應正確,不然命令行打包會認證不了而失敗

password 一定要填寫正確的鑰匙串的密碼

Code Signing Identity 是從鑰匙 找到相應的證書,然後顯示簡介複製 來,這 Identities 少要填 兩項,是因爲這 放 個的話後 的設置 Code Signing Identity 能 動對應上。

Provisioning Profiles Directory Path 這 項 填寫/Users/用戶名/Library/ MobileDevice/Provisioning Profiles

注意:路徑裏Provisioning Profiles是Provisioning\ Profiles

點擊Save

4.2

安裝完Xcode integration後進 系統設置。

Xcode Builder

填寫鑰匙串密碼

5. 新建

5.1

填寫名字,選擇構建自由風格,點擊ok


進入Project,進入配置

5.2

General

項目名稱,這裏可以和git項目名稱一樣,也可以另起一個名稱。

5.3

源碼管理

選擇git,然後在Repository URL 上填寫git倉庫地址


選擇add

選擇SSH Username with private key
Username 爲上傳到git的用戶名,Private Key從id_rsa文件裏複製出來


5.4

構建觸發器,可選填

5.5

構建環境

選擇Keychains andProvisioning Profiles Management和Mobile Provisioning Profiles


Code Signing Identity 第一次不能自動顯示出來沒關係,保存,然後第二次進來就自動選上了。

5.6

構建

有兩種打包方式,一是用Xcode打包,而是用Shell腳本打包,當Xcode版本爲9或者以上,應該使用Shell腳本打包。

因爲 Xcode 9 默認不允許訪問鑰匙串的內容,必須要設置 allowProvisioningUpdates 纔會允許,但是由於 Xcode integration 插件封閉,並不能對其進行修改加上這個屬性,所以使用 Shell 腳本代替插件。

5.6.1
這裏先介紹Xcode打包,項目使用的是Xcode 8.1

General build settings

這裏Target請於Xcode項目中Target的名字對應
Clean before build設置爲YES
Configuration選擇Release)
Output directory爲.ipa的輸出路徑,可選填

Code signing & OS X keychain options

teamId 爲對應證書括號裏面有,如果沒找到,請在蘋果開發網站登陸進去,從Membership上查看。選擇Unlock Keychain,填寫鑰匙串密碼

Advanced Xcode build options

Xcode Schema Files 填寫Xcode項目的schema
因爲項目含有Workspace,所以填寫Xcode Workspace File,填寫該項目xcworkspace文件的絕對路徑
Build output directory 填寫${WORKSPACE}/build/ ,這裏注意是大括號


打開工程,mange schemes,勾選項目schemes對應的shared,該scheme爲填寫的Xcode Schema Files的內容


ok,點擊保存,使用Xcode構建配置完畢

5.6.2

選擇Execute shell 打包


command 寫入腳本:

xcodebuild clean -workspace 工程名稱.xcworkspace -scheme 工程名稱 -configuration Release

xcodebuild archive -workspace 工程名稱.xcworkspace -scheme 工程名稱 -archivePath 工程名稱.xcarchive

xcodebuild -exportArchive -archivePath 工程名稱.xcarchive -exportOptionsPlist /Users/用戶名/ExportOptions.plist -allowProvisioningUpdates -exportPath ./

ExportOptions.plist 注意大小寫,先打包工程,將ExportOptions.plist文件拷貝到/Users/用戶名/或者指定的目錄,記得腳本中/Users/用戶名/ExportOptions.plist 對應的是該文件的路徑。




Next ,然後Export



6. 上傳

這裏把項目上傳到fir.im平臺爲例

之前選擇使用命令行

但遺憾的是用終端安裝fir-cli總是失敗,於是就選擇使用fir.im插件

這裏選擇使用fir.im插件, 進入 官網下載,下載jenkins插件

然後進入Jenkins,選擇插件管理 — 高級

選擇下載好的fir-plugin-1.9.5.hpi,上傳並安裝完後

進入項目的配置頁面,在構建後操作裏選擇Upload to fir.im,填寫fir.im token


好了,點擊保存,立即構建吧

如果構建時後臺打印出以下信息,恭喜你,你構建成功了


二、配置Jenkins錯誤

1、 拷貝文件錯誤

Failed to copy /Users/Shared/Jenkins/Home/kpp_upload/xxxx.mobileprovision to /Users/xxx/Library/MobileDevice/Provisioning Profiles/xxxx.mobileprovision

解決:文件拷貝失敗。1.查看Library讀寫權限是否開放;2. 查看系統管理 — Keychains and Provisioning Profiles Management,查看Provisioning Profiles Directory path 是否有誤

2、 證書文件找不到

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/管理員用戶名/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.

解決:確認鑰匙串密碼是正確的

4、 pod沒有權限

/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

解決:在終端打開項目目錄並輸入 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插件版本過低,使用最新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

解決:沒有安裝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

解決:進入mac 系統偏好設置 — 用戶與羣組 — 其他用戶 — jenkins ,勾選允許用戶管理這臺電腦

8、

FATAL: Failed to mkdirs: /Users/xxx/Desktop/ipa java.io.IOException: Failed to mkdirs: /Users/xxx/Desktop/ipa

解決:Output directory 路徑錯誤

9、

FATAL: Build directory does not exist at /$(WORKSPACE)/build. Potential configuration issue. Build step 'Xcode' marked build as failure

解決:Build output directory 填寫錯誤,正確爲${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 路徑錯誤

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.

解決:打包工程導出ipa時選擇手動選擇證書

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 名稱大小寫錯誤

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/用戶名/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

解決:用腳本構建時,腳本輸寫有誤

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 文件名稱有誤,或者.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 對應不上 certificate證書,把鑰匙串相應的證書刪掉,重新從蘋果商店下載證書和provisioning profile,在Jenkis —系統設置— Keychains and Provisioning Profiles Management插件裏全部刪除,執行前文 2、拷貝文件 操作,然後upload新的login.keychain和provisioning profile

關注微信公衆號:iOSSir,每日更新蘋果資訊、技術乾貨!


原文地址:https://www.jianshu.com/p/9cb3d8c8c78d

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章