搭建Jenkins前,请确认mac系统上已经搭建好了Java环境。
从零开始一步一步构建,遇到了很多坑,好在最终success了。
一、 搭建Jenkins
1. 安装Jenkins
从官网上下载pkg安装包
这里以jenkis-2.73.3.pkg包为例
安装完成后,Safari可能会自动打开,如果没有自动打开,打开浏览器,输入http://localhost:8080
如果页面提示不能连接服务器的错误,重启电脑。然后再输入http://localhost:8080进入登录页面。
根据提示,找到/Users/Shared/Jenkins/Home/ 这个目录,找到secrets文件,显示简介,设置所有人都可以读与写的权限,并找到initialAdminPassword文件,同样放开权限,复制出密码,登录进去,一路安装过来,输入用户名、密码、邮件等。最后点击Save and Finish。
注意:如果安装之后发现密码忘记了,从initialAdminPassword文件中复制输入也错误,试了很多方法也不管用,那就卸载重装Jenkis然后重启电脑吧。
2. 拷贝文件
2.1
将/Users/用户名/Library的MobileDevice文件夹拷贝到/Users/Shared/Jenkins/Library下
注意:是将MobileDevice这个文件夹拷贝过去,而不是MobileDevice里的文件,/Users/Shared/Jenkins/Library 目录下是没有MobileDevice这个文件夹,所以先创建一个MobileDevice文件夹。
在终端输入:
sudo mkdir /Users/Shared/Jenkins/Library/MobileDevice
sudo cp -r /Users/xxx/Library/MobileDevice/ /Users/Shared/Jenkins/Library/MobileDevice/
注意:曾遇到/Users/xxx/Library/下没有MobileDevice文件夹,原来是新机上没有安装Xcode。
2.2
将/Users/用户名/Library/Keychains/下的login.keychain及login.keychain-db这两个文件拷贝到/Users/Shared/Jenkins/Library/Keychains文件夹下面
注意:Mac OS 10.12以下的没有login.keychain-db这个文件,只需要拷贝login.keychain文件。
在终端输入:
cd /Users/Shared/Jenkins/Library/
sudo mkdir Keychains
sudo cp /Users/xxx/Library/Keychains/login.keychain ./Keychains/
sudo cp /Users/xxx/Library/Keychains/login.keychain-db ./Keychains/
再将这个login.keychain拷贝到桌面上
在终端输入:
sudo cp /Users/jiekangwei/Library/Keychains/login.keychain ~/Desktop/
注意:如果/Users/xxx/Library/Keychains/目录下没有login.keychain这个文件,就复制login.keychain-db文件到指定目录,然后将复制在桌面上的该文件改后缀为.keychain。
3. 钥匙串权限、~/Library权限以及电脑用户管理权限
3.1
打开钥匙串,找到相应的证书
右键密钥显示简介,在访问控制那一项里勾选允许所有应用程序访问此项目,并存储更改。
3.2
找到Library资源库文件夹,设置共享与访问权限,everyone读与写都可以
3.3
进入mac系统偏好设置 — 用户与群组 — 其他用户 — jenkins ,勾选允许用户管理这台电脑,并右键选择高级选项,将名字改为jenkins
4. Jenkins插件及系统设置
进入管理插件,在可选插件里所有关键字,下载安装插件Keychains and Provisioning ProfilesManagement
和 Xcode integration
4.1
选取拷 在桌上login.keychain以及项 相对应的Provisioning Profile 并upload
注意:开发证书和provision profile 一定要对应正确,不然命令行打包会认证不了而失败
password 一定要填写正确的钥匙串的密码
Code Signing Identity 是从钥匙 找到相应的证书,然后显示简介复制 来,这 Identities 少要填 两项,是因为这 放 个的话后 的设置 Code Signing Identity 能 动对应上。
Provisioning Profiles Directory Path 这 项 填写/Users/用户名/Library/ MobileDevice/Provisioning Profiles
注意:路径里Provisioning Profiles是Provisioning\ Profiles
点击Save
4.2
安装完Xcode integration后进 系统设置。
Xcode Builder
填写钥匙串密码
5. 新建
5.1
填写名字,选择构建自由风格,点击ok
进入Project,进入配置
5.2
General
项目名称,这里可以和git项目名称一样,也可以另起一个名称。
5.3
源码管理
选择git,然后在Repository URL 上填写git仓库地址
选择add
选择SSH Username with private key
Username 为上传到git的用户名,Private Key从id_rsa文件里复制出来
5.4
构建触发器,可选填
5.5
构建环境
选择Keychains andProvisioning Profiles Management
和Mobile Provisioning Profiles
Code Signing Identity 第一次不能自动显示出来没关系,保存,然后第二次进来就自动选上了。
5.6
构建
有两种打包方式,一是用Xcode打包,而是用Shell脚本打包,当Xcode版本为9或者以上,应该使用Shell脚本打包。
因为 Xcode 9 默认不允许访问钥匙串的内容,必须要设置 allowProvisioningUpdates 才会允许,但是由于 Xcode integration 插件封闭,并不能对其进行修改加上这个属性,所以使用 Shell 脚本代替插件。
5.6.1
这里先介绍Xcode打包,项目使用的是Xcode 8.1
General build settings
这里Target请于Xcode项目中Target的名字对应
Clean before build设置为YES
Configuration选择Release)
Output directory为.ipa的输出路径,可选填
Code signing & OS X keychain options
teamId 为对应证书括号里面有,如果没找到,请在苹果开发网站登陆进去,从Membership上查看。选择Unlock Keychain,填写钥匙串密码
Advanced Xcode build options
Xcode Schema Files 填写Xcode项目的schema
因为项目含有Workspace,所以填写Xcode Workspace File,填写该项目xcworkspace文件的绝对路径
Build output directory 填写${WORKSPACE}/build/ ,这里注意是大括号
打开工程,mange schemes,勾选项目schemes对应的shared,该scheme为填写的Xcode Schema Files的内容
ok,点击保存,使用Xcode构建配置完毕
5.6.2
选择Execute shell 打包
command 写入脚本:
xcodebuild clean -workspace 工程名称.xcworkspace -scheme 工程名称 -configuration Release
xcodebuild archive -workspace 工程名称.xcworkspace -scheme 工程名称 -archivePath 工程名称.xcarchive
xcodebuild -exportArchive -archivePath 工程名称.xcarchive -exportOptionsPlist /Users/用户名/ExportOptions.plist -allowProvisioningUpdates -exportPath ./
ExportOptions.plist 注意大小写,先打包工程,将ExportOptions.plist文件拷贝到/Users/用户名/或者指定的目录,记得脚本中/Users/用户名/ExportOptions.plist 对应的是该文件的路径。
Next ,然后Export
6. 上传
这里把项目上传到fir.im平台为例
之前选择使用命令行
但遗憾的是用终端安装fir-cli总是失败,于是就选择使用fir.im插件
这里选择使用fir.im插件, 进入 官网下载,下载jenkins插件
然后进入Jenkins,选择插件管理 — 高级
选择下载好的fir-plugin-1.9.5.hpi,上传并安装完后
进入项目的配置页面,在构建后操作里选择Upload to fir.im,填写fir.im token
好了,点击保存,立即构建吧
如果构建时后台打印出以下信息,恭喜你,你构建成功了
二、配置Jenkins错误
1、 拷贝文件错误
Failed to copy /Users/Shared/Jenkins/Home/kpp_upload/xxxx.mobileprovision to /Users/xxx/Library/MobileDevice/Provisioning Profiles/xxxx.mobileprovision
解决:文件拷贝失败。1.查看Library读写权限是否开放;2. 查看系统管理 — Keychains and Provisioning Profiles Management,查看Provisioning Profiles Directory path 是否有误
2、 证书文件找不到
Check dependencies Code Sign error: No matching provisioning profile found: Your build settings specify a provisioning profile with the UUID “75025357-245d-448b-9343-96148a0xxxxx”, however, no such provisioning profile was found.
解决:将/Users/管理员用户名/Library下的MobileDevice文件夹拷贝到/Users/Shared/Jenkins/Library下面
security: SecKeychainUnlock login.keychain: The user name or passphrase you entered is not correct. FATAL: Unable to unlock the keychain.
解决:确认钥匙串密码是正确的
4、 pod没有权限
/Users/xxx/workspace/xxxx/Pods/Target Support Files/Pods-xxxx/Pods-xxxx-resources.sh: line 7: /Users/xxx/workspace/xxxx/Pods/resources-to-copy-DiscoverPanjin.txt: Permission denied
解决:在终端打开项目目录并输入 sudo chmod 777 Pods
5、 使用fir插件上传失败
ERROR: Can not found mapping or dysm file in /Users/Shared/Jenkins/Home/jobs/xxxx/workspace. Deployment failed : Error while get details for update id = xxxxx Build step 'Upload to fir.im' marked build as failure
解决:fir插件版本过低,使用最新fir插件试试
5、使用命令行上传失败
fir p/Users/Shared/Jenkins/Home/jobs/xxx/workspace/build/xxx.ipa -T 4dc6d4c3ebe1969b3c76353514xxxxx /Users/Shared/Jenkins/tmp/jenkins3233517671203652013.sh: line 2: fir: command not found Build step 'Execute shell' marked build as failure
解决:没有安装fir-cli命令
6、
Will not set default: file /Users/Shared/Jenkins/Library/Keychains/login.keychain-db is owned by UID=0, but we have UID=262 security: SecKeychainSetDomainDefault user: write permissions error
解决:进入mac 系统偏好设置 — 用户与群组 — 其他用户 — jenkins ,勾选允许用户管理这台电脑
7、
FATAL: Failed to mkdirs: /Users/xxx/Desktop/ipa java.io.IOException: Failed to mkdirs: /Users/xxx/Desktop/ipa
解决:Output directory 路径错误
8、
FATAL: Build directory does not exist at /$(WORKSPACE)/build. Potential configuration issue. Build step 'Xcode' marked build as failure
解决:Build output directory 填写错误,正确为${WORKSPACE}/build
9、
== Available schemes [workspace] $ /usr/bin/xcodebuild -list -workspace /Users/xxx/workspace/xxxx.xcworkspace xcodebuild: error: '/Users/xxx/workspace/xxxx.xcworkspace' does not exist.
解决:Xcode Workspace File 路径错误
10、
rror Domain=IDEProfileLocatorErrorDomain Code=1 "No profiles for 'xxxxxxx' were found" UserInfo={NSLocalizedDescription=No profiles for 'xxxxxxx' were found, NSLocalizedRecoverySuggestion=Xcode couldn't find any iOS Ad Hoc provisioning profiles matching 'xxxxxxx'. Automatic signing is disabled and unable to generate a profile. To enable automatic signing, pass -allowProvisioningUpdates to xcodebuild.
解决:打包工程导出ipa时选择手动选择证书
11、
+ xcodebuild -exportArchive -archivePath /xxx.xcarchive -exportPath -exportOptionsPlist /Users/xxx/exportOptionsPlist.plist
xcodebuild: error: The flag -exportOptionsPlist is required when specifying -exportArchive.
Build step 'Execute shell' marked build as failure
解决:拷贝到指定路径的ExportOptions.plist 名称大小写错误
12、
create and update profiles, app IDs, and certificates. For manually signed targets, xcodebuild will download missing or updated provisioning profiles. Requires a developer account to have been added in Xcode's Accounts preference pane. -allowProvisioningDeviceRegistration Allow xcodebuild to register your destination device on the developer portal if necessary. This flag only takes effect if -allowProvisioningUpdates is also passed. Build step 'Xcode' marked build as failure
解决:脚本上缺少allowProvisioningUpdates这一句
13、
$ /usr/bin/xcodebuild -scheme xxxx -workspace /Users/用户名/workSpace/xxxx/xxx.xcworkspace -configuration Release clean archive -archivePath "/Users/Shared/Jenkins/Home/workspace/xxx/build/xxxx.xcarchive" "BUILD_DIR=/Users/Shared/Jenkins/Home/workspace/xxx/build/" DEVELOPMENT_TEAM=EFL2T3NT3W -exportArchive -exportPath /Users/Shared/Jenkins/Home/workspace/xxx/build/ -exportOptionsPlist /Users/xxx/options.plist
xcodebuild: error: The flag -scheme cannot be specified along with -exportArchive.
或者
[iOS Panjin] $ /bin/sh -xe /Users/Shared/Jenkins/tmp/jenkins410183735701877848.sh
+ echo '================= Clean一下 ================='
================= Clean一下 =================
+ xcodebuild clean -workspace .xcworkspace -configuration -target
xcodebuild: error: If you specify a workspace then you must also specify a scheme. Use -list to see the schemes in this workspace.
Build step 'Execute shell' marked build as failure
Build step 'Xcode' marked build as failure
或者:
[iOS xxx] $ /bin/sh -xe /Users/Shared/Jenkins/tmp/jenkins8239787762841783385.sh
+ xcodebuild clean -workspace .xcworkspace -scheme -configuration
xcodebuild: error: '.xcworkspace' does not exist.
Build step 'Execute shell' marked build as failure
或者:
+ xcodebuild -exportArchive -archivePath /xxx.xcarchive -exportOptionsPlist /Users/xxx/ExportOptions.plist -allowProvisioningUpdates -exportPath ./
error: archive not found at path '/xxx.xcarchive'
Build step 'Execute shell' marked build as failure
解决:用脚本构建时,脚本输写有误
14、
+ xcodebuild -exportArchive -archivePath /DiscoverPanjin.xcarchive -exportOptionsPlist /Users/xunlusheji/exportOptionsPlist.plist -allowProvisioningUpdates -exportPath ./
error: Couldn't load -exportOptionsPlist: The file “exportOptionsPlist.plist” couldn’t be opened because there is no such file.
Error Domain=NSCocoaErrorDomain Code=260 "The file “exportOptionsPlist.plist” couldn’t be opened because there is no such file." UserInfo={NSFilePath=/Users/xxxx/exportOptionsPlist.plist, NSUnderlyingError=0x7fd745d91140 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
** EXPORT FAILED **
Build step 'Execute shell' marked build as failure
解决:缺少ExportOptions.plist
15、
+ xcodebuild -exportArchive -archivePath xxxx.xcarchive -exportOptionsPlist /Users/xxx/ExportOptions.plist -allowProvisioningUpdates -exportPath ./
error: archive not found at path '/Users/Shared/Jenkins/Home/workspace/xxx/xxx.xcarchive'
** EXPORT FAILED **
Build step 'Execute shell' marked build as failure
解决: /Users/Shared/Jenkins/Home/workspace/ 下.xcarchive 文件名称有误,或者.xcarchive文件路径有误
16、
Error Domain=IDECodesignResolverErrorDomain Code=1 "No certificate for team 'xxxxx' matching 'DB8C944F71D475DE773696BD637xxxxxxx' found" UserInfo={IDEProvisioningError_UserInfoKey_IDEProvisioningErrorSpecifier=DB8C944F71D475DE773696BD6374C5xxxxx, NSLocalizedDescription=No certificate for team 'xxxxx' matching 'DB8C944F71D475DE773696BD6374C5BBxxxxxxx' found, NSLocalizedRecoverySuggestion=Specify a different signing certificate for "signingCertificate" in your Export Options property list., IDEProvisioningError_UserInfoKey_IDEProvisioningErrorTeam=, IDEProvisioningError_UserInfoKey_IDEProvisioningErrorPlatform=com.apple.platform.iphoneos, IDEProvisioningError_UserInfoKey_IDEProvisioningErrorAction=5}
解决:keychains下的provisioning profile 对应不上 certificate证书,把钥匙串相应的证书删掉,重新从苹果商店下载证书和provisioning profile,在Jenkis —系统设置— Keychains and Provisioning Profiles Management插件里全部删除,执行前文 2、拷贝文件 操作,然后upload新的login.keychain和provisioning profile