iOS一行shell脚本自动打包上传指定位置

2017-10-12 19:39:47来源:CSDN作者:hou_manager人点击

分享

一、背景

在实际开发中,需要不停的打各种包,开发人员忙于新需求实现,打包时重复而且没有意义的事情。于是造了这个轮子,配置好参数一键上传到内测网站(蒲公英、Fir等)或者APPStore。源码地址:https://github.com/stackhou/AutoPacking-iOS

二、预览效果图

2.1 执行脚本 和 选项配置

2.2 开始构建

2.3 构建成功并开始导出ipa

2.4 导出ipa成功并上传到内测网站

二、脚本环境

基于 Xcode 9 设计,注意Xcode 8和9有所区别,请参考我的另一篇:http://www.jianshu.com/p/ba179c731e3f ,
如有问题,欢迎指正。

三、功能

  • 支持 xcworkspace 和 xcodeproj 两种类型的工程;
  • 可以自动化清理、编译、构建工程导出ipa;
  • 支持Debug 和 Release;
  • 支持导出app-store, ad-hoc, enterprise, development的包;
  • 支持自动上传到蒲公英或者Fir等内测网站

四、实现

4.1 更新RVM

curl -L get.rvm.io | bash -s stable

4.2 所需知识点

xcodebuild clean            // 等同于Xcode下点击Product -> Cleanxcodebuild -xcworkspace     // 等同于xcworkspace工程 command+Bxcodebuild -xcodeproj       // 等同于xcworkspace工程 command+Bxcodebuild archive          // 等同于Xcode下点击Product -> Archivexcodebuild -exportArchive   // 等同于点击 export

五、脚本

