@[TOC](iOS利用Jenkins+Mac+gitLab+fir/蒲公英 快速打包)
原文地址:https://www.jianshu.com/p/484b038712b9
- 简书没有标题目录啊,醉了
一、下载安装
- 笔者直接使用
brew install Jenkins
完成安装 - 然后输入
Jenkins
启动
二、访问Jenkins
- 在浏览器打开地址:http://localhost:8080/
- 使用安装包安装后会自动打开,如果端口冲突那么请修改端口
defaults write /Library/Preferences/org.jenkins-ci httpPort xxxx
,其中xxxx
为自定义端口数字,此时打开地址为http://localhost:xxxx/
三、添加插件
- 若选则
自定义安装
插件,并且注册成功
登陆后 -
系统管理
>>插件管理
>>Available
将Xcode/gitLab
相关的插件全部install
加入 - 若
Available
中看不到
可选则的插件,- 则
系统管理
>>插件管理
>>Advanced
(在Available
后面第二个) - 在这个界面拉到底部会看到
Update Site
标题下的URL
, - 将
https://updates.jenkins.io/update-center.json
去掉s
变为http://updates.jenkins.io/update-center.json
- 然后回到
Available
添加插件
- 则
四、gitLab设置
-
首页
系统管理
>>系统设置
>>下拉找到GitLab
-
添加
GitLab API token
-
登录自己的gitLab,copy APIToken
五、创建新Item任务
-
回到首页,点击
新任务
-
General
-
SourceCodeManagement
- 跳过
Build Trigger
,Build Environment
-
Build
选中Execute Shell
,若没有就找执行 shell
,写自定义打包脚本
六、Item打包设置
-
观察自己GitLab要打包的项目结构[先尝试本地打包,成功后将其中的
ExportOptions.plist
取出使用]
-
看图理解path参数
- shell代码
#要打包的项目地址,若使用了cocoapods则"test1110/test1110.xcworkspace"
ProjectPath="test1110/test1110.xcodeproj"
#项目中要打包的scheme
TargetScheme="test1110"
#生成的中转文件xcarchive存放地址
ArchivePath="test1110.xcarchive"
#打包使用的配置文件地址
ExportOptionsPlistPath="ExportOptions.plist"
#打包IPA的存放文件,这个名字随便自定义,注意别跟其他path重复
ExportPath="XCBuildIPA"
xcodebuild clean -project ${ProjectPath} \
-scheme ${TargetScheme} \
xcodebuild archive -project ${ProjectPath} \
-sdk iphoneos \
-scheme ${TargetScheme} \
-archivePath ${ArchivePath} \
-configuration Release \
echo "+++++++++++++++++ exportArchive +++++++++++++++++"
xcodebuild -exportArchive -archivePath ${ArchivePath}\
-exportPath ${ExportPath}\
-exportOptionsPlist ${ExportOptionsPlistPath}\
-configuration Release \
七、带参数(tag、branch)的打包
- 安装插件
系统管理
>>插件管理
>>Available
搜索Git Parameter Plug-In
,下载重启 - 来到item的设置界面
General
-
此时打包变成了
八、打包完成自动上传fir
可以直接在脚本后加入上传代码,但是这要求Mac安装了fir—cli。所以还是建议用插件
下载插件到本地 fir插件:fir-plugin-1.9.5.hpi
-
系统管理
>>插件管理
>>Advanced
-
重启后回到项目设置界面
九、钉钉机器人提醒
https://oapi.dingtalk.com/robot/send?access_token=e9。。。41
- 注意只使用
access_token=
后面的字符串即可 -
系统管理
>>插件管理
>>Available
搜ding
得到的插件更新 -
重启后回到项目设置界面
十、自定义描述界面
description支持html:
系统管理
>>全局安全配置
>>Markup Formatter
>>Safe html
增加插件
系统管理
>>插件管理
>>Available
搜description setter plugin
得到的插件更新-
重启后回到项目设置界面
-
打包成功后点击链接可以直接跳转下载,从钉钉点击跳转过来也能看到这个下载链接
十一、邮件通知
Jenkins自带的
-
通过插件扩展
-
系统管理
>>插件管理
>>Available
搜Email Extension Plugin
得到的插件更新 -
系统管理
>>系统设置
>>Extended E-mail Notification
将图中标红处修改成自己使用的邮箱
-
-
配置项目
十二、配置奴隶Mac机实现自动打包:参考
-
1.配置奴隶Mac机: 打开slave mac机,在
系统偏好设置
>>共享
,如图 -
2.创建节点:
系统管理
>>节点管理
>>NewNode
-
3.配置节点:
-
4.修改项目配置
重要提示:
- Jenkins,ssh方式到slave机上,默认是
没有账户
的,但是访问钥匙串要求必须有用户身份,所以添加一步输入密码解锁钥匙串,可以给Jenkins一个用户身份。
build步骤前添加一步解锁钥匙串。
security unlock-keychain -p "login pwd" ~/Library/Keychains/login.keychain
十三、自动配置项目打包模式
- 问提:在不同的branch或者tag上,可能有的的是“autoSign”自动签名模式,有的是手动模式,而项目的的打包需要手动签名模式;如果每次自动打包都要检查一下xcode工程的配置,那也太不自动了
- 分析:Jenkins的工作原理是 将GitLab远程库中的代码拉取到 Jenkins工作空间
workspace
中,然后在workspace中与打包机进行交互实现打包;而iOS项目中有关签名配置的相关数据都在某项目.xcodeproj/project.pbxproj
文件中,那么我们是否可以 修改 文件,来达到统一配置呢?
- 此方案是可行的,
脚本替换文件内容
十四、适配Xcode10 的BuildSystem
-
众所周知,在Xcode9升10后,项目运行会失败,报info.plist编译错误此时有两种处理方式
- 更改Xcode配置:
In Xcode, go to File -> Project/Workspace settings. -> Build System -> Legacy Build System
- 更改项目配置:选中项目的
Target
->Build Phases
->搜索info.plist->CopyBundleSourses中删除info.plist
即可
- 更改Xcode配置:
新branch我们可以通过方法2来处理,这样打包不会有问题,但是以前的老分支呢?也要去改么?这样不符合一个合格码农的习惯,其实主要采取方法1即可。
第三行代码 -UseModernBuildSystem=NO 的功能与方法1相同
echo "+++++++++++++++++ 清理工程 +++++++++++++++++"
xcodebuild clean -project ${ProjectPath} \
-scheme ${TargetScheme} \
-UseModernBuildSystem=NO \
总结
- 其中 12的重要提示,13,14条由于是原创做法,所以花费的精力比较大
- 最终脚本如下
#要打包的项目地址
ProjectPath="OA/CRM.xcodeproj"
#项目中要打包的scheme
TargetScheme="CRM"
#生成的中转文件xcarchive存放地址
ArchivePath="CRM.xcarchive"
#打包使用的配置文件地址,这个是存放在slave机上。
ExportOptionsPlistPath="/Users/macserver/Desktop/ExportOptions.plist"
#打包IPA的存放文件
ExportPath="XCBuildIPA"
#肉鸡的登录密码
SlaveMacPWD="macserver"
#项目配置文件地址
ProjectSettingFilePath="OA/CRM.xcodeproj/project.pbxproj"
echo "+++++++++++++++++ 若项目是自动签名,则修改配置为手动Release签名 +++++++++++++++++"
sed -i "" "s#ProvisioningStyle = Automatic#ProvisioningStyle = Manual#g" ${ProjectSettingFilePath}
sed -i "" "s#CODE_SIGN_STYLE = Automatic#CODE_SIGN_STYLE = Manual#g" ${ProjectSettingFilePath}
sed -i "" "s#PROVISIONING_PROFILE = \"\"#PROVISIONING_PROFILE = \"dc28777c-0cde-4bb2-b9bf-6c6a4c55aa76\"#g" ${ProjectSettingFilePath}
sed -i "" "s#PROVISIONING_PROFILE_SPECIFIER = \"\"#PROVISIONING_PROFILE_SPECIFIER = \"Xueersi.WangXiao.CRM(Dis)\"#g" ${ProjectSettingFilePath}
sed -i "" "s#DEVELOPMENT_TEAM =.*#DEVELOPMENT_TEAM = 6BUND8EFF7;#g" ${ProjectSettingFilePath}
sed -i "" "s#DevelopmentTeam =.*#DevelopmentTeam = 6BUND8EFF7;#g" ${ProjectSettingFilePath}
#若项目bundle id不是com.Xueersi.CRM(target项目的bundle id)那么别替换了,打包失败很正常
#sed -i "" "s#PRODUCT_BUNDLE_IDENTIFIER =.*#PRODUCT_BUNDLE_IDENTIFIER = com.Xueersi.CRM;#g" ${ProjectSettingFilePath}
echo "+++++++++++++++++ 解锁slave机的钥匙串权限 +++++++++++++++++"
security unlock-keychain -p ${SlaveMacPWD} ~/Library/Keychains/login.keychain
echo "+++++++++++++++++ 清理工程 +++++++++++++++++"
xcodebuild clean -project ${ProjectPath} \
-scheme ${TargetScheme} \
-UseModernBuildSystem=NO \
echo "+++++++++++++++++ 开始打包 +++++++++++++++++"
xcodebuild archive -project ${ProjectPath} \
-sdk iphoneos \
-scheme ${TargetScheme} \
-archivePath ${ArchivePath} \
-configuration Release \
-UseModernBuildSystem=NO \
echo "+++++++++++++++++ 导出打包 +++++++++++++++++"
xcodebuild -exportArchive -archivePath ${ArchivePath}\
-exportPath ${ExportPath}\
-exportOptionsPlist ${ExportOptionsPlistPath}\
-configuration Release \
echo "+++++++++++++++++ 上传蒲公英 +++++++++++++++++"
#蒲公英账号对应的userkey与apikey,只需替换这俩参数即可切换上传地址
PGYUserKey="蒲公英账号对应的userkey(这里我手动打码)"
PGYAPIKey="蒲公英账号对应的apikey(这里也打码)"
curl -F "file=@${ExportPath}/CRM.ipa" -F "uKey=${PGYUserKey}" -F "_api_key=${PGYAPIKey}" https://qiniu-storage.pgyer.com/apiv1/app/upload
附:Jenkins 自动打包问题
一、Command /Applications/Xcode.app/Contents/Developer/usr/bin/actool failed with exit code 255
failed content
The following build commands failed: CompileAssetCatalog /Users/macserver/Library/Developer/Xcode/DerivedData/CRM-aghqpbaaoycsdgddnnenvhgeuxjo/Build/Intermediates.noindex/ArchiveIntermediates/CRM/InstallationBuildProductsLocation/Applications/CRM.app CRM/Assets.xcassets
参考答案1:link
That happened because of JPG images in assets catalog, so I imported jpg images directly in the project, and now it works
I had the same issue with Xcode V7b6 and JPG images in assets. Strangely this was working for some JPGs but not for others. Then I've looked at the JPGs and their color profile, exif data and other meta stuff and indeed removing these data (via GraphicConverter) solved it for us and Xcode now builds fine.
Pdf files in image assets will cause this error. I use png files for @1x @2x @3x images and Xcode works fine.
This error happened with me because the image name contained some unsupported characters (it was saved by Xcode as c71a25e%2FÍcone-XPTO Imagem - 120x120.png, even when placed in .xcassets). Changing the image name solved this error for me.
二、Command /usr/bin/codesign failed with exit code 1:errSecInternalComponent
failed content
/Users/macserver/Library/Developer/Xcode/DerivedData/CRM-aghqpbaaoycsdgddnnenvhgeuxjo/Build/Intermediates.noindex/ArchiveIntermediates/CRM/InstallationBuildProductsLocation/Applications/CRM.app: errSecInternalComponent
参考答案 链接
- 大部分答案都告诉你
重启机器
- 根本原因是Jenkins,ssh方式到slave机上,默认是没有账户的,但是访问钥匙串要求必须有用户身份,所以添加一步输入密码解锁钥匙串,可以给Jenkins一个用户身份。
build步骤前添加一步解锁钥匙串。
security unlock-keychain -p "login pwd" ~/Library/Keychains/login.keychain
三、自动手动冲突问题
failed content
Check dependencies Code Signing Error: CRM has conflicting provisioning settings. CRM is automatically signed for development, but a conflicting code signing identity iPhone Distribution 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.
修复思路,文件内容替换
要替换的文件是项目的配置文件
OA/CRM.xcodeproj/project.pbxproj
脚本语法
sed -i "" "s#被替换的旧内容#替换成的内容#g" 被替换内容的文件的Path
比如:
sed -i "" "s#ProvisioningStyle = Automatic#ProvisioningStyle = Manual#g" OA/CRM.xcodeproj/project.pbxproj
被替换的旧内容:ProvisioningStyle = Automatic
替换成的内容:ProvisioningStyle = Manual
被替换内容的文件的Path:OA/CRM.xcodeproj/project.pbxproj
- ProvisioningStyle
ProvisioningStyle = Automatic;
替换为
ProvisioningStyle = Manual;
- CODE_SIGN_STYLE
CODE_SIGN_STYLE = Automatic
替换为
CODE_SIGN_STYLE = Manual
从Automatic
到Manual
修改完成后
iPhoneConnect: ## Unable to mount developer disk image, (Error Domain=com.apple.dtdevicekit Code=601 "Could not locate device support files." UserInfo={DeviceType=iPhone7,1, NSLocalizedDescription=Could not locate device support files., NSLocalizedRecoverySuggestion=This iPhone 6 Plus is running iOS 12.0 (16A366), which may not be supported by this version of Xcode.}) { DeviceType = "iPhone7,1"; NSLocalizedDescription = "Could not locate device support files."; NSLocalizedRecoverySuggestion = "This iPhone 6 Plus is running iOS 12.0 (16A366), which may not be supported by this version of Xcode."; } Code Signing Error: "CRM" requires a provisioning profile with the Push Notifications feature. Select a provisioning profile for the "Release" build configuration in the project editor.
- 经过检查
PROVISIONING_PROFILE = "";
PROVISIONING_PROFILE_SPECIFIER = "";
需要替换为
PROVISIONING_PROFILE = "dc28777c-0cde-4bb2-b9bf-6c6a4c55aa76";
PROVISIONING_PROFILE_SPECIFIER = "Xueersi.WangXiao.CRM(Dis)";
现在跑3.14,3.15. 3.13 正常了,然后到3.12时候又报错
Code Signing Error: Signing for "CRM" requires a development team. Select a development team in the build settings editor that matches the selected profile "Xueersi.WangXiao.CRM(Dis)".
明显是没有development team
经过检查
DEVELOPMENT_TEAM = ""
需要替换为DEVELOPMENT_TEAM = 6BUND8EFF7
DevelopmentTeam = 246724P5ZQ;
需要替换为DevelopmentTeam = 6BUND8EFF7;
PRODUCT_BUNDLE_IDENTIFIER = com.Xueersi.CRM022;
>>PRODUCT_BUNDLE_IDENTIFIER = com.Xueersi.CRM;