iOS开发CIiOS开发

4.通过shell脚本完成iOS打包并上传至蒲公英测试平台

2017-03-02  本文已影响1108人  Lee丶Way

背景:

随着公司业务的扩大,人员的增多,每次通过XCode给测试人员Run代码进行测试,已经满足不了,况且Run出来的程序不能够很好的留下“证据”,所以自动化构建项目是很必要的。
搜集了相关资料,通过OS X Server和Jenkins使用居多,然而小弟愚笨,OS X Server环境已经构建好,但是却未能成功完成目的,Jenkins也一直没有去做,而是退而求次的使用shell编写一个脚本,依靠XCode中的xcodebuild和xcrun来实现代码的打包。

简单说几个常用的xcodebuild命令

可以执行 xcodebuild -help 查看都有哪些

xcodebuild [-project <projectname>] [[-target <targetname>]...|-alltargets] [-configuration <configurationname>] [-arch <architecture>]... [-sdk [<sdkname>|<sdkpath>]] [-showBuildSettings] [<buildsetting>=<value>]... [<buildaction>]...
xcodebuild [-project <projectname>] -scheme <schemeName> [-destination <destinationspecifier>]... [-configuration <configurationname>] [-arch <architecture>]... [-sdk [<sdkname>|<sdkpath>]] [-showBuildSettings] [<buildsetting>=<value>]... [<buildaction>]...
xcodebuild -workspace <workspacename> -scheme <schemeName> [-destination <destinationspecifier>]... [-configuration <configurationname>] [-arch <architecture>]... [-sdk [<sdkname>|<sdkpath>]] [-showBuildSettings] [<buildsetting>=<value>]... [<buildaction>]...
xcodebuild -version [-sdk [<sdkfullpath>|<sdkname>] [<infoitem>] ]
xcodebuild -list [[-project <projectname>]|[-workspace <workspacename>]] [-json]
xcodebuild -showsdks
xcodebuild -exportArchive -archivePath <xcarchivepath> -exportPath <destinationpath> -exportOptionsPlist <plistpath>
xcodebuild -exportLocalizations -localizationPath <path> -project <projectname> [-exportLanguage <targetlanguage>...]
xcodebuild -importLocalizations -localizationPath <path> -project <projectname>

Options:
    -usage                              print brief usage
    -help                               print complete usage
    -verbose                            provide additional status output
    -license                            show the Xcode and SDK license agreements
    -checkFirstLaunchStatus             Check if any First Launch tasks need to be performed
    -project NAME                       build the project NAME
    -target NAME                        build the target NAME
    -alltargets                         build all targets
    -workspace NAME                     build the workspace NAME
    -scheme NAME                        build the scheme NAME
    -configuration NAME                 use the build configuration NAME for building each target
    -xcconfig PATH                      apply the build settings defined in the file at PATH as overrides
    -arch ARCH                          build each target for the architecture ARCH; this will override architectures defined in the project
    -sdk SDK                            use SDK as the name or path of the base SDK when building the project
    -toolchain NAME                     use the toolchain with identifier or name NAME
    -destination DESTINATIONSPECIFIER   use the destination described by DESTINATIONSPECIFIER (a comma-separated set of key=value pairs describing the destination to use)
    -destination-timeout TIMEOUT        wait for TIMEOUT seconds while searching for the destination device
    -parallelizeTargets                 build independent targets in parallel
    -jobs NUMBER                        specify the maximum number of concurrent build operations
    -dry-run                            do everything except actually running the commands
    -quiet                              do not print any output except for warnings and errors
    -hideShellScriptEnvironment         don't show shell script environment variables in build log
    -showsdks                           display a compact list of the installed SDKs
    -showBuildSettings                  display a list of build settings and values
    -list                               lists the targets and configurations in a project, or the schemes in a workspace
    -find-executable NAME               display the full path to executable NAME in the provided SDK and toolchain
    -find-library NAME                  display the full path to library NAME in the provided SDK and toolchain
    -version                            display the version of Xcode; with -sdk will display info about one or all installed SDKs
    -enableAddressSanitizer YES|NO      turn the address sanitizer on or off
    -enableThreadSanitizer YES|NO       turn the thread sanitizer on or off
    -resultBundlePath PATH              specifies the directory where a result bundle describing what occurred will be placed
    -derivedDataPath PATH               specifies the directory where build products and other derived data will go
    -archivePath PATH                   specifies the directory where any created archives will be placed, or the archive that should be exported
    -exportArchive                      specifies that an archive should be exported
    -exportOptionsPlist PATH            specifies a path to a plist file that configures archive exporting
    -enableCodeCoverage YES|NO          turn code coverage on or off when testing
    -exportPath PATH                    specifies the destination for the product exported from an archive
    -skipUnavailableActions             specifies that scheme actions that cannot be performed should be skipped instead of causing a failure
    -exportLocalizations                exports completed and outstanding project localizations
    -importLocalizations                imports localizations for a project, assuming any necessary localized resources have been created in Xcode
    -localizationPath                   specifies a path to XLIFF localization files
    -exportLanguage                     specifies multiple optional ISO 639-1 languages included in a localization export
    -xctestrun                          specifies a path to a test run specification
    -only-testing:TEST-IDENTIFIER       constrains testing by specifying tests to include, and excluding other tests
    -skip-testing:TEST-IDENTIFIER       constrains testing by specifying tests to exclude, but including other tests
    -json                               output as JSON (note: -json implies -quiet)
