iOS自动打包Shell脚本

461 阅读3分钟

记录一下

一、打包脚本shell。

执行脚本时通过输入1或2来确定打包模式。如果使用自动构建系统,比如在gitlab上进行自动构建,可以将该脚本拆分成两个,一种模式对应一个脚本。视自身具体情况而定。

if [ ! -d ./IPADir ];
then
mkdir -p IPADir;
fi

#工程绝对路径
project_path=$(cd `dirname $0`; pwd)

#工程名
project_name=XXX

#scheme名
scheme_name=XXX

#打包模式 Debug/Release
development_mode=Debug

#build文件夹路径
build_path=${project_path}/build

#plist文件所在路径
exportOptionsPlistPath=${project_path}/exportTest.plist

#导出.ipa文件所在路径
exportIpaPath=${project_path}/IPADir/${development_mode}

echo "Place enter the number you want to export ? [ 1:app-store 2:ad-hoc] "

read number
while([[ $number != 1 ]] && [[ $number != 2 ]])
do
echo "Error! Should enter 1 or 2"
echo "Place enter the number you want to export ? [ 1:app-store 2:ad-hoc] "
read number
done

if [ $number == 1 ];then
development_mode=Release
exportOptionsPlistPath=${project_path}/exportAppstore.plist
else
development_mode=Debug
exportOptionsPlistPath=${project_path}/exportTest.plist
fi

echo '/// 正在清理工程'

xcodebuild \
clean -workspace ${project_path}/${project_name}.xcworkspace -scheme ${scheme_name} -configuation ${development_mode} -quiet  || exit

echo '/// 清理完成'

echo '/// 正在编译工程:'${development_mode}
xcodebuild \
archive -workspace ${project_path}/${project_name}.xcworkspace \
-scheme ${scheme_name} \
-configuration ${development_mode} \
-archivePath ${build_path}/${project_name}.xcarchive  -quiet  || exit

echo '/// 编译完成'

echo '/// 开始ipa打包'
xcodebuild  -exportArchive \
-archivePath ${build_path}/${project_name}.xcarchive \
-configuration ${development_mode} \
-exportPath ${exportIpaPath} \
-exportOptionsPlist ${exportOptionsPlistPath} \
-allowProvisioningUpdates YES \
-quiet || exit

if [ -e $exportIpaPath/$scheme_name.ipa ]; then
echo '/// ipa包已导出'
open $exportIpaPath
else
echo '/// ipa包导出失败 '
fi

echo '/// 打包ipa完成  '
echo '/// 开始发布ipa包 '

if [ $number == 1 ];then
#验证并上传到App Store。 使用apiKey和apiIssuer进行身份验证。
altoolPath="/Applications/Xcode.app/Contents/Developer/usr/bin/altool"
"$altoolPath" --validate-app -f ${exportIpaPath}/${scheme_name}.ipa -apiKey XXX -apiIssuer XXX -t ios --output-format xml
"$altoolPath" --upload-package ${exportIpaPath}/${scheme_name}.ipa -apiKey  XXX -apiIssuer XXX -t ios --output-format xml
else
#上传到Fir
#xxx为自己的Fir平台的token
fir login -T XXXX
fir publish $exportIpaPath/$scheme_name.ipa
fi

exit 0

二、脚本说明

1.altool位于:/Applications/Xcode.app/Contents/Developer/usr/bin/altool。 2.身份验证时也可以通过-u username和-p password来进行验证。username是App Store Connect用户名,password是对应密码,该方式明文显示用户名和密码,有泄露账户的风险。所以使用apiKey/apiIssuer秘钥方式来验证。 3.上传到App Store时,-upload-app 命令已被弃用,请改用 --upload-package 命令。

三、如何生成apiKey/apiIssuer?

1.登录appstoreconnect, 在用户和访问--秘钥--秘钥类型(App Store Connect API),新增一个API秘钥,

image.png 名称可以写个易理解用途的名称,访问选择管理者或开发者都行,有上传开发包权限的职能就可以。

2.生成秘钥之后,秘钥ID就是apiKey,上边的IssuerID就是apiIssuer。 image.png

3.下载秘钥。秘钥只能下载一次,下载完页面就不在提供下载按钮了,注意妥善保存。 在电脑当前用户对应的目录下(mac左上角点击‘前往’--‘个人’对应的目录,里边有公共,图片,文稿,音乐那个目录下),新建一个名字为.private_keys的文件夹,将秘钥放进去即可。

4.保持API密钥的安全性和私密性。永远不要共享密钥、在代码存储库中存储密钥或在客户端代码中包含密钥。如果密钥丢失或泄露,立即撤销它。 image.png

四、exportTest.plist 和 exportAppstore.plist配置文件

image.png

image.png

1.上图中的method参数对应的就是Xcode打包的类型。 method : String Describes how Xcode should export the archive. Available options: app-store, validation, ad-hoc, package, enterprise, development, developer-id, and mac-application. The list of options varies based on the type of archive. Defaults to development.

2.destination参数对应的是导出包,还是直接上传包。 destination : String Determines whether the app is exported locally or uploaded to Apple. Options are export or upload. The available options vary based on the selected distribution method. Defaults to export. 【更多xcodebuild参数的意思,可在终端通过xcodebuild -help查看。】

3.我列出来的exportTest.plist 和 exportAppstore.plist这两个配置文件里的字段可能不全。具体值可以参考自己项目手动打包生成的plist文件。xcode--window--Organizer--Distribute App到本地,里边的ExportOptions.plist就是我们要用的plist。ad-hoc和appstore各对应一个plist。 (建议使用自己项目手动打包生成的plist,毕竟项目配置不一样生成的plist字段就不一样了)

最后附上相关官方地址:

altool使用指南:help.apple.com/asc/appsalt…

App Store Connect API:developer.apple.com/documentati…