iOS 持續整合

weixin_34321977發表於2018-01-29

AutoPackageScript.sh

# !/bin/bash

# 使用方法:
# step1 : 將PPAutoPackageScript整個資料夾拖入到專案主目錄,專案主目錄,專案主目錄~~~(重要的事情說3遍!???)
# step2 : 開啟PPAutoPackageScript.sh檔案,修改 "專案自定義部分" 配置好專案引數
# step3 : 開啟終端, cd到PPAutoPackageScript資料夾 (ps:在終端中先輸入cd ,直接拖入PPAutoPackageScript資料夾,回車)
# step4 : 輸入 sh PPAutoPackageScript.sh 命令,回車,開始執行此打包指令碼

# ===============================專案自定義部分(自定義好下列引數後再執行該指令碼)============================= #
# 計時
SECONDS=0
# 是否編譯工作空間 (例:若是用Cocopods管理的.xcworkspace專案,賦值true;用Xcode預設建立的.xcodeproj,賦值false)
is_workspace="true"
# 指定專案的scheme名稱
# (注意: 因為shell定義變數時,=號兩邊不能留空格,若scheme_name與info_plist_name有空格,指令碼執行會失敗,暫時還沒有解決方法,知道的還請指教!)
scheme_name="中金圈子"
# 工程中Target對應的配置plist檔名稱, Xcode預設的配置檔案為Info.plist
info_plist_name="Info"
# 指定要打包編譯的方式 : Release,Debug...
build_configuration="Release"


# ===============================自動打包部分(無特殊情況不用修改)============================= #

# 匯出ipa所需要的plist檔案路徑 (預設為AdHocExportOptionsPlist.plist)
ExportOptionsPlistPath="./PPAutoPackageScript/AdHocExportOptionsPlist.plist"
# 返回上一級目錄,進入專案工程目錄
cd ..
# 獲取專案名稱
project_name=`find . -name *.xcodeproj | awk -F "[/.]" '{print $(NF-1)}'`
# 獲取版本號,內部版本號,bundleID
info_plist_path="$project_name/$info_plist_name.plist"
bundle_version=`/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" $info_plist_path`
bundle_build_version=`/usr/libexec/PlistBuddy -c "Print CFBundleIdentifier" $info_plist_path`
bundle_identifier=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" $info_plist_path`

# 刪除舊.xcarchive檔案
rm -rf ~/Desktop/wdw_cmstop/移動採編/ipa/$scheme_name-IPA/$scheme_name.xcarchive
# 指定輸出ipa路徑
export_path=~/Desktop/wdw_cmstop/移動採編/ipa/$scheme_name-IPA
# Desktop/wdw_cmstop/移動採編/ipa
# 指定輸出歸檔檔案地址
export_archive_path="$export_path/$scheme_name.xcarchive"
# 指定輸出ipa地址
export_ipa_path="$export_path"
# 指定輸出ipa名稱 : scheme_name + bundle_version
ipa_name="$scheme_name-v$bundle_version"

# AdHoc,AppStore,Enterprise三種打包方式的區別: http://blog.csdn.net/lwjok2007/article/details/46379945
echo "\033[36;1m請選擇打包方式(輸入序號,按回車即可) \033[0m"
echo "\033[33;1m1. AdHoc       \033[0m"
echo "\033[33;1m2. AppStore    \033[0m"
echo "\033[33;1m3. Enterprise  \033[0m"
echo "\033[33;1m4. Development \033[0m"
# 讀取使用者輸入並存到變數裡
read parameter
sleep 0.5
method="$parameter"

