xcode自動打包

最近在研究Xcode自動打包上傳到fir或App Store,這里主要介紹一些步驟。


一、Xcodebuild介紹:(終端進入項目根目錄下)

1.xcodebuild –help/–h         查看具體的選項
2.xcodebuild –version         顯示xcodebuild version
3.xcodebuild –showsdks        顯示當前系統安裝的sdk
4.xcodebuild –list            顯示當前目錄下project Information
5.xcodebuild -workspace       build工程命令,其參數稍后附上
xcodebuild -help(大概展示一下)
     xcodebuild [-project projectname] [-target targetname ...] [-configuration configurationname]
                [-sdk [sdkfullpath | sdkname]] [buildaction ...] [setting=value ...]
                [-userdefault=value ...]
     xcodebuild [-project projectname] -scheme schemename [-destination destinationspecifier]
                [-destination-timeout value] [-configuration configurationname]
                [-sdk [sdkfullpath | sdkname]] [buildaction ...] [setting=value ...]
                [-userdefault=value ...]
     xcodebuild -workspace workspacename -scheme schemename [-destination destinationspecifier]
                [-destination-timeout value] [-configuration configurationname]
                [-sdk [sdkfullpath | sdkname]] [buildaction ...] [setting=value ...]
                [-userdefault=value ...]
     xcodebuild -version [-sdk [sdkfullpath | sdkname]] [infoitem]
     xcodebuild -showsdks
     xcodebuild -list [-project projectname | -workspace workspacename]
     xcodebuild -exportArchive -exportFormat format -archivePath xcarchivepath -exportPath destinationpath
                [-exportProvisioningProfile profilename] [-exportSigningIdentity identityname]
                [-exportInstallerIdentity identityname]
xcodebuild –version
Xcode 8.3.2
Build version 8E2002
xcodebuild –list
Information about project "XXX":
    Targets:
        XXX
        XXX Tests
        XXX UITests

    Build Configurations:
        Debug
        Release

    If no build configuration is specified and -scheme is not passed then "Release" is used.

    Schemes:
        XXX
xcodebuild -workspace
從help中可得信息
xcodebuild -workspace workspacename -scheme schemename [-destination destinationspecifier]
                [-destination-timeout value] [-configuration configurationname]
                [-sdk [sdkfullpath | sdkname]] [buildaction ...] [setting=value ...]
                [-userdefault=value ...]
其中scheme,configuration即list里面參數,workspace必須和scheme一起使用,構建該workspace下的一個scheme。若當根目錄下有多個Project的時候,必須使用“-project”指定project。target:構建某個Target。

1.新建文件

首先在工程目錄下建一個XXX.sh文件(自動打包上傳腳本),兩個plist文件:XXXAdHoc.plist,XXXAppStore.plist(導出IPA使用)

2.plist文件(Plist文件用于導出不同ipa包時使用。)
XXXAdHoc.plist

method=ad-hoc,compileBitcode=NO

XXXAppStore.plist

method=app-store,uploadBitcode=YES,uploadSymbols=YES

3.XXX.sh文件

#!/bin/sh(建立.sh時自動生成)
1).基本配置信息
# 腳本配置
# 項目
target_name="需要打包的target名字"
scheme_name="$target_name"
configurationType="Release" #所選要與證書匹配

# 證書(與環境相匹配的證書)、描述文件(DVTPlugInCompatibilityUUID,可以通過$ xcodebuild -list查看,也可以前往/Applications/Xcode.app/Contents,查看info.plist文件)
CODE_SIGN_IDENTITY="iPhone Distribution: XX  XX(XXXXXXXXXX)"
PROVISIONING_PROFILE="XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"

# 目錄(生成的xcarchive會在archive文件下,ipa會在archive/${target_name}.ipa文件下)
archive_path="archive/${target_name}.xcarchive"
ipa_path="archive/${target_name}.ipa"

# fir配置
commit_msg="$1"
fir_Token="XXXXXXXXXXXXXXXXXXXXX"
2).打包前清理一些舊文件
#  刪除舊文件
rm -rf "archive/${target_name}.xcarchive"
rm -rf "archive/${target_name}.ipa"

# 清理舊項目
xcodebuild clean -configuration "$configurationType" -alltargets

