近期在学习CI(Continuous Intigration,持续集成)。为了实际操作一下,以一个iOS App Demo作为“小白鼠”试验了一下:即将构建、单元测试、打包等一系列流程用脚本自动化了,以把人从重复劳动中解放出来。
当然,这仅仅是demo。
在实现CI的路上,最让人望而却步的还是写单元测试这一步。那是漫长的积累过程,也是对程序员耐力、分解抽象能力最大的考验。这几十行脚本不过是把最外层、关键的几个环节给自动化“串”起来。
下面代码摘自demo中的build.sh文件
#!/bin/sh
projName="ci-demo"
scheme="${projName}"
archive="${projName}.xcarchive"
ipa="${projName}.ipa"
outPath="./build"
archivePath="${outPath}/${archive}"
ipaPath="${outPath}/${ipa}"
logPath="${outPath}/log"
buildLog="${logPath}/build.log"
archiveLog="${logPath}/archive.log"
exportLog="${logPath}/export.log"
# show the commands
set -x
echo "clear old outputs..."
del -r ${outPath}
mkdir ${outPath}
mkdir ${logPath}
# 1. buid&test
echo "build&test..."
xcodebuild -workspace ${projName}.xcworkspace/ -scheme ${scheme} -destination 'platform=iOS Simulator,name=iPhone 5s' clean build test > ${buildLog}
if [ 0 == $? ]; then
echo "build&test succ..."
# 2. archive
echo "archive..."
xcodebuild -workspace ${projName}.xcworkspace/ -scheme ${scheme} -archivePath ${archivePath} archive > ${archiveLog}
if [ 0 == $? ]; then
echo "archive succ..."
# 3. export ipa
echo "export ipa..."
xcodebuild -exportArchive -archivePath ${archivePath} -exportPath ${ipaPath} > ${exportLog}
if [ 0 == $? ]; then
echo "export ipa succ, so all succ"
exit 0
else
echo "export ipa failed"
exit 3
fi
else
echo "archive failed"
exit 2
fi
else
echo "build failed"
exit 1
fi