背景介紹
入職到公司一年多來肘交,公司的業(yè)務(wù)逐漸步入正軌笆载,定制的產(chǎn)品也是越來越多,從剛開始的基礎(chǔ)版本,一直到最近的各種定制包凉驻,其中一個(gè)項(xiàng)目就有6個(gè)定制包腻要,而且定制包還在不斷的增加中,從前都是測試手動一個(gè)一個(gè)打包涝登,打包用的mini本身配置低雄家,打包很慢,打個(gè)包運(yùn)氣好等個(gè)十幾分鐘胀滚,人要一直守在電腦前趟济,上次上線,測試需要將所有版本先打包出來測試一遍咽笼,然后再上傳到appstore顷编,就光打包就占用了一上午的時(shí)間,還導(dǎo)致測試一個(gè)人力的浪費(fèi)剑刑,要是測試出現(xiàn)bug勾效,又要重復(fù)上述過>程,而且聯(lián)想到以后其他項(xiàng)目也有這種情況叛甫,所以自動化研發(fā)已是必不可少。
研發(fā)過程
網(wǎng)上自動化腳本很多杨伙,之前使用過一個(gè)打包腳本其监,但是這個(gè)打包腳本只能打一個(gè)包,可以上傳fir測試限匣,但是上傳不了appstore抖苦,為了解決上面的問題,花了大概一下午的時(shí)間米死,查找資料學(xué)習(xí)shell腳本語法锌历,在之前腳本的基礎(chǔ)上税灌,添加了for循環(huán)實(shí)現(xiàn)批量打包柴钻,同時(shí)為了方便測試進(jìn)行個(gè)性化打包,又對腳本進(jìn)行了優(yōu)化图云,實(shí)現(xiàn)了執(zhí)行腳本時(shí) 可以選擇
- 批量打包上傳appstore
- 批量打包上傳fir 測試
- 批量打包放到桌面指定文件夾
- 單獨(dú)打某個(gè)定制版的包
廢話不多說物喷,直接上腳本卤材,要修改的變量我也在腳本開頭單獨(dú)標(biāo)出。其他看備注即可峦失。
#! bin/bash
#Author:Bruce http://www.heyuan110.com
#Update Date:2015.06.23
#Use:命令行進(jìn)入目錄直接執(zhí)行sh Build+DeployToFir.sh即可完成打包發(fā)布到fir.im
echo "~~~~~~~~~~~~~~~~開始執(zhí)行腳本~~~~~~~~~~~~~~~~"
#-------------------------腳本配置信息------------------------------------
beginTime=`date +%s`
DATE=`date '+%Y-%m-%d-%T'`
#需要編譯的 targetName數(shù)組
array_tagName=(
"SoocQiPei"
"東方瑞藝"
"中程在線"
"智融在線"
"博智教育"
"四海華辰"
"青龍農(nóng)信"
)
#需要編譯的 證書數(shù)組
array_profileNames=(
"企培_發(fā)布"
"東方瑞藝_發(fā)布"
"中程在線_發(fā)布"
"e融_發(fā)布"
"博智教育_發(fā)布"
"四海華辰_發(fā)布"
"青龍農(nóng)信_發(fā)布"
)
#生成的ipa包名(上傳appstore的ipa包名不允許有中文)
array_ipaName=(
"SoocQiPei"
"dongfangruiyi"
"zhongchengzaixian"
"zhirongzaixian"
"bozhijianyu"
"sihaihuachen"
"qinglongnongxin"
)
#工程名稱
PROJECT_NAME="SoocQiPei"
#編譯模式 工程默認(rèn)有 Debug Release
CONFIGURATION_TARGET=Release
#證書名
CODE_SIGN_IDENTITY="iPhone Distribution: xxxxxxx Stock Co., Ltd. (xxxxxxx)"
#蘋果賬號
AppleID="xxxxxxxxxxxxx"
AppleIDPWD="xxxxxxxxxxx"
#fir賬號(自己去fir查看賬號的token)
counttoken="xxxxxxxxxxxxxxx"
#fir賬號下的appid(每個(gè)app有個(gè)id)
array_apptokens=(
"xxxxxxxxxxxxxxxxxxxxxxxx" #企培
"xxxxxxxxxxxxxxxxxxxxxxxx" #東方瑞藝
"xxxxxxxxxxxxxxxxxxxxxxxx" #中程在線
"xxxxxxxxxxxxxxxxxxxxxxxx" #e融
"xxxxxxxxxxxxxxxxxxxxxxxx" #博智教育
"xxxxxxxxxxxxxxxxxxxxxxxx" #四海華辰
"xxxxxxxxxxxxxxxxxxxxxxxx" #青龍農(nóng)信
)
#導(dǎo)出ipa 所需plist
ADHOCExportOptionsPlist=./ADHOCExportOptionsPlist.plist
AppStoreExportOptionsPlist=./AppStoreExportOptionsPlist.plist
#-------------------------------------------------------------------------------------
echo "請選擇上傳方式:0 批量打包不上傳 1 批量打包上傳fir 2 批量打包上傳appstore 3 單獨(dú)打包某個(gè)版本"
read answer
array_way=(批量打包不上傳 批量打包上傳fir 批量打包上傳appstore 單獨(dú)打包某個(gè)版本)
echo "你的選擇是:${array_way[$answer]}"
#定義打包函數(shù) 傳入兩個(gè)參數(shù) $1 tagName $2證明文件
function archive(){
echo "tag名稱----$1 證明文件是------$2"
#描述文件
PROVISIONING_PROFILE_NAME=$2
#需要編譯的 targetName
TARGET_NAME=$1
ipa_name=$3
#編譯路徑
BUILDPATH=~/Desktop/所有的ipa包
#archivePath
ARCHIVEPATH=${BUILDPATH}/${PROJECT_NAME}.xcarchive
#輸出的ipa目錄
IPAPATH=~/Desktop/所有的ipa包
echo "~~~~~~~~~~~~~~~~開始編譯~~~~~~~~~~~~~~~~~~~"
echo "~~~~~~~~~~~~~~~~開始清理~~~~~~~~~~~~~~~~~~~"
# 清理 避免出現(xiàn)一些莫名的錯(cuò)誤
xcodebuild clean -workspace ${PROJECT_NAME}.xcworkspace \
-configuration \
${CONFIGURATION} -alltargets
echo "~~~~~~~~~~~~~~~~開始構(gòu)建~~~~~~~~~~~~~~~~~~~"
#開始構(gòu)建
xcodebuild archive -workspace ${PROJECT_NAME}.xcworkspace \
-scheme ${TARGET_NAME} \
-archivePath ${ARCHIVEPATH} \
-configuration ${CONFIGURATION_TARGET} \
CODE_SIGN_IDENTITY="${CODE_SIGN_IDENTITY}" \
PROVISIONING_PROFILE="${PROVISIONING_PROFILE_NAME}"
echo "~~~~~~~~~~~~~~~~檢查是否構(gòu)建成功~~~~~~~~~~~~~~~~~~~"
# xcarchive 實(shí)際是一個(gè)文件夾不是一個(gè)文件所以使用 -d 判斷
if [ -d "$ARCHIVEPATH" ]
then
echo "構(gòu)建成功......"
else
echo "構(gòu)建失敗......"
rm -rf $BUILDPATH
exit 1
fi
endTime=`date +%s`
ArchiveTime="構(gòu)建時(shí)間$[ endTime - beginTime ]秒"
echo "~~~~~~~~~~~~~~~~導(dǎo)出ipa~~~~~~~~~~~~~~~~~~~"
beginTime=`date +%s`
xcodebuild -exportArchive \
-archivePath ${ARCHIVEPATH} \
-exportOptionsPlist ${ExportOptionsPlist} \
-exportPath ${IPAPATH}
echo "~~~~~~~~~~~~~~~~檢查是否成功導(dǎo)出 ipa~~~~~~~~~~~~~~~~~~~"
IPAPATH=${IPAPATH}/${TARGET_NAME}.ipa
if [ -f "$IPAPATH" ]
then
echo "導(dǎo)出ipa成功......"
#修改ipa名字
mv ~/Desktop/所有的ipa包/${TARGET_NAME}.ipa ~/Desktop/所有的 ipa包/${ipa_name}.ipa
IPAPATH=~/Desktop/所有的ipa包/${ipa_name}.ipa
else
echo "導(dǎo)出ipa失敗......"
# 結(jié)束時(shí)間
endTime=`date +%s`
echo "$ArchiveTime"
echo "導(dǎo)出ipa時(shí)間$[ endTime - beginTime ]秒"
exit 1
fi
endTime=`date +%s`
ExportTime="導(dǎo)出ipa時(shí)間$[ endTime - beginTime ]秒"
}
#上傳fir的 函數(shù) 傳兩個(gè)參數(shù) $1賬號token $2 appid
function exportFir(){
export LANG=en_US
export LC_ALL=en_US;
echo "正在上傳到fir.im...."
fir p $IPAPATH
changelog=`cat $projectDir/README`
curl -X PUT --data "changelog=$changelog" http://fir.im/api/v2/app/$2?token=$1
echo "\n打包上傳更新成功扇丛!"
rm -rf $buildAppToDir
rm -rf $projectDir/tmp
}
#上傳appstore -------------------------------------------
function exportAppStore(){
#mini的路徑
altoolPath="/Applications/Xcode.app/Contents/Applications/Application\ Loader.app/Contents/Frameworks/ITunesSoftwareService.framework/Versions/A/Support/altool"
#我的電腦路徑
#altoolPath="/Users/duanshuai/Desktop/Xcode.app/Contents/Applications/Application_Loader.app/Contents/Frameworks/ITunesSoftwareService.framework/Versions/A/Support/altool"
${altoolPath} --validate-app \
-f ${IPAPATH} \
-u ${AppleID} \
-p ${AppleIDPWD} \
-t ios --output-format xml
if [ $? = 0 ]
then
echo "~~~~~~~~~~~~~~~~驗(yàn)證ipa成功~~~~~~~~~~~~~~~~~~~"
"${altoolPath}" --upload-app \
-f ${IPAPATH} \
-u ${AppleID} \
-p ${AppleIDPWD} \
-t ios --output-format xml
if [ $? = 0 ]
then
echo "~~~~~~~~~~~~~~~~提交AppStore成功~~~~~~~~~~~~~~~~~~~"
else
echo "~~~~~~~~~~~~~~~~提交AppStore失敗~~~~~~~~~~~~~~~~~~~"
fi
else
echo "~~~~~~~~~~~~~~~~驗(yàn)證ipa失敗~~~~~~~~~~~~~~~~~~~"
fi
}
#開始運(yùn)行腳本
if [ $answer -eq 0 ]
then
# 批量打包 放到桌面
echo "~~~~~~~~~~~~~~~~批量打包 放到桌面~~~~~~~~~~~~~~~~"
ExportOptionsPlist=${ADHOCExportOptionsPlist}
value=0
for tagName in ${array_tagName[@]}
do
archive ${tagName} ${array_profileNames[$value]} $ {array_ipaName[$value]}
let value++
done
elif [ $answer -eq 1 ]
then
#批量打包上傳fir
echo "~~~~~~~~~~~~~~~~批量打包上傳fir~~~~~~~~~~~~~~~~"
ExportOptionsPlist=${ADHOCExportOptionsPlist}
value=0
for tagName in ${array_tagName[@]}
do
#打包 導(dǎo)出ipa
archive ${tagName} ${array_profileNames[$value]} ${array_ipaName[$value]}
#上傳fir
exportFir $counttoken ${array_apptokens[$value]}
let value++
done
elif [ $answer -eq 2 ]
then
#批量打包上傳appstore
ExportOptionsPlist=${AppStoreExportOptionsPlist}
for tagName in ${array_tagName[@]}
do
#打包 導(dǎo)出ipa
archive ${tagName} ${array_profileNames[$value]} $ {array_ipaName[$value]}
#上傳appstore
exportAppStore
let value++
done
echo "~~~~~~~~~~~~~~~~批量打包上傳appstore~~~~~~~~~~~~~~~~"
elif [ $answer -eq 3 ]
then
#選擇要打包的tag
ExportOptionsPlist=${ADHOCExportOptionsPlist}
echo "選擇要打包的tag:0 SoocQiPei 1 東方瑞藝 2 中程在線 3 智融在線 4 博智教育 5 四海華辰 6 青龍農(nóng)信/n"
read num
echo "是否上傳ifr:1 上傳 2 不上傳/n"
read isfir
archive ${array_tagName[$num]} ${array_profileNames[$num]} ${array_ipaName[$num]}
if [ $isfir -eq 1 ]
then
#上傳fir
exportFir $counttoken ${array_apptokens[$num]}
fi
else
echo "~~~~~~~~~~~~~~~~你選錯(cuò)了~~~~~~~~~~~~~~~~"
fi
使用說明
1 將腳本復(fù)制到工程目錄
2 將ADHOCExportOptionsPlist.plist和 AppStoreExportOptionsPlist.plist文件也添加到工程目錄,兩個(gè)文件內(nèi)容最后附加尉辑。
3 修改腳本里的配置信息
4 最后cd到工程目錄帆精,執(zhí)行 sh 腳本名.sh
附錄
adhoc.plist
<?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>method</key>
<string>ad-hoc</string>
<key>compileBitcode</key>
<false/>
</dict>
</plist>
appstore.plist
<?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>method</key>
<string>app-store</string>
<key>compileBitcode</key>
<false/>
<key>uploadSymbols</key>
<true/>
</dict>
</plist>