2018年4月27日
一.jenkins實(shí)現(xiàn)不同環(huán)境用不同證書打包 (這樣uat,pro線上環(huán)境就可以用生產(chǎn)證書(ad-hoc)打包瘾蛋,測(cè)試極光推送等功能)
第一步.工程里面創(chuàng)建開發(fā)和生產(chǎn)的打包文件
生產(chǎn)的(主要是以下幾個(gè)地方的區(qū)別)
image.png
image.png
第二步:需要用生產(chǎn)證書打包的,改成對(duì)應(yīng)的pp文件,建議用release, 這樣debug模式 uat和pro環(huán)境本機(jī)工程上還可以調(diào)試。(生產(chǎn)證書無法聯(lián)機(jī)調(diào)試)
image.png
image.png
第三步:不同環(huán)境指定不同打包腳本 (注意shell if條件判斷一定要有左右空格瓶颠,if [ ${i} == "UAT”] 這樣會(huì)有問題的)涩笤,注意用release打包。
env_array=("SIT" "DEV" "UAT" "PRO”)
configuration="Release"
if [ ${i} == "PRO" ]; then
build_scheme="${project_name}"
build_config="${configuration}"
export_optionsplist_path=${project_path}/QuanQuanNursing/ExportOptionsPro.plist
elif [ ${i} == "UAT" ]; then
export_optionsplist_path=${project_path}/QuanQuanNursing/ExportOptionsPro.plist
else
export_optionsplist_path=${project_path}/QuanQuanNursing/ExportOptions.plist
fi
archivie_cmd=$(xcodebuild -exportArchive -archivePath ${archive_path} -exportPath ${ipa_path} -exportOptionsPlist ${export_optionsplist_path} -allowProvisioningUpdates)
成功:
image.png
2017年6月1日
已修改腳本 支持最新版本xcode8.3
2016年10月23日
一.實(shí)現(xiàn)效果(一級(jí)目錄和二級(jí)目錄)
Paste_Image.png
1.使用
a.直接運(yùn)行腳本就可以
sh archiveAndDownloadShell.sh
b.或者配置在jenkins上
Paste_Image.png
二.腳本實(shí)現(xiàn)原理
1.先在jenkins目錄下創(chuàng)建所有文件(都用相對(duì)路徑定位,便于管理以及實(shí)現(xiàn)頁面鏈接之間的跳轉(zhuǎn))
ps:不會(huì)jenkins安裝參照如下鏈接
用Jenkins集成ios項(xiàng)目舅逸,同時(shí)實(shí)現(xiàn)同一代碼自動(dòng)輸出多個(gè)環(huán)境包
不會(huì)創(chuàng)建可下載的ipa包鏈接的,參照如下鏈接
制作可以下載蘋果app包鏈接
2.文件創(chuàng)建好后皇筛,在將創(chuàng)建好的文件目錄整個(gè)移動(dòng)到服務(wù)器根目錄下面(我這里是nginx)
不會(huì)安裝服務(wù)器nginx的參照如下鏈接
創(chuàng)建可供手機(jī)下載安裝app包全過程(含服務(wù)器配置和支持https證書制作)
3.刪除多余的文件
三.對(duì)應(yīng)如下文件結(jié)構(gòu)
1.服務(wù)器的文件 琉历,最終下載鏈接都在這里, ipa包也在這個(gè)路徑下面
Paste_Image.png
2.jenkins 路徑 拷貝git服務(wù)器上的項(xiàng)目到本機(jī)
Paste_Image.png
3.我是如下方式構(gòu)建schema和configuration
Paste_Image.png
四.shell腳本源碼
#!/bin/bash
#1.定義可配的變量 (按自己對(duì)應(yīng)修改)
jenkinsItemName=ShellTest
envArray=("DEV") #需要打幾種環(huán)境類型的包(測(cè)試水醋,開發(fā)旗笔,生產(chǎn),UAT)"SIT" "DEV" "PRO" "UAT"
#1.1本機(jī)下載服務(wù)器拄踪,對(duì)應(yīng)的是安裝nginx的ip地址(我這里就是本機(jī)ip地址)
server="https://172.16.120.4:443"
bundleID="org.wangyu.app"
#2.固定變量以及不建議修改的變量exportOptions
#2.1固定變量
rootHomeDir="/usr/local/var/www" #一般如果你是用brew命令安裝的nginx蝇恶,這個(gè)值不用改。(nginx根目錄)
#2.2不建議修改的變量
homeDir="317"
iosHomeDir="${homeDir}/IOS" #真正的各個(gè)環(huán)境下載鏈接目錄(對(duì)應(yīng)的是二級(jí)目錄)
dirHomeDir="${homeDir}/dir" #對(duì)應(yīng)第一級(jí)目錄(下載鏈接固定)
backupDir="${dirHomeDir}/backup" #備份第一級(jí)目錄 (一般不會(huì)用到宫蛆,出問題在定位用)
#3.用當(dāng)前時(shí)間變量來明白文件名和文件艘包,這樣就可以保證唯一,且保留歷史數(shù)據(jù)耀盗。eg20161019_160957
curTime="$(date +"%Y%m%d_%H%M%S")"
#3.1.第一級(jí)目錄dir相關(guān)(對(duì)應(yīng)的下載鏈接首頁想虎,內(nèi)容會(huì)累加)
firstDownLoadFilePath="${dirHomeDir}/download.html"
remoteFirstDownloadFilePath="${rootHomeDir}/${firstDownLoadFilePath}"
firstDownLoadBackupFilePath="${backupDir}/download${curTime}.html"
#3.2.第二級(jí)目錄ios相關(guān)(每頁最多只有3個(gè)環(huán)境鏈接,通過累加文件目錄來保留歷史記錄)
secondCurTimeDir="${iosHomeDir}/${curTime}"
secondDownloadFilePath="${secondCurTimeDir}/${curTime}.html"
#4.切換到工程路面 下面很多命令都與這個(gè)相關(guān) ls git
projectPath=${HOME}/.jenkins/workspace/${jenkinsItemName} #jenkins 從git服務(wù)器拷貝下來的工程路徑
cd ${projectPath}
#5工程屬性相關(guān)
projectName=$(ls | grep xcodeproj | awk -F .xcodeproj '{print $1}')
plistFilePath=${projectPath}/QuanQuanNursing/Info.plist #工程名字和文件名字不一致導(dǎo)致
export_optionsplist_path=${project_path}/QuanQuanNursing/ExportOptions.plist
bundleShortVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleShortVersionString" "${plistFilePath}")
bundleVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleVersion" "${plistFilePath}")
#5.1證書(按自己對(duì)應(yīng)修改)
configuration="Debug"
signIdentity="iPhone Developer: wang yu (4FNNN527UN)"
provisioningProfile="317_Development"
#5.2 git數(shù)據(jù)相關(guān)
gitCommitNo=$(git log --pretty=format:"%H " -1)
gitCurBranch=$(git branch | grep '*')
#6.創(chuàng)建目錄(創(chuàng)建最里層的目錄就可以)
#remote ios/20161023_093325
if [ ! -d "${secondCurTimeDir}" ]; then
mkdir -p ${secondCurTimeDir}
fi
# /dir/backup
if [ ! -d "${backupDir}" ]; then
mkdir -p ${backupDir}
fi
#7.download.html(firstDownLoadFile)
htmlHeader="<html>
<head><title>317hu iOS下載目錄</title></head>
<meta charset=\"UTF-8\">
<div style=\"text-align:center;\">
<a style=\"margin-top:10%;display:inline-block;font-size:20px;\"></a>
"
#7.1這里最好用相對(duì)路徑(一級(jí)目錄和二級(jí)目錄的跳轉(zhuǎn)鏈接叛拷,)
htmlContent=" <a style=\"font-size:15px;\" href=\"../../${secondDownloadFilePath}\"><h1>${curTime}-${bundleShortVersion}</h1></a>"
htmlFooter="</div>
</html>"
echo "first=${firstDownLoadFilePath},remoteFist=${remoteFirstDownloadFilePath}"
#7.1復(fù)制遠(yuǎn)程的文件到本地(因?yàn)閮?nèi)容要累加)<建議只復(fù)制需要修改的文件舌厨,不要全部home文件復(fù)制,減輕服務(wù)器壓力>
if [ -f "${remoteFirstDownloadFilePath}" ]; then
cp -r ${remoteFirstDownloadFilePath} ${firstDownLoadFilePath}
fi
#7.2如果沒有直接創(chuàng)建改文件
if [ ! -f "${firstDownLoadFilePath}" ]; then
touch ${firstDownLoadFilePath}
echo "${htmlHeader}" >> ${firstDownLoadFilePath}
echo "${htmlContent}" >> ${firstDownLoadFilePath}
echo "${htmlFooter}" >> ${firstDownLoadFilePath}
else #7.3如果有了就追加忿薇,新插入新的固定5行裙椭,在將就文件的出頭5行的其他部分追加到文件末尾
echo "${htmlHeader}" >> tmp.html
echo "${htmlContent}" >> tmp.html
sed "1,5d" ${firstDownLoadFilePath} >> tmp.html
mv tmp.html ${firstDownLoadFilePath}
fi
cp ${firstDownLoadFilePath} ${firstDownLoadBackupFilePath}
#10.真正下載各個(gè)環(huán)境的鏈接(secondDownloadFile)header部分
htmlHeader="<html>
<head><title>317hu iOS下載頁面</title></head>
<meta charset=\"UTF-8\">
<div style=\"text-align:center;\">
<a style=\"margin-top:10%;display:inline-block;font-size:20px;\"></a>
"
#10.1 輸入頭部分
echo "${htmlHeader}" >> ${secondDownloadFilePath}
#8.循環(huán)打包命令
for i in ${envArray[@]}
do
#8.1清楚bulid文件夾(目前打的是debug包)
if [ ! -d "build" ]; then
xcodebuild clean -configuration ${configuration}
fi
#8.2 構(gòu)建參數(shù)配置
env=${i}
buildScheme="${projectName}-${env}"
bulidConfiguration="${configuration}-${env}"
#生產(chǎn)環(huán)境特殊處理
if [ ${i} == "PRO" ]; then
buildScheme="${projectName}"
bulidConfiguration="${configuration}"
fi
#8.3輸出包名字和路徑設(shè)置
archiveName="${buildScheme}${curTime}.xcarchive"
archiveFilePath="archive/$archiveName"
ipaName="${buildScheme}${curTime}"
ipaFilePath="${iosHomeDir}/${curTime}/${ipaName}" #沒有ipa
#8.4打包命令(法1寫法先構(gòu)建命令在執(zhí)行)
#如果打的project的工程
#bulidCmd='xcodebuild archive -project '${projectName}.xcodeproj' -scheme '${buildScheme}' -configuration '${bulidConfiguration}' -archivePath '${archiveFilePath}
#如何打的是含有workspace(比如用到cocoapods)
bulidCmd='xcodebuild archive -workspace '${projectName}.xcworkspace' -scheme '${buildScheme}' -configuration '${bulidConfiguration}' -archivePath '${archiveFilePath}
#8.4.1執(zhí)行命令
${bulidCmd}
#8.5(法2寫法直接構(gòu)建命令執(zhí)行)
#(用證書打包躏哩,如果代碼里面有通知功能,制作出來的ipa會(huì)無法下載)
#archiveCmd=$(xcodebuild -exportArchive -archivePath ${archiveFilePath} -exportPath ${ipaFilePath} -exportFormat ipa -exportSigningIdentity "${signIdentity}")
#archivie_cmd=$(xcodebuild -exportArchive -archivePath ${archiveFilePath} -exportPath ${ipaFilePath} -exportFormat ipa -exportProvisioningProfile "${provisioningProfile}")
archivie_cmd=$(xcodebuild -exportArchive -archivePath ${archive_path} -exportPath ${ipa_path} -exportOptionsPlist ${export_optionsplist_path} -allowProvisioningUpdates)
#${archiveCmd}
#9創(chuàng)建下載鏈接綁定的plist文件
plistName="${buildScheme}${curTime}.plist"
plistFilePath="${iosHomeDir}/${curTime}/${plistName}"
touch ${plistFilePath}
plistContent="<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">
<plist version=\"1.0\">
<dict>
<key>items</key>
<array>
<dict>
<key>assets</key>
<array>
<dict>
<key>kind</key>
<string>software-package</string>
<key>url</key>
<string>${server}/${ipaFilePath}/${buildScheme}.ipa</string>
</dict>
</array>
<key>metadata</key>
<dict>
<key>bundle-identifier</key>
<string>${bundleID}</string>
<key>bundle-version</key>
<string>${bundleShortVersion}</string>
<key>kind</key>
<string>software</string>
<key>subtitle</key>
<string>${projectName}</string>
<key>title</key>
<string>${buildScheme}</string>
</dict>
</dict>
</array>
</dict>
</plist>"
#9.1 創(chuàng)建文件
echo "${plistContent}" >> ${plistFilePath}
htmlContent=" <a style=\"font-size:20px;\" href=\"itms-services://?action=download-manifest&url=${server}/${plistFilePath}\"><h1>iOS APP(${i}環(huán)境)${time}</h1></a>
"
#10.2 真正下載各個(gè)環(huán)境的鏈接(secondDownloadFile) content部分
echo "${htmlContent}" >> ${secondDownloadFilePath}
done
#10.3.真正下載各個(gè)環(huán)境的鏈接(secondDownloadFile)輸入末尾部分 <第一次安裝需要下載安裝服務(wù)器的crt證書>
htmlFooter=" <span style=\"text-align:center;font-size:30px;\">gitCommitNo:${gitCommitNo}</span>
<span style=\"text-align:center;font-size:30px;\">gitCurBranch:${gitCurBranch}</span>
<span style=\"text-align:center;font-size:30px;\">appVersion:${bundleShortVersion}</span>
<a style=\"margin-top:80%;display:inline-block;font-size:20px;\" href=\"${server}/app_server.crt\"><h1>出現(xiàn)無法連接到揉燃,請(qǐng)下載安裝證書</h1></a>
<span style=\"text-align:center;font-size:30px;\">仍有問題請(qǐng)聯(lián)系iOS開發(fā)組</span>
</div>
</html>"
echo "${htmlFooter}" >> ${secondDownloadFilePath}
#11.打包好后清除多余的文件
#11.1將jenkins下的創(chuàng)建好的項(xiàng)目復(fù)制到ngix下面
cp -r ${homeDir} ${rootHomeDir}
rm -r ${homeDir}
rm -r archive
rm -r build
如果您發(fā)現(xiàn)本文對(duì)你有所幫助扫尺,如果您認(rèn)為其他人也可能受益,請(qǐng)把它分享出去炊汤。