1.xcodebuild -list
➜  xxx-ios git:(develop) xcodebuild -list
Information about project "xxx":
    Targets:
        xxx
        xxxTests
        xxxUITests
    Build Configurations:
        Debug
        Release
    If no build configuration is specified and -scheme is not passed then "Release" is used.
    Schemes:
        xxx

通过输出结果可以得出 -list 展示的是 你当前工程的 Targets Schemes 以及可选择的Build Configurations

2.xcodebuild -version
➜  xxx-ios git:(develop) xcodebuild -version
Xcode 8.2.1
Build version 8C1002

Xcode的版本

3.xcodebuild -workspace
xcodebuild -workspace <workspacename> -scheme <schemeName> [-destination <destinationspecifier>]... [-configuration <configurationname>] [-arch <architecture>]... [-sdk [<sdkname>|<sdkpath>]] [-showBuildSettings] [<buildsetting>=<value>]... [<buildaction>]...

这个是build工程所需要命令,包括的参数有scheme configuration等,这些在-list中都可以拿到
当然,xcodebuild不只只这几个命令,还有很多,例如-license -showsdks 等等等等。

我们使用的这句就够了

xcodebuild \
-workspace $SOURCEPATH/$SCHEMENAME.$SUFFIXNAME \
-scheme $SCHEMENAME \
-configuration $CONGRUATION \
clean \
build \
-derivedDataPath $IPAPATH 

这个是复制sh脚本中的内容,后面我会解释含义

Build完事之后就改导出ipa文件,使用的是xcrun命令

同样的先看看 xcrun下都有哪些

➜  xxx-ios git:(develop) xcrun -help
Usage: xcrun [options] <tool name> ... arguments ...
Find and execute the named command line tool from the active developer
directory.
The active developer directory can be set using `xcode-select`, or via the
DEVELOPER_DIR environment variable. See the xcrun and xcode-select manual
pages for more information.

Options:
  -h, --help                  show this help message and exit
  --version                   show the xcrun version
  -v, --verbose               show verbose logging output
  --sdk <sdk name>            find the tool for the given SDK name
  --toolchain <name>          find the tool for the given toolchain
  -l, --log                   show commands to be executed (with --run)
  -f, --find                  only find and print the tool path
  -r, --run                   find and execute the tool (the default behavior)
  -n, --no-cache              do not use the lookup cache
  -k, --kill-cache            invalidate all existing cache entries
  --show-sdk-path             show selected SDK install path
  --show-sdk-version          show selected SDK version
  --show-sdk-build-version    show selected SDK build version
  --show-sdk-platform-path    show selected SDK platform path
  --show-sdk-platform-version show selected SDK platform version

使用的也是一句

