本文主要讲解如何使用开源社区的一个CI工具Jenkins来搭建iOS的的CI环境
1.下载并运行Jenkins
在Mac环境下,我们需要先安装JDK,然后在Jenkins的官网.下载最新的war包。
下载完成后,打开终端,进入到war包所在目录,执行以下命令:
java -jar jenkins.war --httpPort=8080
进行安装



httpPort指的就是Jenkins所使用的http端口,这里指定8080,可根据具体情况来修改。待Jenkins启动后,在浏览器页面输入以下地址:
http://localhost: 8080
安装完成之后重新打开[http://localhost: 8080](http://localhost: 8080) 此时需要用户输入密码

该密码文件在路径 /Users/Shared/Jenkins/Home/secrets
下无法直接打开 可以终端输入
sudo cat /Users/Shared/Jenkins/Home/secrets/initialAdminPassword
获取内容
2.Jenkins自动配置安装


设置用户名密码

3.安装插件
设置完毕后进入[http://localhost: 8080](http://localhost: 8080) 再配置一些我们所需要的插件

通过过滤搜索,我们安装几个后续需要使用的插件(可根据自己实际需要安装其他的辅助插件)
- GitLab Plugin
- Gitlab Hook Plugin
- Xcode integration
- Keychains and Provisioning Profiles Management
- Credentials Plugin

4.配置SSH与KeyChain
添加SSH.
在Jenkins管理页面,选择“Credentials”,然后选择“Global credentials”,点击“Add Credentials”,进行添加SSH.

Private Key的获取是在路径 /Users/管理员用户名/.ssh
里面,可以通过终端输入 open ~/.ssh
打开文件夹,找到id_rsa (注意不是id_rsa.pub
) 将里面内容全部复制 填写时
开头是-----BEGIN RSA PRIVATE KEY-----
结尾是-----END RSA PRIVATE KEY-----
iOS打包需要签名文件和证书
在系统管理页面,选择刚刚安装好的插件“Keychains and Provisioning Profiles Management”。

上传keychain
先将钥匙串登录里面的相关开发者证书复制到系统目录下

然后

点击选取文件 选择路径
/Users/管理员用户名/Library/keychains/login.keychain
然后点击Upload
Code Signing Identity 填写复制的内容

然后再点击选取文件 选择路径/Users/管理员用户名/Library/MobileDevice/Provisioning Profiles
下自己项目所需要的配置文件
成功后如下所示

理论上Jenkins会把这个Keychain拷贝到/Users/Shared/Jenkins/Library/keychains
这里,(Library是隐藏文件)。Provisioning Profiles文件也直接拷贝到/Users/Shared/Jenkins/Library/MobileDevice
文件目录下。
执行上述操作后查看是否有路径Users/Shared/Jenkins/Library/keychains
与Users/Shared/Jenkins/Library/MobileDevice
如果不存在这两个文件夹,自己先创建两条路径 然后将login.keychain与login.keychain-db和配置文件夹复制到相应的文件夹内
否则容易出现问题:
No profile matching 'getgetsetset5CommonTest' found: Xcode couldn't find a profile matching 'getgetsetset5CommonTest'. 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
即 Jenkins 在指定的目录下没有找到对应的profile文件
解决方案:
/Users/Shared/Jenkins/Library/MobileDevice/Provisioning Profiles 看看这个目录下面,有没有对应的profile文件,如果没有,可以手动拷贝相应的profile文件,如果中间某些目录(例如 :MobileDevice)缺失,也可以自己手动创建
或是下面问题
FATAL: Unable to unlock the keychain. Will not set default: file /Users/Shared/Jenkins/Library/Keychains/login.keychain
10.12系统后 keychain变成keychain-db
所以最好将login.keychain与login.keychain-db 都复制过去
5.构建
插件全部安装完毕后,下面就可以正式开始自动化构建了!!!
1)点击新建进行构建新的项目

2)设置参数

为了方便举例子,这里使用GitHub上的项目,具体情况可以使用自己公司的项目地址

如果出现上述的红色警告,请检查SSH是否配置正确,配置成功如下图

Poll SCM (poll source code management) 轮询源码管理
需要设置源码的路径才能起到轮询的效果,因为这个项目主要是用来测试,因此时间设置为60分钟才查询一下

3)添加脚本
Xcode integration也是可以进行打包测试的,但xcode8.0之后老是有一下问题,因此直接使用脚本构建

点击 增加构建步骤 选择 Execute shell 填写
xcodebuild clean build test -workspace JenkinsWithKIFDemo.xcworkspace -scheme JenkinsWithKIFDemo -destination 'platform=iOS,name=coderOnePolo'
本来是在模拟器进行测试的 但是老是报错
iPhoneSimulator: Could not launch simulator: -10810
,即使按照SETTING UP JENKINS CI ON A MAC的设置依旧有问题,所以现在先用真机进行测试

到目前为止 基本的构建已经完成 后面可以根据需要再添加远程测试或者添加报表等需求。
3.使用KIF进行测试
使用Cocoapod管理,在项目的Podfile file中添加KIF
# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'
target 'JenkinsWithKIFDemo' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
pod 'AFNetworking','~>2.5.4'
end
target 'JenkinsWithKIFDemoTests' do
#inherit! :search_paths
pod 'KIF', :configurations => ['Debug']
# Pods for testing
end

如果是在旧的项目引入Test,则需要添加以下几个配置





之后就是添加测试代码了,简单的测试效果如下

使用Jenkins实现自动化测试
接下来就是见证成果的时候了,可以自己马上点击立即构建进行测试


配置好后当自己的代码修改提交到远程仓库后,Jenkins就能马上自动化测试代码。(__)
补充 配置时候的一些坑
qustion:1.A build only device cannot be used to run this target
I get this error when I trying to test an app.
A build only device cannot be used to run this target.
No supported iOS devices are available. Connect a device to run your application or choose a simulated device as the destination.
add -destination 'platform=iOS Simulator,name=iPhone 6,OS=9.3' to your xcodebuild
qustion:2.Could not launch simulator: -10810.
I had exact same issue. It happens because Mac OS X doesn't allow an iOS simulator to run in the BACKGROUND.
If you are constructing a Jenkins CI environment, and trying to unit test via the simulator, you can't run the simulator, because Jenkins is basically run as a DAEMON. This also happens if you're running the tests behind environments like tmux or screen.
Here is a great tutorial to read that can help you fix this.
Cheers!
qustion:3 The target … overrides the OTHER_LDFLAGS
build setting defined in `Pods/Pods.xcconfig
在那个target下就去设置哪个target的setting
This definitely works most of the time:
Go to your target Build Settings -> Other linker flags -> double click . Add $(inherited) to a new line.
If you have problem with "...target overrides the GCC_PREPROCESSOR_DEFINITIONS build setting defined in..." then you must add $(inherited) to your target Build Settings -> Preprocessor Macros
qustion:4 出现错误 Failed to connect to repository : Command "git ls-remote -h
选择SSH Username with private key
直接输入私钥,要完整复制 ~/.ssh/id_isa里面的内容。
网友评论