Xcode8下基于Shell脚本的自动化打包及上传fir

最近源于项目迭代比较空闲,在这里总结下 xcode8执行xcodebuild自动化打包的实现,提高测试开发效率

自动化打包方式

1、shell 脚本执行
2、基于Jenkins的自动化打包
3、fastlane
这里我只用第一种方式 去实现...

以下执行脚本文件

该脚本在网上下载的,需要根据自己的环境去配置,包括rvm

#!/bin/sh
echo "~~~~~~~~~~~~~~~~开始执行脚本~~~~~~~~~~~~~~~~"
####################################################################
###################工程信息以及最下面的蒲公英信息########################
#工程名 获取工程名
PROJECTNAME=`find . -name *.xcodeproj | awk -F "[/.]" '{print $(NF-1)}'` #项目名称
#需要编译的 targetName Scheme
TARGET_NAME="weLianAppDev"
# ADHOC 内测
#证书名#描述文件  比如你是手动管理证书,改你相应的id
#ADHOCCODE_SIGN_IDENTITY="Z5FDR82ZVP"
#ADHOCPROVISIONING_PROFILE_NAME="f2b59a5a-85a5-4187-9f9b-676d56a13196"

#如果是自定管理证书  则填空,网上有人说 xcode8自动化打包只能手动管理,不清楚原因,我这里赋空值是没问题的
ADHOCCODE_SIGN_IDENTITY=""
ADHOCPROVISIONING_PROFILE_NAME=""

#AppStore证书名#描述文件
APPSTORECODE_SIGN_IDENTITY="V6JKEEZATW"
APPSTOREADHOCPROVISIONING_PROFILE_NAME="xxxxxx-xxxx-xxxx-xxxxxxxxxxxx"

#是否是工作空间
ISWORKSPACE=true
####################################################################

#证书名
CODE_SIGN_IDENTITY=${ADHOCCODE_SIGN_IDENTITY}
#描述文件
PROVISIONING_PROFILE_NAME=${ADHOCPROVISIONING_PROFILE_NAME}

# 开始时间
beginTime=`date +%s`
DATE=`date '+%Y-%m-%d-%T'`

#编译模式 工程默认有 Debug Release 
CONFIGURATION_TARGET=Release
#编译路径
BUILDPATH=~/Desktop/${TARGET_NAME}_${DATE}
#archivePath
ARCHIVEPATH=${BUILDPATH}/${TARGET_NAME}.xcarchive
#输出的ipa目录
IPAPATH=${BUILDPATH}

#导出ipa 所需plist
ADHOCExportOptionsPlist=./ADHOCExportOptionsPlist.plist
AppStoreExportOptionsPlist=./AppStoreExportOptionsPlist.plist

ExportOptionsPlist=${ADHOCExportOptionsPlist}

# 是否上传FIR
UPLOADFIR=false

echo "~~~~~~~~~~~~~~~~选择打包方式~~~~~~~~~~~~~~~~"
echo "      1 ad-hoc (默认)"
echo "      2 AppStore "

# 读取用户输入并存到变量里
read parameter
sleep 0.5
method="$parameter"

# 判读用户是否有输入 
if [ -n "$method" ]
then
    if [ "$method" = "1" ]
    then 
    CODE_SIGN_IDENTITY=${ADHOCCODE_SIGN_IDENTITY}
    PROVISIONING_PROFILE_NAME=${ADHOCPROVISIONING_PROFILE_NAME}
    ExportOptionsPlist=${ADHOCExportOptionsPlist}
    elif [ "$method" = "2" ]
    then
    CODE_SIGN_IDENTITY=${APPSTORECODE_SIGN_IDENTITY}
    PROVISIONING_PROFILE_NAME=${APPSTOREADHOCPROVISIONING_PROFILE_NAME}
    ExportOptionsPlist=${AppStoreExportOptionsPlist}
    else
    echo "参数无效...."
    exit 1
    fi
else
    ExportOptionsPlist=${ADHOCExportOptionsPlist}
fi

echo "~~~~~~~~~~~~~~~~是否上传FIR~~~~~~~~~~~~~~~~"
echo "      1 不上传 (默认)"
echo "      2 上传 "
read para
sleep 0.5

if [ -n "$para" ]
then
    if [ "$para" = "1" ]
    then 
    UPLOADFIR=false
    elif [ "$para" = "2" ]
    then
    UPLOADFIR=true
    else
    echo "参数无效...."
    exit 1
    fi
else
    UPLOADFIR=false
fi

###############获取版本号,bundleID info.plist 必须存在
infoPlist="${PROJECTNAME}/${TARGET_NAME}-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`

echo "~~~~~~~~~~~~~~~~开始编译~~~~~~~~~~~~~~~~~~~"

if [ $ISWORKSPACE = true ]
then
# 清理 避免出现一些莫名的错误
xcodebuild clean -workspace ${PROJECTNAME}.xcworkspace \
-configuration \
${CONFIGURATION} -alltargets