#xcrun .ipa
xcrun -sdk iphoneos PackageApplication \
-v $IPAPATH/Build/Products/$CONGRUATION-iphoneos/$SCHEMENAME.app \
-o $IPAPATH/$IPANAME

好了,重要的这两句已经搞定了。接下来就要进行脚本的制作了

我们的代码托管用的是git,针对git主要使用的是 checkout 和 pull 。

git checkout $BRANCHNAME
if [ $? -ne ]; then
    #statements
    exit 1
fi
git pull
if [ $? -ne 0 ]; then
    #statements
    exit 1
fi 

对于git其他命令感兴趣的,自行百度吧。

最后就是往蒲公英托管内测平台提交文件了,这个根据他文档的说明

蒲公英上传App

curl -F "file=@$IPAPATH/$IPANAME" \
-F "uKey=$PGYUSERKEY" \
-F "_api_key=$PGYAPIKEY" \
-F "password=password" \
https://qiniu-storage.pgyer.com/apiv1/app/upload

👌大功告成

最后粘上我的代码

#!/bin/bash
# Author leeway 

# $1工程所在的根路径 
# $2工程打包ipa的路径
# $3分支名字develop OR master
# $4工程的名字一般情况下和scheme一致 app1 app2 app3
# $5启动脚本传入Debug或者Release就可以
PROJECTPATH=$1
PROJECTIPAPATH=$2
BRANCHNAME=$3
SCHEMENAME=$4
CONGRUATION=$5
#我这里假设,有的app用的pods有的没有用
if [[ $SCHEMENAME = app1 ]]; then
    #如果是app1
    SUFFIXNAME=xcworkspace
elif [[ $SCHEMENAME = app2 ]]; then
    #app2
    SUFFIXNAME=xcodeproj
else
    #app3
    SUFFIXNAME=xcworkspace
fi

PGYUSERKEY=716e2e6b24b34d65xxx
PGYAPIKEY=dd5c15cc9e7c7ff2592xxx


DATE=`date +%Y%m%d%H%M`
SOURCEPATH=$( cd $PROJECTPATH && pwd )
IPAPATH=$PROJECTIPAPATH/AutoBuildIPA/$BRANCHNAME/$DATE
IPANAME=$SCHEMENAME$DATE.ipa

git checkout $BRANCHNAME
if [ $? -ne ]; then
    #statements
    exit 1
fi

git pull
if [ $? -ne 0 ]; then
    #statements
    exit 1
fi

# delete trash files
if [ -e $IPAPATH/* ]; then
    #statements
    mv $IPAPATH/* ~/.trash
    if [ $? -ne 0 ]; then
        #statements
        echo "error:delete trash files failed!"
        exit 1
    fi
fi


# xcodebuild .app
xcodebuild \
-workspace $SOURCEPATH/$SCHEMENAME.$SUFFIXNAME \
-scheme $SCHEMENAME \
-configuration $CONGRUATION \
clean \
build \
-derivedDataPath $IPAPATH 

if [ -e $IPAPATH ]; then
    #statements
    echo "xcodebuild Successful!!!"
else
    echo "error: Build failed!!!"
fi

#xcrun .ipa
xcrun -sdk iphoneos PackageApplication \
-v $IPAPATH/Build/Products/$CONGRUATION-iphoneos/$SCHEMENAME.app \
-o $IPAPATH/$IPANAME

if [ -e $IPAPATH/$IPANAME ]; then
    #statements
    echo "\n--------------------------------------------\n\n\n"
    echo "leeway tell you: Configurations! Build Successful!"
    echo "\n\n\n-------------------------------------------\n\n"
    echo "Current Branch log:"
else
    echo "\n-------------------------------------------------\n"
    echo "leeway tell you: error:Create IPA failed!!"
    echo "\nPlease check the cause of failure and contact leeway"
    echo "\n-------------------------------------------------\n"

fi

#自动上传到蒲公英
curl -F "file=@$IPAPATH/$IPANAME" \
-F "uKey=$PGYUSERKEY" \
-F "_api_key=$PGYAPIKEY" \
-F "password=xxxxxxx" \
https://qiniu-storage.pgyer.com/apiv1/app/upload
上一篇下一篇

猜你喜欢

热点阅读