前提條件:Mac系統,并且已經安裝xcode,homedrew,Java,如果專案工程中集成了cocoapod,也需要安裝cocoapod
Jenkins打包方式分為兩種,一種是xcode插件化配置打包,一種是腳本配置化打包,本人更傾向于腳本打包,因此接下來主要介紹iOS腳本打包配置程序
一.通過homebrew安裝Jenkins
打開終端,執行命令列
brew install jenkins(注:目前通過這個命令列下載的Jenkins,安裝的插件keychain無法正常上傳檔案)
建議下載更低版本的Jenkins
brew install jenkins-lts
通過homedrew安裝后jenkins安裝好后所在的位置:
實際安裝位置:/usr/local/Cellar/jenkins/
組態檔所在位置:/usr/local/opt/jenkins/
作業空間位置:/Users/用戶/.jenkins
進入作業空間位置:cd ~/.jenkins
二.啟動Jenkins
打開一個終端,執行命令
brew services start jenkins-lts
重啟Jenkins
brew services restart jenkins-lts
關閉Jenkins
brew services stop jenkins-lts
第一次服務開啟成功后,在瀏覽器中輸入http://localhost:8080,會出現一個讓你輸入Jenkins初始密碼的頁面
獲取Jenkins初始密碼
重新打開一個終端,執行命令
Ls -a

找到.jenkins檔案,執行命令
cd .jenkins
ls -a
cd secrets
ls -a
直到獲取到initialAdminPassword檔案,執行命令
cat initialAdminPassword
至此將獲取到的初始密碼輸入Jenkins,重啟Jenkins
brew services restart jenkins
重新在瀏覽器輸入http://localhost:8080,會進入配置界面
,擊安裝建議的插件,耐心等待插件安裝程序(時間較長)
(若程序中有插件安裝失敗,可暫時忽略,進入Jenkins后可根據錯誤log,重新下載安裝)
插件安裝成功后,

按要求將所有專案填寫成功后,點擊保存就可以正常使用Jenkins了
三.接下來創建專案任務,如圖

1.新建任務打開后,由于我們是用Git管理代碼的,所以原始碼管理選擇Git

2.點擊保存,回到任務操作頁面,點擊立即構建,如果Git專案地址和賬號密碼沒問題,構建完成后就可以在
/Users/用戶/.jenkins/workspace看到要打包的專案
四.腳本自動化打包流程
1.專案創建完成后,開始配置構建引數

3.增加構建步驟,選擇執行shell
4.以專案為例,腳本命令如下
echo 'start build xxxProject'
pwd
whoami
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
pod install --verbose --no-repo-update
workspace_path=.
# 工程名
APP_NAME="xxxProject"
APP_target="xxxProject"
# 證書,如果自動選擇證書,設為“iPhone Developer”即可
CODE_SIGN_DEVELOPER="iPhone Developer"
# info.plist路徑
project_infoplist_path="./Info_enterprise.plist"
#取版本號
bundleShortVersion=`xcodebuild -showBuildSettings -target $APP_target | grep MARKETING_VERSION | tr -d 'MARKETING_VERSION ='`
#取build值
bundleVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleVersion" "${project_infoplist_path}")
echo "$bundleShortVersion"
DATE="$(date +%Y%m%d)"
IPANAME="xxxProject_${app_path}_V${bundleShortVersion}_${DATE}"
#要上傳的ipa檔案路徑
IPA_PATH="${WORKSPACE}/package/"
echo ${IPA_PATH}
#echo "${IPA_PATH}">> text.txt
#工程環境路徑
project_path=$(pwd)
echo "-----> 工程路徑:${project_path}"
#工程名稱
project_name=$(ls | grep xcodeproj | awk -F.xcodeproj '{print $1}')
#配置測驗環境的.m檔案
configureFile=$project_path/$project_name/xxx/xxx/xxx.m
echo "$configureFile"
url='^NSString \*const kBaseUrl.*$'
app_url=''
if [ $app_path = "A" ];then
app_url='NSString *const kBaseUrl = @"http:\/\/xxx\/xxx";'
elif [ $app_path = "B" ];then
app_url='NSString *const kBaseUrl = @"http:\/\/xxx\/xxx";'
elif [ $app_path = "C" ];then
app_url='NSString *const kBaseUrl = @"http:\/\/xxx\/xxx";'
";'
else
echo '**************'
fi
sed -i '' "s/$url/$app_url/g" ${configureFile}
sed -i '' "s/$web_url/$app_web_url/g" ${configureFile}
sed -i '' "s/$vip_url/$app_vip_url/g" ${configureFile}
#集成有Cocopods的用法
echo "=================clean================="
xcodebuild -workspace "${APP_NAME}.xcworkspace" -scheme "${APP_target}" -configuration "${build_configuration}" clean
echo "+++++++++++++++++build+++++++++++++++++"
xcodebuild archive -workspace "${APP_NAME}.xcworkspace" -scheme "${APP_target}" -archivePath "${IPA_PATH}${APP_target}.xcarchive" -configuration "${build_configuration}"
xcodebuild -exportArchive -archivePath "${IPA_PATH}${APP_target}.xcarchive" -exportPath "${IPA_PATH}${IPANAME}" -exportOptionsPlist "${IPA_PATH}exportOptions.plist"
5.需注意,使用exportArchive匯出.ipa包時,必須在匯出路徑內配置exportOptions.plist檔案
exportOptions.plist格式如下

6..ipa檔案構建成功,如圖

首先,. ipa包匯出路徑必須放在Jenkins工程專案路徑下,例如
#要上傳的ipa檔案路徑
IPA_PATH="${WORKSPACE}/package/"
然后,增加構建后的操作,選擇歸檔成品
用于存檔的檔案選擇ipa包路徑,如圖

注意
1.專案構建時,報錯pod: command not found
解決方案:前提是已經安裝了cocoapods
第一步:打開Jenkins,然后在Jenkins左側的選單中找到 系統管理--->系統設定--->全域屬性,然后找到Environment variables(中文:環境變數)并且勾選,
第二步:打開你的終端,在終端中輸入echo $PATH,終端會列印出一串字串,幾個路徑,復制該路徑,回傳到Jenkins中
第三步:會讓填寫一個鍵值對,鍵默認填寫PATH,值填寫你從終端復制的那個字串,點擊保存,完成,構建該專案,

2.專案打包成功,但是報錯 Step ‘Archive the artifacts’ failed: No artifacts found that match the file pattern
解決方案:
第一步. ipa包匯出路徑必須放在Jenkins工程專案路徑下,例如
#要上傳的ipa檔案路徑
IPA_PATH="${WORKSPACE}/package/"
第二步.增加構建后的操作,選擇歸檔成品
用于存檔的檔案選擇ipa包路徑,如圖

3.打包報錯 ARCHIVE FAILED
error: No profiles for 'com.ts.hjwealth' were found: Xcode couldn't find any iOS App Development provisioning profiles matching 'com.ts.hjwealth'. Automatic signing is disabled and unable to generate a profile. To enable automatic signing, pass -allowProvisioningUpdates to xcodebuild. (in target 'HJInvestment' from project 'HJInvestment')
解決方案:
一般這個報錯原因是描述檔案和工程bundleID不匹配,檢查配置的描述檔案,重新配置相對應的profile
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/299211.html
標籤:其他
