Shell脚本自动会打包iPa并提交iPa(xcodebuild -exportArchive)

具体内容如下

build.sh:

#! bin/bash

###############使用方法
#1、build.sh和build.plist放入项目工程目录
#2、命令行进入目录直接执行:sh build.sh

export LC_ALL=zh_CN.GB2312;
export LANG=zh_CN.GB2312

###############设置
gitPath="https://xxx/xxx.git"
buildConfig="DevQA" #编译的方式, 如Release,Debug等
isWorkSpace=true  #判断是用的workspace还是直接project,workspace设置为true,否则设置为false
projectName=`find . -name *.xcodeproj | awk -F "[/.]" '{print $(NF-1)}'` #项目名称

###############编译目录
buildRootDir=~/Desktop/$projectName-IPA #ipa,icon最后所在的目录绝对路径
if [ -d "$buildRootDir" ]; then
echo $buildRootDir
else
echo "文件目录不存在"
mkdir -pv $buildRootDir
echo "创建${buildRootDir}目录成功"
fi

###############拉取代码,
cd $buildRootDir
rm -rf ./build
mkdir build
buildProgressDir=$buildRootDir/build #打包过程目录
cd $buildProgressDir
git clone ${gitPath}

projectDir=$buildProgressDir/$projectName #项目所在目录的绝对路径
echo $projectDir
exportPlist=$projectDir/build.plist  #build.plist路径
archivePath=$buildProgressDir/${projectName}.xcarchive #xcarchive路径

###############获取版本号,bundleID
infoPlist="$projectDir/$projectName/SupportingFiles/Info.plist"
bundleVersion=`/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" $infoPlist`
bundleIdentifier=`/usr/libexec/PlistBuddy -c "Print CFBundleIdentifier" $infoPlist`
bundleBuildVersion=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" $infoPlist`

###############开始编译app
cd $projectDir
if $isWorkSpace ; then  #判断编译方式
    echo "*****开始编译 workspace*****"
    xcodebuild -target ${projectName} -configuration ${buildConfig} -sdk iphoneos clean  SYMROOT=$buildProgressDir
    xcodebuild archive  -workspace $projectName.xcworkspace -scheme $projectName -archivePath ${archivePath}
else
    echo "*****开始编译 project*****"
    xcodebuild  -target  $projectName  -configuration $buildConfig clean build SYMROOT=$buildProgressDir
    xcodebuild archive  -project $projectName.xcodeproj -scheme $projectName -archivePath ${archivePath}
fi
#判断编译结果
if test $? -eq 0
then
echo "*****编译成功*****"
else
echo "*****编译失败*****"
exit 1
fi

###############开始打包成.ipa
appDir=$buildProgressDir/$findFolderName  #app所在路径
echo "开始打包${archivePath}至$buildProgressDir....."
xcodebuild -exportArchive -archivePath $archivePath -exportPath $buildProgressDir -exportOptionsPlist $exportPlist


###############检查文件是否存在确认打包成功
exportIpaPath=$buildProgressDir/$projectName.ipa
if [ -f "$exportIpaPath" ]
then
echo "$exportIpaPath 生成成功."
else
echo "打包失败."
exit 1
fi

###############复制文件到主目录
finalIPAPath=${buildRootDir}/${projectName}-$(date +%Y%m%d_%H%M).ipa
cp -f -p $exportIpaPath  $finalIPAPath   #拷贝ipa文件
echo "复制$ipaName.ipa到${finalIPAPath}成功"
echo "*****结束编译,处理成功*****"
#open $buildRootDir

###############上传到蒲公英,{uKey},{_api_key}需替换
export LANG=en_US
export LC_ALL=en_US;
echo "正在上传到蒲公英...."
curl -F "file=@$finalIPAPath" -F "uKey={uKey}" -F "_api_key={_api_key}" http://www.pgyer.com/apiv1/app/upload
#判断上传结果
if test $? -eq 0
then
echo "*****\^o^/*****上传到蒲公英成功*****\^o^/*****"
else
echo "*****\(╯-╰)/*****上传到蒲公英失败*****\(╯-╰)/*****"
exit 1
fi

###############删除构建目录
rm -rf $buildProgressDir

build.plist:
teamID:可登录https://developer.apple.com/account 登录相应账号查看Membership获取
method:app-store、enterprise、ad-hoc、development





    method
    enterprise
    teamID
    xxx
    compileBitcode
    


相对build_deployto_fir.sh主要做了以下变动:
1)xrun替换为xcodebuild -exportArchive
2)从远程clone代码,防止本地代码不是最新代码
3)优化一些其他逻辑

参考链接:
1)http://www.jianshu.com/p/bccbb37f21f9
2)https://github.com/heyuan110/BashShell

你可能感兴趣的:(Shell脚本自动会打包iPa并提交iPa(xcodebuild -exportArchive))