#!/bin/sh# 该脚本使用方法# 源码地址:https://github.com/stackhou# step 1. 工程目录Shell新建文件夹,在文件夹中新建文件autopacking.sh,将该脚本复制到autopacking.sh文件并保存;# step 2. 配置该脚本;# step 2. cd 该脚本目录,运行chmod +x autopacking.sh;# step 3. 终端运行 sh autopacking.sh;# step 4. 选择不同选项....# step 5. Success  �� �� ��!# ************************* 配置 Start ********************************# 上传到蒲公英__PGYER_U_KEY="4xxxxxxxxxxxxxxxxxxxxxxxxxxxxxb"__PGYER_API_KEY="3xxxxxxxxxxxxxxxxxxxxxxxxxx5"# 上传到 Fir__FIR_API_TOKEN="xKKdjdldlodeikK626266skdkkddK"# 证书__CODE_SIGN_DISTRIBUTION="iPhone Distribution: xxxxxxxxxxxCo., Ltd."__CODE_SIGN_DEVELOPMENT="iPhone Developer: xxxx xxxx (5xxxxxxxxxx2V)"# 换行符__LINE_BREAK_LEFT="/n/033[32;1m*********"__LINE_BREAK_RIGHT="***************/033[0m/n"__SLEEP_TIME=0.3# 指定Targetecho "/033[36;1m请选择 SCHEME (输入序号, 按回车即可) /033[0m"echo "/033[33;1m1. APPxxxxDev /033[0m"echo "/033[33;1m2. APPxxxxTest /033[0m"echo "/033[33;1m3. APPxxxxRelease /033[0m"echo "/033[33;1m4. APPxxxxAppStore /033[0m/n"read parametersleep ${__SLEEP_TIME}__SCHEME_NAME_SELECTED="${parameter}"# 判读用户是否有输入if [[ "${__SCHEME_NAME_SELECTED}" == "1" ]]; then__SCHEME_NAME="APPxxxxDev"elif [[ "${__SCHEME_NAME_SELECTED}" == "2" ]]; then__SCHEME_NAME="APPxxxxTest"elif [[ "${__SCHEME_NAME_SELECTED}" == "3" ]]; then__SCHEME_NAME="APPxxxxRelease"elif [[ "${__SCHEME_NAME_SELECTED}" == "4" ]]; then__SCHEME_NAME="APPxxxxAppStore"elseecho "${__LINE_BREAK_LEFT} 您输入 SCHEME 参数无效!!! ${__LINE_BREAK_RIGHT}"exit 1fi# ************************* 配置 END ********************************# 指定打包编译的模式,如:Release, Debug...echo "/033[36;1m请选择 BUILD_CONFIGURATION (输入序号, 按回车即可) /033[0m"echo "/033[33;1m1. Debug /033[0m"echo "/033[33;1m2. Release /033[0m"read parametersleep ${__SLEEP_TIME}__BUILD_CONFIGURATION_SELECTED="${parameter}"# 判读用户是否有输入if [[ "${__BUILD_CONFIGURATION_SELECTED}" == "1" ]]; then__BUILD_CONFIGURATION="Debug"elif [[ "${__BUILD_CONFIGURATION_SELECTED}" == "2" ]]; then__BUILD_CONFIGURATION="Release"elseecho "${__LINE_BREAK_LEFT} 您输入 BUILD_CONFIGURATION 参数无效!!! ${__LINE_BREAK_RIGHT}"exit 1fi# 工程类型(.xcworkspace项目,赋值true; .xcodeproj项目, 赋值false)echo "/033[36;1m请选择是否是.xcworkspace项目(输入序号, 按回车即可) /033[0m"echo "/033[33;1m1. 是 /033[0m"echo "/033[33;1m2. 否 /033[0m"read parametersleep ${__SLEEP_TIME}__IS_WORKSPACE_SELECTE="${parameter}"# 判读用户是否有输入if [[ "${__IS_WORKSPACE_SELECTE}" == "1" ]]; then__IS_WORKSPACE=trueelif [[ "${__IS_WORKSPACE_SELECTE}" == "2" ]]; then__IS_WORKSPACE=falseelseecho "${__LINE_BREAK_LEFT} 您输入是否是.xcworkspace项目 参数无效!!! ${__LINE_BREAK_RIGHT}"exit 1fi# AdHoc, AppStore, Enterprise, Developmentecho "/033[36;1m请选择打包方式(输入序号, 按回车即可) /033[0m"echo "/033[33;1m1. AdHoc /033[0m"echo "/033[33;1m2. AppStore /033[0m"echo "/033[33;1m3. Enterprise /033[0m"echo "/033[33;1m4. Development /033[0m/n"# 读取用户输入并存到变量里read parametersleep ${__SLEEP_TIME}__BUILD_METHOD="${parameter}"# 判读用户是否有输入if [[ "${__BUILD_METHOD}" == "1" ]]; thenExportOptionsPlistPath="./Shell/Plist/AdHocExportOptionsPlist.plist"elif [[ "${__BUILD_METHOD}" == "2" ]]; thenExportOptionsPlistPath="./Shell/Plist/AppStoreExportOptionsPlist.plist"elif [[ "${__BUILD_METHOD}" == "3" ]]; thenExportOptionsPlistPath="./Shell/Plist/EnterpriseExportOptionsPlist.plist"elif [[ "${__BUILD_METHOD}" == "4" ]]; thenExportOptionsPlistPath="./Shell/Plist/DevelopmentExportOptionsPlist.plist"elseecho "${__LINE_BREAK_LEFT} 您输入的打包方式参数无效!!! ${__LINE_BREAK_RIGHT}"exit 1fi# 选择内测网站 用Fir或者pgyerecho "/033[36;1m请选择ipa内测发布平台 (输入序号, 按回车即可) /033[0m"echo "/033[33;1m1. None /033[0m"echo "/033[33;1m2. Pgyer /033[0m"echo "/033[33;1m3. Fir /033[0m"echo "/033[33;1m4. Pgyer and Fir /033[0m/n"# 读取用户输入并存到变量里read parametersleep ${__SLEEP_TIME}__UPLOAD_TYPE_SELECTED="${parameter}"# 成功出包后是否自动打开文件夹echo "/033[36;1m成功出包后是否自动打开文件夹(输入序号, 按回车即可) /033[0m"echo "/033[33;1m1. 是 /033[0m"echo "/033[33;1m2. 否 /033[0m"read parametersleep ${__SLEEP_TIME}__IS_AUTO_OPENT_FILE_SELECTED="${parameter}"# 判读用户是否有输入if [[ "${__IS_AUTO_OPENT_FILE_SELECTED}" == "1" ]]; then__IS_AUTO_OPENT_FILE=trueelif [[ "${__IS_AUTO_OPENT_FILE_SELECTED}" == "2" ]]; then__IS_AUTO_OPENT_FILE=falseelseecho "${__LINE_BREAK_LEFT} 您输入的成功出包后是否自动打开文件夹 参数错误!!! ${__LINE_BREAK_RIGHT}"exit 1fiecho "${__LINE_BREAK_LEFT} 您选择了 ${__SCHEME_NAME}-${__BUILD_CONFIGURATION} 模式 ${__LINE_BREAK_RIGHT}"# 配置完毕是否开始打包echo "/033[36;1m配置完毕是否立即开始打包 (输入序号, 按回车即可) /033[0m"echo "/033[33;1m1. 是 /033[0m"echo "/033[33;1m2. 否 /033[0m"read parametersleep ${__SLEEP_TIME}__IS_START_PACKINF="${parameter}"# 判读用户是否有输入if [[ "${__IS_START_PACKINF}" == "1" ]]; thenecho "${__LINE_BREAK_LEFT} 立即开始 ${__LINE_BREAK_RIGHT}"elif [[ "${__IS_START_PACKINF}" == "2" ]]; thenecho "${__LINE_BREAK_LEFT} 您退出了打包 ${__LINE_BREAK_RIGHT}"exit 1elseecho "${__LINE_BREAK_LEFT} 您输入 是否立即开始打包 参数无效!!! ${__LINE_BREAK_RIGHT}"exit 1fi# ===============================自动打包部分=============================echo "${__LINE_BREAK_LEFT} 使用打包配置文件路径=${ExportOptionsPlistPath} ${__LINE_BREAK_RIGHT}"# 打包计时__CONSUME_TIME=0# 回退到工程目录cd ../__PROGECT_PATH=`pwd`echo "${__LINE_BREAK_LEFT} 进入工程目录=${__PROGECT_PATH} ${__LINE_BREAK_RIGHT}"# 获取项目名称__PROJECT_NAME=`find . -name *.xcodeproj | awk -F "[/.]" '{print $(NF-1)}'`# 已经指定Target的Info.plist文件路径__CURRENT_INFO_PLIST_NAME="${__SCHEME_NAME}-Info.plist"# 获取 Info.plist 路径__CURRENT_INFO_PLIST_PATH="${__PROJECT_NAME}/Configs/${__CURRENT_INFO_PLIST_NAME}"# 当前的plist文件路径echo "${__LINE_BREAK_LEFT} 当前Info.plist路径= ${__CURRENT_INFO_PLIST_PATH} ${__LINE_BREAK_RIGHT}"# 获取版本号__BUNDLE_VERSION=`/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" ${__CURRENT_INFO_PLIST_PATH}`# 获取编译版本号__BUNDLE_BUILD_VERSION=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" ${__CURRENT_INFO_PLIST_PATH}`# 打印版本信息echo "${__LINE_BREAK_LEFT} 打包版本=${__BUNDLE_VERSION} 编译版本=${__BUNDLE_BUILD_VERSION} ${__LINE_BREAK_RIGHT}"# 编译生成文件目录__EXPORT_PATH="./build"# 指定输出文件目录不存在则创建if test -d "${__EXPORT_PATH}" ; thenecho "${__LINE_BREAK_LEFT} 保存归档文件和ipa的路径=${__EXPORT_PATH} ${__LINE_BREAK_RIGHT}"rm -rf ${__EXPORT_PATH}elsemkdir -pv ${__EXPORT_PATH}fi# 归档文件路径__EXPORT_ARCHIVE_PATH="${__EXPORT_PATH}/${__SCHEME_NAME}.xcarchive"# ipa 导出路径__EXPORT_IPA_PATH="${__EXPORT_PATH}"# 获取时间 如:201706011145__CURRENT_DATE="$(date +%Y%m%d_%H%M%S)"# ipa 名字__IPA_NAME="${__SCHEME_NAME}_V${__BUNDLE_BUILD_VERSION}_${__CURRENT_DATE}"echo "${__LINE_BREAK_LEFT} 打包APP名字=${__IPA_NAME} ${__LINE_BREAK_RIGHT}"# 修改编辑版本#__SET_BUNDLE_BUILD_VERSION="${__BUNDLE_BUILD_VERSION}.${__CURRENT_DATE}"#/usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${__SET_BUNDLE_BUILD_VERSION}" "${__CURRENT_INFO_PLIST_PATH}"echo "${__LINE_BREAK_LEFT} 开始构建项目 ${__LINE_BREAK_RIGHT}"if ${__IS_WORKSPACE} ; then#echo "${__LINE_BREAK_LEFT} 开始pod ${__LINE_BREAK_RIGHT}"#pod install --verbose --no-repo-update#echo "${__LINE_BREAK_LEFT} pod完成 ${__LINE_BREAK_RIGHT}"if [[ ${__BUILD_CONFIGURATION} == "Debug" ]]; thenecho "${__LINE_BREAK_LEFT} 您选择了以 xcworkspace-Debug 模式打包 ${__LINE_BREAK_RIGHT}"# step 1. Cleanxcodebuild clean  -workspace ${__PROJECT_NAME}.xcworkspace /-scheme ${__SCHEME_NAME} /-configuration ${__BUILD_CONFIGURATION}# step 2. Archivexcodebuild archive  -workspace ${__PROJECT_NAME}.xcworkspace /-scheme ${__SCHEME_NAME} /-configuration ${__BUILD_CONFIGURATION} /-archivePath ${__EXPORT_ARCHIVE_PATH} /CFBundleVersion=${__BUNDLE_BUILD_VERSION} /-destination generic/platform=ios /CODE_SIGN_IDENTITY="${__CODE_SIGN_DEVELOPMENT}"elif [[ ${__BUILD_CONFIGURATION} == "Release" ]]; thenecho "${__LINE_BREAK_LEFT} 您选择了以 xcworkspace-Release 模式打包 ${__LINE_BREAK_RIGHT}"# step 1. Cleanxcodebuild clean  -workspace ${__PROJECT_NAME}.xcworkspace /-scheme ${__SCHEME_NAME} /-configuration ${__BUILD_CONFIGURATION}# step 2. Archivexcodebuild archive  -workspace ${__PROJECT_NAME}.xcworkspace /-scheme ${__SCHEME_NAME} /-configuration ${__BUILD_CONFIGURATION} /-archivePath ${__EXPORT_ARCHIVE_PATH} /CFBundleVersion=${__BUNDLE_BUILD_VERSION} /-destination generic/platform=ios /CODE_SIGN_IDENTITY="${__CODE_SIGN_DISTRIBUTION}"elseecho "${__LINE_BREAK_LEFT} 您输入的参数不对 �� �� �� ${__LINE_BREAK_RIGHT}"echo "Usage:/n"echo "sh autopacking.sh"echo "sh autopacking.sh"exit 1fielseif [[ ${__BUILD_CONFIGURATION} == "Debug" ]] ; thenecho "${__LINE_BREAK_LEFT}您选择了以 xcodeproj-Debug 模式打包 ${__LINE_BREAK_RIGHT}"# step 1. Cleanxcodebuild clean  -project ${__PROJECT_NAME}.xcodeproj /-scheme ${__SCHEME_NAME} /-configuration ${__BUILD_CONFIGURATION} /-alltargets# step 2. Archivexcodebuild archive  -project ${__PROJECT_NAME}.xcodeproj /-scheme ${__SCHEME_NAME} /-configuration ${__BUILD_CONFIGURATION} /-archivePath ${__EXPORT_ARCHIVE_PATH} /CFBundleVersion=${__BUNDLE_BUILD_VERSION} /-destination generic/platform=ios /CODE_SIGN_IDENTITY="${__CODE_SIGN_DEVELOPMENT}"elif [[ ${__BUILD_CONFIGURATION} == "Release" ]]; thenecho "${__LINE_BREAK_LEFT} 您选择了以 xcodeproj-Release 模式打包 ${__LINE_BREAK_RIGHT}"# step 1. Cleanxcodebuild clean  -project ${__PROJECT_NAME}.xcodeproj /-scheme ${__SCHEME_NAME} /-configuration ${__BUILD_CONFIGURATION} /-alltargets# step 2. Archivexcodebuild archive  -project ${__PROJECT_NAME}.xcodeproj /-scheme ${__SCHEME_NAME} /-configuration ${__BUILD_CONFIGURATION} /-archivePath ${__EXPORT_ARCHIVE_PATH} /CFBundleVersion=${__BUNDLE_BUILD_VERSION} /-destination generic/platform=ios /CODE_SIGN_IDENTITY="${__CODE_SIGN_DISTRIBUTION}"elseecho "${__LINE_BREAK_LEFT} 您输入的参数不对 �� �� �� ${__LINE_BREAK_RIGHT}"echo "Usage:/n"echo "sh autopacking.sh"echo "sh autopacking.sh"exit 1fifi# 检查是否构建成功# xcarchive 实际是一个文件夹不是一个文件所以使用 -d 判断if test -d "${__EXPORT_ARCHIVE_PATH}" ; thenecho "${__LINE_BREAK_LEFT} 项目构建成功 �� �� �� ${__LINE_BREAK_RIGHT}"elseecho "${__LINE_BREAK_LEFT} 项目构建失败 �� �� �� ${__LINE_BREAK_RIGHT}"exit 1fiecho "${__LINE_BREAK_LEFT} 开始导出ipa文件 ${__LINE_BREAK_RIGHT}"xcodebuild -exportArchive -archivePath ${__EXPORT_ARCHIVE_PATH} /-exportPath ${__EXPORT_IPA_PATH} /-destination generic/platform=ios /-exportOptionsPlist ${ExportOptionsPlistPath} /-allowProvisioningUpdates# 修改ipa文件名称mv ${__EXPORT_IPA_PATH}/${__SCHEME_NAME}.ipa ${__EXPORT_IPA_PATH}/${__IPA_NAME}.ipa# 检查文件是否存在if test -f "${__EXPORT_IPA_PATH}/${__IPA_NAME}.ipa" ; thenecho "${__LINE_BREAK_LEFT} 导出 ${__IPA_NAME}.ipa 包成功 �� �� �� ${__LINE_BREAK_RIGHT}"if test -n "${__UPLOAD_TYPE_SELECTED}"thenif [[ "${__UPLOAD_TYPE_SELECTED}" == "1" ]] ; thenecho "${__LINE_BREAK_LEFT} 您选择了不上传到内测网站 ${__LINE_BREAK_RIGHT}"elif [[ "${__UPLOAD_TYPE_SELECTED}" == "2" ]]; thencurl -F "file=@${__EXPORT_IPA_PATH}/${__IPA_NAME}.ipa" /-F "uKey=$__PGYER_U_KEY" /-F "_api_key=$__PGYER_API_KEY" /"http://www.pgyer.com/apiv1/app/upload"echo "${__LINE_BREAK_LEFT} 上传 ${__IPA_NAME}.ipa 包 到 pgyer 成功 �� �� �� ${__LINE_BREAK_RIGHT}"elif [[ "${__UPLOAD_TYPE_SELECTED}" == "3" ]]; thenfir login -T ${__FIR_API_TOKEN}fir publish "${__EXPORT_IPA_PATH}/${__IPA_NAME}.ipa"echo "${__LINE_BREAK_LEFT} 上传 ${__IPA_NAME}.ipa 包 到 fir 成功 �� �� �� ${__LINE_BREAK_RIGHT}"elif [[ "${__UPLOAD_TYPE_SELECTED}" == "4" ]]; thenfir login -T ${__FIR_API_TOKEN}fir publish "${__EXPORT_IPA_PATH}/${__IPA_NAME}.ipa"echo "${__LINE_BREAK_LEFT} 上传 ${__IPA_NAME}.ipa 包 到 fir 成功 �� �� �� ${__LINE_BREAK_RIGHT}"curl -F "file=@{${__EXPORT_IPA_PATH}/${__IPA_NAME}.ipa}" /-F "uKey=$__PGYER_U_KEY" /-F "_api_key=$__PGYER_API_KEY" /"http://www.pgyer.com/apiv1/app/upload"echo "${__LINE_BREAK_LEFT} 上传 ${__IPA_NAME}.ipa 包 到 pgyer 成功 �� �� �� ${__LINE_BREAK_RIGHT}"elseecho "${__LINE_BREAK_LEFT} 您输入 上传内测网站 参数无效!!! ${__LINE_BREAK_RIGHT}"exit 1fi# 自动打开文件夹if ${__IS_AUTO_OPENT_FILE} ; thenopen ${__EXPORT_IPA_PATH}fifielseecho "${__LINE_BREAK_LEFT} 导出 ${__IPA_NAME}.ipa 包失败 �� �� �� ${__LINE_BREAK_RIGHT}"exit 1fi# 输出打包总用时echo "${__LINE_BREAK_LEFT} 使用YJShell脚本打包总耗时: ${SECONDS}s ${__LINE_BREAK_RIGHT}"

六、注意事项

注意ExportOptions.plist配置,如下所示:

<?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>compileBitcode</key>    <false/>    <key>method</key>    <string>enterprise</string>    <key>provisioningProfiles</key>    <dict>        <key>com.houmanager.enterprise.test</key>        <string>com.houmanager.enterprise.test</string>    </dict>    <key>signingCertificate</key>    <string>iPhone Distribution</string>    <key>signingStyle</key>    <string>manual</string>    <key>stripSwiftSymbols</key>    <true/>    <key>teamID</key>    <string>5XXXXXXXXXXXHM</string>    <key>thinning</key>    <string><none></string></dict></plist>

如果不知道怎么填写,手动用Xcode9打包,导出文件中会有ExportOptions.plist

直接复制到指定路径或者手动copy即可。


源码地址:https://github.com/stackhou/AutoPacking-iOS


最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台