# 进入当前工作空间目录
cd ${WORKSPACE}/# Project名称
PROJECT_NAME="xxx"## Scheme名, 具体获取看图一
SCHEME_NAME="xxx"## 编译类型 Debug/Release二选一
BUILD_TYPE="Release"## 项目根路径,.xcodeproj文件所在路径
PROJECT_ROOT_PATH="xxx"## 打包生成路径
PRODUCT_PATH="${PROJECT_ROOT_PATH}/output"## ExportOptions_xxx.plist文件的存放路径,该文件描述了导出ipa文件所需要的配置, 如果不知道如何配置该plist,可直接使用xcode打包ipa结果文件夹的ExportOptions.plist文件, 文件格式参考图二
if [[ $env == 'test' ]]; thenEXPORTOPTIONSPLIST_PATH="${PROJECT_ROOT_PATH}/Archives/ExportOptions_AppHoc.plist"echo "测试包"
elseEXPORTOPTIONSPLIST_PATH="${PROJECT_ROOT_PATH}/Archives/ExportOptions_AppStore.plist"echo "线上包"
fi## project路径
PROJECT_PATH="${PROJECT_ROOT_PATH}/${PROJECT_NAME}.xcodeproj"## 清理缓存
xcodebuild clean -project ${PROJECT_PATH} -scheme ${SCHEME_NAME} -configuration ${BUILD_TYPE} || exit# 设置新的版本号
#获取Version
VERSION_NUMBER=${VERSION_NUMBER}
# 获取build
BUILD_NUMBER=${BUILD_NUMBER}echo ${PROJECT_PATH}# 修改MARKETING_VERSION
sed -i "" "s/MARKETING_VERSION = .*/MARKETING_VERSION = ${VERSION_NUMBER};/g" "${PROJECT_PATH}/project.pbxproj"# 修改CURRENT_PROJECT_VERSION
sed -i "" "s/CURRENT_PROJECT_VERSION = .*/CURRENT_PROJECT_VERSION = ${BUILD_NUMBER};/g" "${PROJECT_PATH}/project.pbxproj"#获取Version
# VERSION_NUMBER=`sed -n '/MARKETING_VERSION = /{s/MARKETING_VERSION = //;s/;//;s/^[[:space:]]*//;p;q;}' ${PROJECT_PATH}/project.pbxproj`
# 获取build
# BUILD_NUMBER=`sed -n '/CURRENT_PROJECT_VERSION = /{s/CURRENT_PROJECT_VERSION = //;s/;//;s/^[[:space:]]*//;p;q;}' ${PROJECT_PATH}/project.pbxproj`## IPA所在目录路径
IPA_DIR_NAME="${VERSION_NUMBER}_${BUILD_NUMBER}"##xcarchive文件的存放路径
ARCHIVE_PATH="${PRODUCT_PATH}/IPA/${IPA_DIR_NAME}/${SCHEME_NAME}.xcarchive"
## ipa文件的存放路径
IPA_PATH="${PRODUCT_PATH}/IPA/${IPA_DIR_NAME}"## 导出archive包
xcodebuild archive -project ${PROJECT_PATH} -scheme ${SCHEME_NAME} -archivePath ${ARCHIVE_PATH}
echo "============Build Archive Success============"
## 导出IPA包
xcodebuild -exportArchive -archivePath $ARCHIVE_PATH -exportPath ${IPA_PATH} -exportOptionsPlist ${EXPORTOPTIONSPLIST_PATH} -quiet || exit
if [ -e ${IPA_PATH}/${SCHEME_NAME}.ipa ];thenecho "============Export IPA SUCCESS============"
elseecho "============Export IPA FAIL============"
fi### 上传到app store
xcrun altool --upload-app -f "${导出的ipa的路径}" -t iOS --apiKey "${apiKey}" --apiIssuer "${apiIssuer}" --verbose || exit
在项目中使用xcodebuild -list
命令可以看到对应的schemes,如下图:
ExportOptions_AppHoc.plist文件:
ExportOptions_AppStore.plist文件:
配置在苹果后台获取,文件所在位置如下图:
其中ad-hoc和app store区别如下:
- ad-hoc:
- 限定配置了设备的UDID的测试设备使用,通过iTunes或配置文件安装
- 不需审核,随时更新
- app-store:
- 所有用户可通过App Store下载安装
- 需通过苹果官方审核
手动修改.pbxproj文件的版本号,不用每次打包都要到xcode界面修改:
# 设置新的版本号
#获取Version,例如: 1.0.0
VERSION_NUMBER=${VERSION_NUMBER}
# 获取build, 例如: 100
BUILD_NUMBER=${BUILD_NUMBER}# 修改MARKETING_VERSION
sed -i "" "s/MARKETING_VERSION = .*/MARKETING_VERSION = ${VERSION_NUMBER};/g" "${PROJECT_PATH}/project.pbxproj"
# 修改CURRENT_PROJECT_VERSION
sed -i "" "s/CURRENT_PROJECT_VERSION = .*/CURRENT_PROJECT_VERSION = ${BUILD_NUMBER};/g" "${PROJECT_PATH}/project.pbxproj"