iOS 通过Jenkins 自动构建ipa
默认Jenkins装好了
环境配置
安装插件:
GitLab Plugin、Gitlab Hook Plugin、Xcode integration和Keychains and Provisioning ProfilesManagement
拷贝文件:(大坑)
1、将/Users/管理员用户名/Library的MobileDevice文件夹拷贝到/Users/Shared/Jenkins/Library下
2、打开钥匙串程序,找到相对应的证书,选择“允许所有的应用程序访问此项目”,并存储修改。
3、将/Users/管理员用户名/Library/Keychains/下的login.keychain及login.keychain-db这两个文件拷贝到/Users/Shared/Jenkins/Library/Keychains文件夹下面
注意:Mac OS 10.12以下的没有login.keychain-db这个文件,只需要拷login.keychain文件。
配置证书及描述文件:
在系统管理页面,选择“Keychains and Provisioning ProfilesManagement”。
进入Keychains and Provisioning ProfilesManagement页面,点击“浏览”按钮,分别上传自己的keychain和证书。上传成功后,我们再为keychain指明签名文件的名称。点击“Add Code Signing Identity”,最后添加成功后如下图所示:
注意:
1、Keychain和ProvisioningProfiles文件是隐藏文件,所以需要拷出来。
Keychain文件路径:/Users/管理员用户名/Library/Keychains/login.keychain,
ProvisioningProfiles文件路径:/Users/管理员用户名/Library/MobileDevice/Provisioning\ Profiles
2、Keychain和ProvisioningProfiles文件选好后,都需要upload下。
配置Kaychain
进入 系统管理->全局配置->Xcode Builder ,添加Keychain。“KeychainPath”路径${HOME}/Library/Keychains/login.keychain。
新建项目
1)点击左侧工具栏"新建",选择第一项"构建一个自由风格的软件项目"
2)General
3)源码管理信息
如果你是用的是Git,那就请选择Git,然后输入你的仓库地址
点击“Add Credentials”添加Credentials。如下图:
如果是svn就输入svn地址,如下图:
4)构建触发器
可选填。
5)构建环境
选择“Keychains andProvisioning Profiles Management”和“Mobile Provisioning Profiles”
注意:Xcode8的自动签名需要改成手动。
6)构建
有两种方式打包,一是用Xcode插件打包,二是用Shell脚本打包。
方式一:
添加构建步骤-> Xcode基本信息
这里Target请于Xcode项目中Target的名字对应
Clean before build设置为YES
Configuration我选择了Release(在Release的时候Archive)
Output directory为.ipa的输出路径,我这里设置为~/Desktop/build/
完成图:
General build settings
Code signing & OS X keychain options
Advanced Xcode build options
1.“Xcode Project File”里填Xcode项目中的schema。
2.本文中的项目中包含有workspace,这里设置绝对路径,不需要带上.xcworkspace后缀。如果项目中没有workspace后缀,那就在“Xcode Project File”上填.xcodeproj文件的路径。
3.“Build output directory”里设置构建输出目录。
方式二:
选择第二个“Executeshell”
Shell脚本:
#工程名
APP_NAME="Demo"
#目标名
TARGET_NAME="Demo"
#证书
CODE_SIGN_DEVELOPER="iPhoneDeveloper: xxx"
#info.plist路径
project_infoplist_path="/Users/管理员用户名/Desktop/Demo/Demo/Demo/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="/Users/管理员用户名/Desktop/ipa/Demo/${IPANAME}"
#workspace路径
WORKSPACE_PATH="/Users/管理员用户名/Desktop/Demo/Demo/Demo.xcworkspace"
echo "+++++++++++++++++clean+++++++++++++++++"
xcodebuild -workspace ${WORKSPACE_PATH} -scheme "${APP_NAME}" -configuration 'Debug' clean
echo "+++++++++++++++++Build+++++++++++++++++"
xcodebuild -workspace ${WORKSPACE_PATH} -scheme "${APP_NAME}" -sdk iphoneos -configuration 'Debug' CODE_SIGN_IDENTITY="${CODE_SIGN_DEVELOPER}" SYMROOT='$(PWD)'
echo "+++++++++++++++++Run+++++++++++++++++"
xcrun -sdk iphoneos PackageApplication -v "./Debug-iphoneos/${APP_NAME}.app" -o ${IPA_PATH}
#userKey和apiKey需要在蒲公英的账号设置中查找
userKey="xxx"
apiKey="xxx"
#蒲公英打包
curl -F "file=@${IPA_PATH}" \
-F "uKey=${userKey}" \
-F "_api_key=${apiKey}" \
-F "isPublishToPublic=2" \
http://www.pgyer.com/apiv1/app/upload
注意:复制粘贴有可能使脚本之间的空格消失,请注意!!!
遇到的坑
坑1:文件拷贝失败
Failed to copy /Users/Shared/Jenkins/Home/kpp_upload/xxx.mobileprovision to /Users/caizhuoyue/Library/MobileDevice/Provisioning Profiles/xxx.mobileprovision
[java.io.IOException](http://stacktrace.jenkins-ci.org/search?query=java.io.IOException): Failed to copy /Users/Shared/Jenkins/Home/kpp_upload/xxx.mobileprovision to /Users/caizhuoyue/Library/MobileDevice/Provisioning Profiles/xxx.mobileprovision
解决方法:开放Library(/Users/管理员用户名/Library)文件夹写入权限
坑2:文件夹中不包含project 或 workspace文件
xcodebuild: error: The directory /Users/Shared/Jenkins/Home/workspace/Demo1 does not contain an Xcode project or workspace.
解决方法:需要在“xcodebuild -workspace /Users/管理员用户名/Desktop/Demo/Demo/Demo.xcworkspace -scheme Demo -configuration Debug clean”脚本中指定路径“-workspace /Users/管理员用户名/Desktop/Demo/Demo/Demo.xcworkspace”
坑3:执行clean脚本后没有下文。
+ xcodebuild -workspace /Users/管理员用户名/Desktop/Demo/Demo/Demo.xcworkspace -scheme Demo -configuration Debug clean
Build was aborted
解决方法:给Shared文件夹(/Users/Shared)开放写入权限
坑4:匹配不到证书
No profile matching '证书' found: Xcode couldn't find a profile matching '证书'. Install the profile (by dragging and dropping it onto Xcode's dock item) or select a different one in the General tab of the target editor.
Code signing is required for product type 'Application' in SDK 'iOS 10.0'
解决方法:将/Users/管理员用户名/Library下的MobileDevice文件夹拷贝到/Users/Shared/Jenkins/Library下面
坑5:自动签名失败
Demo has conflicting provisioning settings. Demo is automatically signed, but code signing identity iPhone Developer: xxx has been manually specified. Set the code signing identity value to "iPhone Developer" in the build settings editor, or switch to manual signing in the project editor.
Code signing is required for product type 'Application' in SDK 'iOS 10.0'
解决方法:在Xcode里将自动签名改成手动签名,并在"TARGETS"->"Build Settings"->"Code Signing Identity"中将所有的team改掉。
8.png 9.png坑6:签名失败
/usr/bin/codesign --force --sign C5FE4334C09900601683D488B17EE6FC1E917F9F --entitlements /Users/Shared/Jenkins/Library/Developer/Xcode/DerivedData/Demo-bzxerumfljpbfaehrejgkxttfmtz/Build/Intermediates/Demo.build/Debug-iphoneos/Demo.build/Demo.app.xcent --timestamp=none /Users/Shared/Jenkins/Home/workspace/Demo/Debug-iphoneos/Demo.app
SecKey API returned: -25308, (null)/Users/Shared/Jenkins/Home/workspace/Demo/Debug-iphoneos/Demo.app: unknown error -1=ffffffffffffffff
Command /usr/bin/codesign failed with exit code 1
解决方法:进入 系统管理->全局配置->Xcode Builder ,添加Keychain。添加完之后打开钥匙串程序,解锁“登录”钥匙串,并找到相对应的证书,选择“允许所有的应用程序访问此项目”,并存储修改。
3.png坑7:导出ipa失败
+ /usr/bin/zip --symlinks --verbose --recurse-paths ~/Users/caizhuoyue/Desktop/ipa/Demo/Demo.ipa/ .
Program /usr/bin/zip returned 15 : [zip I/O error: No such file or directory
zip error: Could not create output file (~/Users/caizhuoyue/Desktop/ipa/Demo/Demo.ipa/.zip)]
解决方法:“~/Users/caizhuoyue/Desktop/ipa/Demo/Demo.ipa/”这个路径错误,正确的路径是“/Users/caizhuoyue/Desktop/ipa/Demo/Demo.ipa/”
坑8:电脑重启后打包失败
/usr/bin/codesign --force --sign C5FE4334C09900601683D488B17EE6FC1E917F9F --entitlements /Users/Shared/Jenkins/Library/Developer/Xcode/DerivedData/Demo-bzxerumfljpbfaehrejgkxttfmtz/Build/Intermediates/Demo.build/Debug-iphoneos/Demo.build/Demo.app.xcent --timestamp=none /Users/Shared/Jenkins/Home/workspace/Demo/Debug-iphoneos/Demo.app
SecKey API returned: -25308, (null)/Users/Shared/Jenkins/Home/workspace/Demo/Debug-iphoneos/Demo.app: unknown error -1=ffffffffffffffff
解决方法:重新拷贝login.keychain和login.keychain-db文件。
参考链接:
http://www.jianshu.com/p/5591fe0c6770(灰常有用)
http://www.cocoachina.com/ios/20160804/17281.html(安装Jenkins)