1.下載jenkins
http://jenkins-ci.org/content/thank-you-downloading-os-x-installer
安裝完之后直接輸入http://localhost:8080
即可進(jìn)入jekins界面,如果不能進(jìn)入請在終端中輸入java -version 查看是否安裝jdk和當(dāng)前系統(tǒng)的java環(huán)境版本,確保在java7及以上
如果是采用的war包,需要執(zhí)行
?java -jar jenkins.war -httpPort=10000 (端口號自定義設(shè)置)
2.安裝插件
打開jenkins后需要安裝git plugin、gitlab plugin、github plugin、xcode plugin、credential 和keychain 相關(guān)的插件,git plugin安裝2.4.0以上,低版本會有問題,具體問題見后面。
3.配置
安裝完插件之后需要進(jìn)行一些配置。
首先要創(chuàng)建一個jenkins任務(wù)。
具體創(chuàng)建過程如下所示:
點(diǎn)擊左側(cè)的“新建”選項,然后填寫item名稱,選擇“構(gòu)建一個自由風(fēng)格的軟件項目”。點(diǎn)擊確定!

點(diǎn)擊確定后進(jìn)入如下界面

這部分根據(jù)需要自己填寫,我們的項目暫時沒有用到該部分,由于我們的代碼由gitlab管理,并且還引入了github上的第三方庫,所以在源碼管理的時候選擇git這個選項

然后填寫代碼所在的git地址,如git@gitlab.xxx.git,然后填寫Credentials,如果之前沒有配置過credentials,可以點(diǎn)擊旁邊的“Add”,

這里選擇 “SSH Username with private key” scope選擇Global還是system酌情選擇,兩者的區(qū)別在于Global的credential可以在別的項目中使用,而system的credential只能在本任務(wù)中使用。
SSH key 生成方法:
現(xiàn)在本地生成ssh key
ssh-keygen -t rsa -C "email address"
這里的email address替換成自己的email
cat ~/.ssh/id_rsa.pub
將輸出的字符串copy
登錄gitlab進(jìn)入ssh key標(biāo)簽頁添加sshkey把剛才copy的字符串粘貼即可
使用ssh -Tgit@gitlab.xxxx.com驗(yàn)證
出現(xiàn)welcome to gitLab xxx表示ssh key添加成功
其中id_rsa是默認(rèn)的路徑 ,可以替換成自己的名字
前面提到過如果git plugin的版本過低則會出現(xiàn)如下錯誤

上面提到我們還引入了github上的第三方庫,為了在編譯過程中能順暢的訪問github同樣要給github添加sshkey ,否則在取第三方庫的時候會出現(xiàn)不能連接github的情況。
配置完源代碼之后接下來就是配置構(gòu)建環(huán)境。
構(gòu)建環(huán)境的方法有兩種,一種是使用xcode插件配置,另一種是直接使用命令行配置,其實(shí)兩種方法的原理都是一樣,只不過使用xcode配置時更直觀,命令行直接使用xcodebuild命令配置參數(shù)。使用xcode插件來構(gòu)建時需要上傳keychain和配置證書和provisioning profile,這里我們使用命令行的方式來做,直接粗暴!如果使用命令行來做,可以少下載幾個插件。
命令行主要使用xcodebuild和xcrun來做。
xcodebuild 的參數(shù)包括但不限于以下幾個:
-workspace, -target , -configuration, -scheme
在Xcode的product中有許多scheme,-scheme參數(shù)用于指定編譯時選用哪個scheme
-workspace用于多個工程的情況,尤其是使用了cocopods來管理第三方庫時需要設(shè)置該參數(shù),舉個列子,如果工程中使用了cocopods引入了AFNetworking,在編譯時需要加上-workspace參數(shù),否則在鏈接階段會報無法鏈接lAFNetworking.o,使用了workspace需要配合scheme參數(shù),因?yàn)槭褂胏ocopods集成了第三方控件之后會有多個scheme,所以需要指定一個編譯的scheme。
-configuration可以指定編譯成Debug還是release。
這些參數(shù)都可以在jenkins的xcode插件里配置,需要注意的是不用帶后綴,如配置workspace時只需指定AAA即可,不用AAA.xcworkspace,編譯器會自動給添加上,但是使用命令行需要自己寫后綴。
以下是一段xcodebuild命令的例子:
xcodebuild -scheme Sherlock -workspace Sherlock.xcworkspace -configuration Release build CONFIGURATION_BUILD_DIR=/Users/knight/.jenkins/jobs/Sherlock/workspace/build "CODE_SIGN_IDENTITY=iPhone Developer:xxxx(G2...4M)"
指定code sign identity時要注意唯一性,在keychain中有多個名字一樣的證書時,編譯過程中會報如下警告:
ambiguous?(matches?"iPhone?Distribution: xxxxxxxx."?and?"iPhone?Distribution:xxxxxxxx"?in?/Users/knight/Library/Keychains/login.keychain)
編譯成功之后是打包ipa,這個過程主要使用xcrun命令,在xcrun過程中會做codesign,用證書給provisioning profile做簽名,在此之前需要解鎖鑰匙串。
xcrun命令舉例如下:
xcrun -sdk iphoneos PackageApplication -v /Users/knight/.jenkins/jobs/Sherlock/workspace/build/Sherlock.app -o /Users/knight/Desktop/ipas/Sherlcok_16_2015.11.26.ipa --embed /Users/knight/Desktop/jekinsCI/xcxcxcd.mobileprovision --sign "iPhone Developer: fdafsfdsf (G2…….cC)" --verbose
在打包階段可能會遇到下面的問題:
error:?/usr/bin/codesign?--force?--preserve-metadata=identifier,entitlements,resource-rules?--sign?iPhone?Distribution:sdfdsfdsfsdffdsfds.?--resource-rules=/var/folders/xy/bxy7kp7932jg2cs266dm7prh0000gn/T/2RL2F3s8TI/Payload/Sherlock.app/ResourceRules.plist?--entitlements?/var/folders/xy/bxy7kp7932jg2cs266dm7prh0000gn/T/2RL2F3s8TI/entitlements_plistFAjhf4Lw?/var/folders/xy/bxy7kp7932jg2cs266dm7prh0000gn/T/2RL2F3s8TI/Payload/Sherlock.app?failed?with?error?1.?Output:
這個是由于在工程中沒有對code sign resource rule path做配置,只要將其配置為$(SDKROOT)/ResourceRules.plist即可,code sign resource rule path在Build settings中設(shè)置。
因此要使用命令行來打包可以在構(gòu)建部分選擇Execute Shell

security unlock-keychain -p 'fasfsdf' ~/Library/Keychains/login.keychain
xcodebuild -target Sherlock clean
xcodebuild -scheme Sherlock -workspace Sherlock.xcworkspace -configuration Release build CONFIGURATION_BUILD_DIR=/Users/knight/.jenkins/jobs/Sherlock/workspace/build "CODE_SIGN_IDENTITY=iPhone Developer: dfsadf bfdasin (fdsfsdfdd)"
xcrun -sdk iphoneos PackageApplication -v /Users/knight/.jenkins/jobs/Sherlock/workspace/build/Sherlock.app -o /Users/knight/Desktop/ipas/Sherlcok_16_2015.11.26.ipa --embed /Users/knight/Desktop/jekinsCI/sherlock.mobileprovision --sign "iPhone Developer: dasfsfdf(fdsfdsfsdaf)" --verbose