iOS自動化打包 xcodebuild

 使用xcode自帶的xcodebuild 命令經過腳本進行打包html

打包->導出ipa, 兩行關鍵的腳本代碼ios

1.Archiveshell

xcodebuild archive 
           -archivePath <archivePath>
           -project <projectName>
           -workspace <workspaceName>
           -scheme <schemeName>    #從-list命令中獲取
           -configuration <Debug|Release>

2.Exportapi

xcodebuild -exportArchive
           -archivePath <xcarchivepath>
           -exportPath <destinationpath>
           -exportOptionsPlist <plistpath>   #這個plist文件能夠經過打一次ipa包裏面去獲取

3.uploadxcode

$altoolPath 是xcode自帶的Application Loader 的命令行,能夠完成提交到App Store app

if [[ $number == 1 ]]; then

    echo "///--------------------"
    echo "/// 開始發佈到 app store"
    echo "///--------------------"

    altoolPath=/Applications/Xcode.app/Contents/Applications/Application\ Loader.app/Contents/Frameworks/ITunesSoftwareService.framework/Support/altool
    "$altoolPath" --validate-app -f $export_ipa_path/$project_scheme.ipa\
                  -u <你的開發者帳號> -p <你的開發者帳號密碼>\
                  --output-format xml
    "$altoolPath" --upload-app -f $export_ipa_path/$project_scheme.ipa\
                  -u <你的開發者帳號> -p <你的開發者帳號密碼>\
                  --output-format xml
else

    echo "///--------------------"
    echo "/// 開始上傳ipa包到蒲公英"
    echo "///--------------------"
    curl -F "file=@$export_ipa_path/$project_scheme.ipa"\
         -F "uKey=<你的蒲公英uKey>"\
         -F "_api_key=<你的蒲公英ApiKey>" https://qiniu-storage.pgyer.com/apiv1/app/upload
fi

 

參考:curl

xcodebuild命令介紹

iOS自動化打包 ---- 集成shell腳本

Xcodebuild自動打包總結

詳解Shell腳本實現iOS自動化編譯打包提交

iOS-如何實現每次打包編譯號自動增長

 

shell 腳本ui

#!/bin/sh

#  WallPaperScript.sh

#計時
SECONDS=0

#取當前時間字符串添加到文件結尾
now=$(date +"%Y%m%d-%H:%M")

# 獲取 setting.plist 文件路徑
setting_path=./setting.plist

# 項目名稱
project_name=$(/usr/libexec/PlistBuddy -c "print project_name" ${setting_path})

# 項目路徑
project_path=$(/usr/libexec/PlistBuddy -c "print project_path" ${setting_path})

# workspace/xcodeproj 路徑(根據項目是否使用cocoapod,肯定打包的方式)
if [ -d "./${project_name}.xcworkspace" ];then # 項目中存在workspace
workspace_path="${project_path}/${project_name}.xcworkspace"
else # 項目中不存在 workspace
workspace_path="${project_path}/${project_name}.xcodeproj"
fi

# scheme名稱
scheme_name=$(/usr/libexec/PlistBuddy -c "print scheme_name" ${setting_path})

# 項目版本
project_version=$(/usr/libexec/PlistBuddy -c "print project_version" ${setting_path})

# 開發者帳號
dev_account=$(/usr/libexec/PlistBuddy -c "print dev_account" ${setting_path})

# 開發者密碼
dev_password=$(/usr/libexec/PlistBuddy -c "print dev_password" ${setting_path})

# 配置打包樣式:Release/ad-hoc/Debug
configuration=$(/usr/libexec/PlistBuddy -c "print configuration" ${setting_path})

# 發佈地址:蒲公英->PGY,蘋果->APPStore, fir.im->FI
upload_address=$(/usr/libexec/PlistBuddy -c "print upload_address" ${setting_path})

# ipa包名稱:項目名+版本號+打包類型
ipa_name=$(/usr/libexec/PlistBuddy -c "print ipa_name" ${setting_path})

# ipa包路徑
ipa_path2=$(/usr/libexec/PlistBuddy -c "print ipa_path" ${setting_path})/${now}
ipa_path="${ipa_path2}-V${project_version}-${upload_address}"

# 打包配置plist文件路徑 (初始化)
plist_path=$(/usr/libexec/PlistBuddy -c "print plist_path" ${setting_path})

# 編譯build路徑
archive_path="${ipa_path}/${project_name}.xcarchive"