#开始构建
xcodebuild archive -workspace ${PROJECTNAME}.xcworkspace \
-scheme ${TARGET_NAME} \
-archivePath ${ARCHIVEPATH} \
-configuration ${CONFIGURATION_TARGET} \
CODE_SIGN_IDENTITY="${CODE_SIGN_IDENTITY}" \
PROVISIONING_PROFILE="${PROVISIONING_PROFILE_NAME}"
else
# 清理 避免出现一些莫名的错误
xcodebuild clean -xcodeproj ${PROJECTNAME}.xcodeproj \
-configuration \
${CONFIGURATION} -alltargets

#开始构建
xcodebuild archive -xcodeproj ${PROJECTNAME}.xcodeproj \
-scheme ${TARGET_NAME} \
-archivePath ${ARCHIVEPATH} \
-configuration ${CONFIGURATION_TARGET} \
CODE_SIGN_IDENTITY="${CODE_SIGN_IDENTITY}" \
PROVISIONING_PROFILE="${PROVISIONING_PROFILE_NAME}"
fi

echo "~~~~~~~~~~~~~~~~检查是否构建成功~~~~~~~~~~~~~~~~~~~"
# xcarchive 实际是一个文件夹不是一个文件所以使用 -d 判断
if [ -d "$ARCHIVEPATH" ]
then
echo "构建成功......"
else
echo "构建失败......"
rm -rf $BUILDPATH
exit 1
fi
endTime=`date +%s`
ArchiveTime="构建时间$[ endTime - beginTime ]秒"


echo "~~~~~~~~~~~~~~~~导出ipa~~~~~~~~~~~~~~~~~~~"

beginTime=`date +%s`

# 苹果对rvm 2.3.0 2.2.4 不支持
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
rvm use system
xcodebuild -exportArchive \
-archivePath ${ARCHIVEPATH} \
-exportOptionsPlist ${ExportOptionsPlist} \
-exportPath ${IPAPATH}

# source ./xcodesafe.sh -exportArchive \
# -archivePath ${ARCHIVEPATH} \
# -exportOptionsPlist ${ExportOptionsPlist} \
# -exportPath ${IPAPATH}

echo "~~~~~~~~~~~~~~~~检查是否成功导出ipa~~~~~~~~~~~~~~~~~~~"
IPAPATH=${IPAPATH}/${TARGET_NAME}.ipa
if [ -f "$IPAPATH" ]
then
echo "导出ipa成功......"
open $BUILDPATH
else
echo "导出ipa失败......"
# 结束时间
endTime=`date +%s`
echo "$ArchiveTime"
echo "导出ipa时间$[ endTime - beginTime ]秒"
exit 1
fi

endTime=`date +%s`
ExportTime="导出ipa时间$[ endTime - beginTime ]秒"

echo "~~~~~~~~~~~~~~~~配置信息~~~~~~~~~~~~~~~~~~~"
ReadPath=$BUILDPATH/README
if [ -f "$ReadPath" ]
then
echo "README 已存在."
else
touch $ReadPath
echo "README 创建成功"
fi

echo "开始执行脚本时间: ${DATE}"  >> $ReadPath
echo "TARGET_NAME scheme: ${TARGET_NAME}"  >> $ReadPath
echo "编译模式: ${CONFIGURATION_TARGET}"  >> $ReadPath
echo "导出ipa配置: ${ExportOptionsPlist}"  >> $ReadPath
echo "打包文件路径: ${ARCHIVEPATH}"  >> $ReadPath
echo "导出ipa路径: ${IPAPATH}"  >> $ReadPath
echo "ipa大小:" `ls -l $IPAPATH | awk '{ print $5,$9 }'` >> $ReadPath
echo "版本号:${bundleVersion}" >> $ReadPath

echo "$ArchiveTime"  >> $ReadPath
echo "$ExportTime"  >> $ReadPath

cat $ReadPath

# 上传FIR 
if [ $UPLOADFIR = true ]
then
# 必须 复原 ruby 的版本 否则fir 失效
    rvm default
    echo "~~~~~~~~~~~~~~~~上传ipa到Fir.com~~~~~~~~~~~~~~~~~~~"
    firAppId=567cf766748aac3a8800001a
    firToken=0a9b84dbc15d70c88a0c7bd3a71c77d2
    echo ${IPAPATH}
    
    fir p ${IPAPATH}
    clog=`cat ${ReadPath}`
    curl -X PUT --data "changelog=$clog" http://fir.im/api/v2/app/$firAppId?token=$firToken

    if [ $? = 0 ]
    then
    echo "~~~~~~~~~~~~~~~~上传Fir成功~~~~~~~~~~~~~~~~~~~"
    else
    echo "~~~~~~~~~~~~~~~~上传Fir失败~~~~~~~~~~~~~~~~~~~"
    fi
fi
exit 1

几个问题

  • 脚本证书配置
    在 build seting里 我证书签名 都是选的xcode8 自动管理
  • xcodebuild 在rvm 在OS X exportArchieve 失败问题,下面会有写

http://www.jianshu.com/p/bd4c22952e01
https://wx.abbao.cn/a/4808-923b964d08d406e5.html
https://inkiu0.gitbooks.io/codenote/content/xcode8zidong_da_bao_qu_diao_automaticallymanag_md.html
http://www.jianshu.com/p/ce60368ea768
http://www.jianshu.com/p/b3dcfa8ddf63

你可能感兴趣的:(Xcode8下基于Shell脚本的自动化打包及上传fir)