我们这里选择执行一段打包脚本。脚本在下一章节详细的讲解。 构建后操作
这里我们选择Execute a set of scripts,这里也是一个脚本,这个脚本用来上传自动打包好的ipa文件。脚本在第四章节有详细的讲解。
至此,我们的Jenkins设置就全部完成了。点击构建,就会开始构建项目了。
构建一次,各个颜色代表的意义如下:
天气的晴雨表代表了项目的质量,这也是Jenkins的一个特色。
如果构建失败了,可以去查看Console Output可以查看log日志。
三、iOS自动化打包命令——xcodebuild + xcrun 和 fastlane - gym 命令
在日常开发中,打包是最后上线不可缺少的环节,如果需要把工程打包成ipa 文件,通常的做法就是在 Xcode 里点击 「Product -> Archive」,当整个工程 archive 后,然后在自动弹出的 「Organizer」 中进行选择,根据需要导出 ad hoc,enterprise 类型的 ipa 包。虽然Xcode已经可以很完美的做到打包的事情,但是还是需要我们手动点击5,6下。加上我们现在需要持续集成,用打包命令自动化执行就顺其自然的需要了。 1. xcodebuild + xcrun命令
Xcode为我们开发者提供了一套构建打包的命令,就是xcodebuild
和xcrun命令。xcodebuild把我们指定的项目打包成.app文件,xcrun将指定的.app文件转换为对应的.ipa文件。
具体的文档如下:xcodebuild官方文档、xcrun官方文档
NAMExcodebuild – build Xcode projects and workspacesSYNOPSIS1. xcodebuild [-project name.xcodeproj] [[-target targetname] … | -alltargets] [-configuration configurationname] [-sdk [sdkfullpath | sdkname]] [action …] [buildsetting=value …] [-userdefault=value …]2. xcodebuild [-project name.xcodeproj] -scheme schemename [[-destination destinationspecifier] …] [-destination-timeout value] [-configuration configurationname] [-sdk [sdkfullpath | sdkname]] [action …] [buildsetting=value …] [-userdefault=value …]3. xcodebuild -workspace name.xcworkspace -scheme schemename [[-destination destinationspecifier] …] [-destination-timeout value] [-configuration configurationname] [-sdk [sdkfullpath | sdkname]] [action …] [buildsetting=value …] [-userdefault=value …]4. xcodebuild -version [-sdk [sdkfullpath | sdkname]] [infoitem]5. xcodebuild -showsdks6. xcodebuild -showBuildSettings [-project name.xcodeproj | [-workspace name.xcworkspace -scheme schemename]]7. xcodebuild -list [-project name.xcodeproj | -workspace name.xcworkspace]8. xcodebuild -exportArchive -archivePath xcarchivepath -exportPath destinationpath -exportOptionsPlist path9. xcodebuild -exportLocalizations -project name.xcodeproj -localizationPath path [[-exportLanguage language] …]10. xcodebuild -importLocalizations -project name.xcodeproj -localizationPath path 上面10个命令最主要的还是前3个。
接下来来说明一下参数:
-project -workspace:这两个对应的就是项目的名字。如果有多个工程,这里又没有指定,则默认为第一个工程。
-target:打包对应的targets,如果没有指定这默认第一个。
-configuration:如果没有修改这个配置,默认就是Debug和Release这两个版本,没有指定默认为Release版本。
-buildsetting=value ...:使用此命令去修改工程的配置。
-scheme:指定打包的scheme。
上面这些是最最基本的命令。
上面10个命令的第一个和第二个里面的参数,其中 -target
和 -configuration 参数可以使用 xcodebuild -list
获得,-sdk 参数可由 xcodebuild -showsdks
获得,[buildsetting=value ...] 用来覆盖工程中已有的配置。可覆盖的参数参考官方文档:Xcode Build Setting Reference。
buildBuild the target in the build root (SYMROOT). This is the default action, and is used if no action is given.analyzeBuild and analyze a target or scheme from the build root (SYMROOT). This requires specifying a scheme.archiveArchive a scheme from the build root (SYMROOT). This requires specifying a scheme.testTest a scheme from the build root (SYMROOT). This requires specifying a scheme and optionally a destination.installsrcCopy the source of the project to the source root (SRCROOT).installBuild the target and install it into the target’s installation directory in the distribution root (DSTROOT).cleanRemove build products and intermediate files from the build root (SYMROOT). 上面第3个命令就是专门用来打带有Cocopods的项目,因为这个时候项目工程文件不再是xcodeproj了,而是变成了xcworkspace了。
再来说说xcrun命令。
Usage:PackageApplication [-s signature] application [-o output_directory] [-verbose] [-plugin plugin] || -man || -helpOptions:[-s signature]: certificate name to resign application before packaging[-o output_directory]: specify output filename[-plugin plugin]: specify an optional plugin-help: brief help message-man: full documentation-v[erbose]: provide details during operation 参数不多,使用方法也很简单,xcrun -sdk iphoneos -v PackageApplication + 上述一些参数。
参数都了解之后,我们就来看看该如何用了。下面这个是使用了xcodebuild + xcrun命令写的自动化打包脚本
# 工程名APP_NAME="YourProjectName"# 证书CODE_SIGN_DISTRIBUTION="iPhone Distribution: Shanghai ******* Co., Ltd."# info.plist路径project_infoplist_path="./${APP_NAME}/Info.plist"#取版本号bundleShortVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleShortVersionString" "${project_infoplist_path}")#取build值bundleVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleVersion" "${project_infoplist_path}")DATE="$(date +%Y%m%d)"IPANAME="${APP_NAME}_V${bundleShortVersion}_${DATE}.ipa"#要上传的ipa文件路径IPA_PATH="$HOME/${IPANAME}"echo ${IPA_PATH}echo "${IPA_PATH}">> text.txt//下面2行是没有Cocopods的用法echo "=================clean================="xcodebuild -target "${APP_NAME}" -configuration 'Release' cleanecho "+++++++++++++++++build+++++++++++++++++"xcodebuild -target "${APP_NAME}" -sdk iphoneos -configuration 'Release' CODE_SIGN_IDENTITY="${CODE_SIGN_DISTRIBUTION}" SYMROOT='$(PWD)'//下面2行是集成有Cocopods的用法echo "=================clean================="xcodebuild -workspace "${APP_NAME}.xcworkspace" -scheme "${APP_NAME}" -configuration 'Release' cleanecho "+++++++++++++++++build+++++++++++++++++"xcodebuild -workspace "${APP_NAME}.xcworkspace" -scheme "${APP_NAME}" -sdk iphoneos -configuration 'Release' CODE_SIGN_IDENTITY="${CODE_SIGN_DISTRIBUTION}" SYMROOT='$(PWD)'xcrun -sdk iphoneos PackageApplication "./Release-iphoneos/${APP_NAME}.app" -o ~/"${IPANAME}" 2. gym 命令
说到gym,就要先说一下fastlane。
fastlane是一套自动化打包的工具集,用 Ruby 写的,用于 iOS 和 Android 的自动化打包和发布等工作。gym是其中的打包命令。
fastlane的官网看这里, fastlane的github看这里。
要想使用gym,先要安装fastlane。
sudo gem install fastlane --verbose