# 上傳到蒲公英設置
user_key=$(/usr/libexec/PlistBuddy -c "print user_key" ${setting_path})
api_key=$(/usr/libexec/PlistBuddy -c "print api_key" ${setting_path})
password=$(/usr/libexec/PlistBuddy -c "print password" ${setting_path})

# 上傳fir.im 設置
fir_token=$(/usr/libexec/PlistBuddy -c "print fir_token" ${setting_path})

#打包方式配置,以及相應的需求配置
if [ ${upload_address} == "APPStore" ];then # 發佈到 AppStore 配置 Release
    configuration="Release"
    plist_path=${project_path}/exportAppstore.plist
elif [ ${upload_address} == "PGY" ] ||[ ${upload_address} == "FI" ];then # 發佈到第三方平臺可 配置 Release、Debug
    if [ ${configuration} == "Release" ];then
     plist_path=${project_path}/exportAdHoc.plist
    else
     plist_path=${project_path}/exportDevelopment.plist
    fi
else # 只打包,不發佈到任何平臺
    if [ ${configuration} == "Release" ];then
       plist_path=${project_path}/exportAppstore.plist
    else
       plist_path=${project_path}/exportDevelopment.plist
    fi
fi

echo '=============正在清理工程============='
xcodebuild clean -configuration ${configuration} -quiet || exit

echo '清理完成-->>>--正在編譯工程:'${configuration}

# 經過workspace方式打包
if [ -d "./${project_name}.xcworkspace" ];then # 項目中存在workspace
    xcodebuild archive -workspace ${workspace_path} -scheme ${scheme_name} \
    -configuration ${configuration} \
    -archivePath ${archive_path} -quiet || exit
else #經過xcodeproj 方式打包
    xcodebuild archive -project ${workspace_path} -scheme ${scheme_name} \
    -configuration ${configuration} \
    -archivePath ${archive_path} -quiet || exit
fi

# 檢查是否構建成功(build)
if [ -d "$archive_path" ] ; then
    echo '=============項目構建成功============='
else
    echo '=============項目構建失敗============='
    exit 1
fi

echo '編譯完成-->>>--開始ipa打包'
xcodebuild -exportArchive -archivePath ${archive_path} \
-configuration ${configuration} \
-exportPath ${ipa_path} \
-exportOptionsPlist ${plist_path} \
-quiet || exit

if [ -e ${ipa_path}/${ipa_name}.ipa ]; then
    echo '=============ipa包導出成功============='
    open $ipa_path
else
    echo '=============ipa包導出失敗============'
fi

echo '打包ipa完成-->>>--開始發佈ipa包'

if [ ${upload_address} == "APPStore" ];then # 發佈到APPStore
    echo '發佈ipa包到 =============APPStore============='
    altoolPath="/Applications/Xcode.app/Contents/Applications/Application Loader.app/Contents/Frameworks/ITunesSoftwareService.framework/Versions/A/Support/altool"
    "$altoolPath" --validate-app -f ${ipa_path}/${ipa_name}.ipa -u ${dev_account} -p ${dev_password} -t ios --output-format xml
    "$altoolPath" --upload-app -f ${ipa_path}/${ipa_name}.ipa -u ${dev_account} -p ${dev_password} -t ios --output-format xml

    if [ $? = 0 ];then
    echo "=============提交AppStore成功 ============="
    else
    echo "=============提交AppStore失敗 ============="
    fi

elif [ ${upload_address} == "PGY" ];then # 發佈到蒲公英平臺
    echo '發佈ipa包到 =============蒲公英平臺============='
    curl -F "file=@${ipa_path}/${ipa_name}.ipa" -F "uKey=${user_key}" -F "_api_key=${api_key}" -F "password=${password}" https://www.pgyer.com/apiv1/app/upload

    if [ $? = 0 ];then
    echo "=============提交蒲公英成功 ============="
    else
    echo "=============提交蒲公英失敗 ============="
    fi

elif [ ${upload_address} == "FI" ];then # 發佈到fir.im 平臺
    echo '發佈ipa包到 =============fir.im平臺============'
    # 須要先在本地安裝 fir 插件,安裝fir插件命令: gem install fir-cli
    fir login -T ${fir_token}              # fir.im token
    fir publish  ${ipa_path}/${ipa_name}.ipa

    if [ $? = 0 ];then
    echo "=============提交fir.im成功 ============="
    else
    echo "=============提交fir.im失敗 ============="
    fi
else # 未配置發佈地址
    echo "=============未發佈 ipa包(打包方式:$configuration) 到任何平臺============="
fi

# 輸出總用時
echo "執行耗時: ${SECONDS}秒"

exit 0
相關文章
相關標籤/搜索