XCode8 下最简单的自动集成打包脚本

1,041 阅读2分钟

公司已经有一个CI平台,但是由于CI上生成的安装包是用企业证书,有一些和证书相关的功能无法测试(push,微博sdk等),所以经常需要手动生成一个develoment的安装包,实在是有点麻烦。捣鼓了几个小时,发现其实不需要配置复杂的jenkins,了解一点最简单的命令行,就能打造出一个简易版的自动集成工具。

话不多说,来看看到底怎么做吧!

###1.使用 xcodebuild archive并export出ipa包

首先需要将Xcode配置改成auto codesign,勾上下图红框中的勾,这样xcode会自动根据你的configuration参数去配置对应的证书和描述文件。

Paste_Image.png

archive命令:

xcodebuild -workspace projectname.xcworkspace -scheme schemename -configuration Debug -derivedDataPath DerivedData -archivePath "bin/projectname.xcarchive"  -destination generic/platform=iOS  clean archive

export ipa命令:

xcodebuild -exportArchive -exportOptionsPlist ./exportPlist.plist -archivePath "bin/projectname.xcarchive" -exportPath "bin/"

-exportOptionsPlist这个参数需要有一个xml的配置文件,文件格式如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>teamID</key>
	<string>xxxxxxxxxx</string>
	<key>method</key>
	<string>development</string>
	<key>uploadSymbols</key>
	<false/>
</dict>
</plist>

其中teamID可以在 https://developer.apple.com/account/#/membership 查到。

###2.使用蒲公英账号上传ipa包

curl -F "file=@bin/projectname.ipa" \
-F "uKey=xxxxxxxxxxx" \
-F "_api_key=xxxxxxxxxxx" \
https://www.pgyer.com/apiv1/app/upload

uKey_api_key在你的蒲公英账号配置中能找到,具体不赘述。

###3.创建分支更新代码,避免污染开发分支 完成了上面两步,每次还是得手动去将xcode配置修改,在打完包后还原,还是很不方便,可以将工程代码重新checkout一份,并创建一个新的分支,在分支上进行打包执行打包操作:

git checkout 开发分支名
git pull
git checkout pushTest
git merge develop --no-commit
git commit -m 'pushtest merge'
git push
pod update

###END 最后整个脚本如下:

git checkout 开发分支名
git pull
git checkout pushTest
git merge develop --no-commit
git commit -m 'pushtest merge'
git push
pod update

xcodebuild -workspace projectname.xcworkspace -scheme schemename -configuration Debug -derivedDataPath DerivedData -archivePath "bin/projectname.xcarchive"  -destination generic/platform=iOS  clean archive

xcodebuild -exportArchive -exportOptionsPlist ./exportPlist.plist -archivePath "bin/projectname.xcarchive" -exportPath "bin/"

curl -F "file=@bin/projectname.ipa" \
-F "uKey=xxxxxxxxxxx" \
-F "_api_key=xxxxxxxxxxx" \
https://www.pgyer.com/apiv1/app/upload

好像有点简陋,我们可以将脚本按功能拆分开来,并加上条件判断: update.sh:

git checkout 开发分支名
git pull
git checkout pushTest
git merge develop --no-commit
git commit -m 'pushtest merge'
git push
pod update

upload.sh:

curl -F "file=@bin/projectname.ipa" \
-F "uKey=xxxxxxxxxxx" \
-F "_api_key=xxxxxxxxxxx" \
https://www.pgyer.com/apiv1/app/upload

packg.sh:

cd /Users/admin/Desktop/workspace/projectname

sh update.sh
if [ $? -eq 0 ]; then
xcodebuild -workspace SnobMass.xcworkspace -scheme SnobMass -configuration Debug -derivedDataPath DerivedData -archivePath "bin/SnobMass.xcarchive"  -destination generic/platform=iOS  clean archive
if [ $? -eq 0 ]; then
xcodebuild -exportArchive -exportOptionsPlist ./exportPlist.plist -archivePath "bin/SnobMass.xcarchive" -exportPath "bin/"
if [ $? -eq 0 ]; then
sh upload.sh

fi
fi
fi

最后利用alias将脚本定制为命令

cp packg.sh ~/
vim ~/.bashrc

加入下面的命令:

alias buildpush='sh ~/packg.sh'

执行

source ~/.bashrc

然后打开~/.bash_profile 在里面加入一行:

source ~/.bashrc

让alias永久生效。 大功告成,之后再打包,就只需要打开终端,执行buildpush这个命令就行了。