3).歸檔
#  歸檔(其他參數不指定的話,默認用的是.xcworkspace或.xcodeproj文件里的配置)
archiveRun () {
    #是否是工作空間
    echo "是否是工作空間:(yes or no)"
    read isWorkspaceParameter
    sleep 0.5
    isWorkspace="$isWorkspaceParameter"

    if [ "$isWorkspace"  == "yes" ]
    then
        workspace_name="${target_name}.xcworkspace"

        xcodebuild archive -workspace "$workspace_name" -scheme "$scheme_name" -destination generic/platform=iOS -configuration "$configurationType" -archivePath "$archive_path" CODE_SIGN_IDENTITY="$CODE_SIGN_IDENTITY" PROVISIONING_PROFILE="$PROVISIONING_PROFILE"
    elif [ "$isWorkspace"  == "no" ]
    then
        project_name = "${target_name}.xcodeproj"

        xcodebuild archive -xcodeproj "$project_name" -scheme "$scheme_name" -archivePath "$archive_path" -configuration "$configurationType" CODE_SIGN_IDENTITY="$CODE_SIGN_IDENTITY" PROVISIONING_PROFILE="$PROVISIONING_PROFILE"
    else
        echo "參數無效, 請重新輸入"
        archiveRun
        #exit 1
    fi
}

archiveRun

# xcarchive 實際是一個文件夾不是一個文件所以使用 -d 判斷
if [ -d "$archive_path" ]
then
echo "構建成功......"
else
echo "構建失敗......"
exit 1
fi
4).導出IPA
exportArchiveRun () {
    echo "選擇打包方式(1.ad-hoc or 2.AppStore):"
    read methodParameter
    sleep 0.5
    method="$methodParameter"

    if [ -n "$method" ]
    then
        if [ "$method" == "1" ]
        then

            plist_path="XXXAdHoc.plist"
        elif  [ "$method" == "2" ]
        then

            plist_path="XXXAppStore.plist"
        else
            echo "參數無效, 請重新輸入"
            exportArchiveRun
            #exit 1
        fi
    else

        plist_path="XXXAdHoc.plist"
    fi

    xcodebuild -exportArchive -archivePath "$archive_path" -exportPath "$ipa_path" -exportOptionsPlist "$plist_path"
}

exportArchiveRun

if [ -f "$ipa_path/${target_name}.ipa" ]
then
    echo "導出ipa成功......"
else
    echo "導出ipa失敗......"
    exit 1
fi
5).上傳
publishRun () {
    echo "是否上傳(yes or no):"
    read isPublishParameter
    sleep 0.5
    isPublish="$isPublishParameter"

    if [ "$isPublish"  == "yes" ]
    then
        if [ "$method" == "2" ]
        then
            #上傳App Store
            echo "請輸入開發者賬號:"
            read usernameParameter
            sleep 0.5
            username="$usernameParameter"

            echo "請輸入開發者賬號密碼:"
            read passwordParameter
            sleep 0.5
            password="$passwordParameter"

            altoolPath="/Applications/Xcode.app/Contents/Applications/Application Loader.app/Contents/Frameworks/ITunesSoftwareService.framework/Versions/A/Support/altool"

            "${altoolPath}" --validate-app -f "$ipa_path/${target_name}.ipa" -u "$username" -p "$password" --output-format xml
            "${altoolPath}" --upload-app -f "$ipa_path/${target_name}.ipa" -u "$username" -p "$password" --output-format xml
        else

            publishFirRun () {
                echo "是否上傳fir.im(yes or no):"
                read isPublishFirParameter
                sleep 0.5
                isPublishFir="$isPublishFirParameter"

                if [ "$isPublishFir"  == "yes" ]
                then
                    fir publish "$ipa_path/${target_name}.ipa" -T "$fir_Token" -c "${commit_msg}"
                elif [ "$isPublishFir"  == "no" ]
                then
                    exit 1
                else
                    echo "參數輸入無效,請重新輸入"
                    publishFirRun
#                    exit 1
                fi
            }
            publishFirRun
        fi
    elif [ "$isPublish"  == "no" ]
    then
        exit 1
    else
        echo "參數輸入無效,請重新輸入"
        publishRun
#        exit 1
fi
}

publishRun

if [ $? = 0 ]
then
    echo "~~~~~~~~~~~~~~~~上傳成功~~~~~~~~~~~~~~~~~~~"
else
    echo "~~~~~~~~~~~~~~~~上傳失敗~~~~~~~~~~~~~~~~~~~"
fi

參考
https://developer.apple.com/legacy/library/documentation/Darwin/Reference/ManPages/man1/xcodebuild.1.html
http://www.lxweimin.com/p/bd4c22952e01
http://www.lxweimin.com/p/e55f76385ed9
fir-cli安裝及問題:https://github.com/FIRHQ/fir-cli/blob/master/doc/install.md

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容