參考鏈接iOS持續集成:Jenkins+GitLab+蒲公英 講了幾種自動化打包的配置方式、很詳細!
下面主要是針對使用xcodebuild + xcrun編寫腳本配置的方式做一個踩坑記錄,我就是用的這個。
一、在全局配置中選擇Keychains and Provisioning Profiles Management
二、進入到創建的項目下
(1)源碼管理
(2)構建觸發器+構建環境
(3)構建
(4)構建後操作(存到ftp服務)
三、xcodebuild + xcrun 腳本
# 工程名/Target/Schema
APP_NAME="test"
# 項目絕對路徑
PROJECT_PWD_PATH="$PWD/ios/test.xcodeproj"
# build 路徑
BUILD_PATH="$PWD/ios/build/Release-iphoneos"
# configuration
CONFIGURATION_TYPE="Release"
# info.plist路徑
INFO_PLIST_PATH="$PWD/ios/test/Info.plist"
# 簽名
CODE_SIGN_NAME="iPhone Distribution: xxxxxx Net Co.,Ltd"
# 描述文件
MOBILE_PROVISION="883xxxxxx0-xxxxx-xxxxx-f4d.mobileprovision"
# 取版本號
bundleShortVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleShortVersionString" "${INFO_PLIST_PATH}")
# 取build值
bundleVersion=$(/usr/libexec/PlistBuddy -c "print CFBundleVersion" "${INFO_PLIST_PATH}")
# 日期
DATE="$(date +%Y%m%d)"
# 生成.ipa名稱
IPANAME="${APP_NAME}_V${bundleShortVersion}_${DATE}.ipa"
# .ipa路徑
IPA_PATH="$PWD/ios/build/${IPANAME}"
# remove Build_Directory
rm -rf "$PWD/ios/build"
# clean
xcodebuild clean -project ${PROJECT_PWD_PATH} -configuration ${CONFIGURATION_TYPE} -target ${APP_NAME} -quiet || exit
# build
xcodebuild -project ${PROJECT_PWD_PATH} -configuration ${CONFIGURATION_TYPE} -scheme ${APP_NAME} -sdk iphoneos11.0 -arch arm64 CONFIGURATION_BUILD_DIR=${BUILD_PATH} -quiet || exit
# export
xcrun -sdk iphoneos11.0 PackageApplication -v "${BUILD_PATH}/${APP_NAME}.app" -o ${IPA_PATH} CODE_SIGN_IDENTITY ${CODE_SIGN_NAME} --embed ${MOBILE_PROVISION} || exit
# open ${IPA_PATH}
# export_plist
# EXPORT_PLIST="~/Desktop/ExportOptions_temp.plist"
# .xcarchive路徑
#XCARCHIVE_PATH="$PWD/ios/build/CaibaClient.xcarchive"
#xcodebuild archive -project ${PROJECT_PWD_PATH} -scheme ${APP_NAME} -configuration Release -archivePath "${XCARCHIVE_PATH}" -quiet || exit
#xcodebuild -exportArchive -archivePath "${XCARCHIVE_PATH}" -exportPath ${IPA_PATH} -exportOptionsPlist ${EXPORT_PLIST} -quiet || exit
修改上面的腳本就可以使用,主要是修改相關的路徑和工程名。
其中:
1. target和schema可以在項目路徑下執行命令xcodebuild -list查看
2.在項目路徑下執行命令xcodebuild -showsdks查看支持的sdk參數
3.此處是針對.xcodeproj項目,如果是使用的cocoapods集成的項目,需執行pod install,並且工程名不再是.xcodeproj而是.xcworkspace,具體可參考iOS持續集成:Jenkins+GitLab+蒲公英
4.上面的腳本可以改成後綴名爲.sh的文件xcodebuild.sh置於*.xcodeproj同級目錄,在該目錄下使用命令./xcodebuild.sh即可運行腳本自動打包,如無權限執行命令chmod +x xcodebuild.sh
5.在上面export命令中使用了PackageApplication ,它是在舊版的xcode中存在,用來將.app文件轉成.ipa,但是在Xcode 9中已經廢棄了這個。
一種解決方式是使用新的腳本命令-exportArchive導出.ipa
另一種方式就是拷貝這個文件到新的Xcode目錄下。 文件下載鏈接-----PackageApplication Download 下載後放到指定目錄下並執行命令即可
# 放到指定目錄
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/
# 終端執行下面兩條命令
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer/
chmod +x /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/PackageApplication
6.如果使用Xcode工具進行構建,需要的team name 和 team id 可登陸 Developer 網站查看