# 判讀使用者是否有輸入
if [ -n "$method" ]
then
    if [ "$method" = "1" ] ; then
    ExportOptionsPlistPath="./PPAutoPackageScript/AdHocExportOptionsPlist.plist"
    elif [ "$method" = "2" ] ; then
    ExportOptionsPlistPath="./PPAutoPackageScript/AppStoreExportOptionsPlist.plist"
    elif [ "$method" = "3" ] ; then
    ExportOptionsPlistPath="./PPAutoPackageScript/EnterpriseExportOptionsPlist.plist"
    elif [ "$method" = "4" ] ; then
    ExportOptionsPlistPath="./PPAutoPackageScript/DevelopmentExportOptionsPlist.plist"
    else
    echo "輸入的引數無效!!!"
    exit 1
    fi
fi

echo "\033[32m*************************  開始構建專案  *************************  \033[0m"
# 指定輸出檔案目錄不存在則建立
if [ -d "$export_path" ] ; then
echo $export_path
else
mkdir -pv $export_path
fi

# 判斷編譯的專案型別是workspace還是project
if $is_workspace ; then
# 編譯前清理工程
xcodebuild clean -workspace ${project_name}.xcworkspace \
                 -scheme ${scheme_name} \
                 -configuration ${build_configuration}

xcodebuild archive -workspace ${project_name}.xcworkspace \
                   -scheme ${scheme_name} \
                   -configuration ${build_configuration} \
                   -archivePath ${export_archive_path}
else
# 編譯前清理工程
xcodebuild clean -project ${project_name}.xcodeproj \
                 -scheme ${scheme_name} \
                 -configuration ${build_configuration}

xcodebuild archive -project ${project_name}.xcodeproj \
                   -scheme ${scheme_name} \
                   -configuration ${build_configuration} \
                   -archivePath ${export_archive_path}
fi

#  檢查是否構建成功
#  xcarchive 實際是一個資料夾不是一個檔案所以使用 -d 判斷
if [ -d "$export_archive_path" ] ; then
echo "\033[32;1m專案構建成功 ? ? ?  \033[0m"
else
echo "\033[31;1m專案構建失敗 ? ? ?  \033[0m"
exit 1
fi

echo "\033[32m*************************  開始匯出ipa檔案  *************************  \033[0m"
xcodebuild  -exportArchive \
            -archivePath ${export_archive_path} \
            -exportPath ${export_ipa_path} \
            -exportOptionsPlist ${ExportOptionsPlistPath}
# 修改ipa檔名稱
mv $export_ipa_path/$scheme_name.ipa $export_ipa_path/$ipa_name.ipa

# 檢查檔案是否存在
if [ -f "$export_ipa_path/$ipa_name.ipa" ] ; then
echo "\033[32;1m匯出 ${ipa_name}.ipa 包成功 ?  ?  ?   \033[0m"
open $export_path
else
echo "\033[31;1m匯出 ${ipa_name}.ipa 包失敗 ? ? ?     \033[0m"
# 相關的解決方法
echo "\033[34mps:以下型別的錯誤可以參考對應的連結\033[0m"
echo "\033[34m  1.\"error: exportArchive: No applicable devices found.\" --> 可能是ruby版本過低導致,升級最新版ruby再試,升級方法自行百度/谷歌,GitHub issue: https://github.com/jkpang/PPAutoPackageScript/issues/1#issuecomment-297589697"
echo "\033[34m  2.\"No valid iOS Distribution signing identities belonging to team 6F4Q87T7VD were found.\" --> http://fight4j.github.io/2016/11/21/xcodebuild/ \033[0m"
exit 1
fi
# 上傳到蒲公英

curl -F "file=@$export_ipa_path/${ipa_name}.ipa" -F "uKey=xxxxxxxx" -F "_api_key=xxxxxxxxxxxxxxxxxxxx" https://qiniu-storage.pgyer.com/apiv1/app/upload
# 輸出打包總用時
echo "\033[36;1m使用PPAutoPackageScript打包總用時: ${SECONDS}s \033[0m"




AdHocExportOptionsPlist.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>
</dict>
</plist>

DevelopmentExportOptionsPlist.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>development</string>
</dict>
</plist>


相關文章