Jenkins+Gitlab CE+Robot Framework持续集成

环境


Ubuntu 14.04.3 LTS Desktop

 

前提


1.在本地能执行测试脚本(pybot yourTestSuit.txt),本文不讲解如何学习使用RF框架

2.已有Gitlab环境,本文不讲解如何搭建Gitlab

 

jenkins安装


添加仓库:

~$ wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -

打开文件源:

~$ gedit /etc/apt/sources.list

添加以下内容:

deb https://pkg.jenkins.io/debian-stable binary/

更新并安装jenkins(下载很慢,有能力请科学上网后执行以下命令)

~$ sudo apt-get update
~$ sudo apt-get install jenkins

 默认端口8080,打开浏览器输入以下登录jenkins

localhost:8080

 

Jenkins上执行RobotFramework测试用例


 安装RobotFramework Plugin

Manage Jenkins > Manage Plugins > 搜索安装Robot Framework plugin

 Jenkins运行测试脚本会出现无法打开浏览器导致fail的情况,因此需要安装XVnc Plugin(跑用例的时候仍然不会打开浏览器,但是测试正常进行)

Manage Jenkins > Manage Plugins > 搜索安装XVnc plugin

$ sudo apt-get install vnc4server
 Now start the VNC server once as the jenkins user and give it a password,
 otherwise it'll try to ask Jenkins
$ sudo su jenkins
$ vncserver
[enter password: vncserver]

 

 创建作业(如图)

Enter an item name > Freestyle project > OK

Build Environment 中勾选 Run Xvnc during build(如图)

Build中输入shell脚本运行测试用例:(以下代码先在本地运行,测试可用后粘贴)

pybot yourTestSuit.txt

 

 

设置输出RF的测试报告

Post-built Actions中,add post-built action > Publish Robot Framework test results

Apply > Save

验证:

Build now > 点击任务名称(如图) > 点击Console Output可查看控制台输出/

用例跑完输出测试报告:

Gitlab代码更新触发Jenkins项目构建


思路: Jenkins生成remote trigger, gitlab 设置webhook, 当仓库代码变更时, 触发构建

安装 Gitlab Plugin

安装 Gitlab Hook Plugin。

Jenkins端设置Remote Trigger

 Gitlab端设置Webhook

进入项目,右上角齿轮选择Intergtations(webhook)

 

URL填自己的IP+/buildByToken/build?job=gitlab-test&token=+自己设置的token值,可以直接在浏览器输入验证:

如果Jenkins项目成功构建,则URL正确:

触发选项选择自己需要的,我这只是测试,故保持默认

测试是否正常工作

点击test

 

如果成功,则本页面提示蓝色successfully,失败提示红色failed

查看Jenkins项目是否被触发构建

 

 至此,Gitlab代码更新能够触发Jenkins项目构建。

 

自动化测试持续集成完整实现


 

设计:

项目1:Gitlab代码更新触发本项目实现自动pull代码至本地

项目2:项目1完成后,本项目运行自动化测试,并输出测试结果与报告,发送至管理员邮箱

 

待续...

 

错误处理


Xvnc报错

[Host_Regression_easytest] $ vncserver :62 You will require a password to access your desktops.

getpassword error: Invalid argument

Password:FATAL: Failed to run 'vncserver :62' (exit code 1), blacklisting display #62; consider checking the "Clean up before start" optionjava.io.IOException: Failed to run 'vncserver :62' (exit code 1), blacklisting display #62; consider checking the "Clean up before start" option

at hudson.plugins.xvnc.Xvnc.doSetUp(Xvnc.java:100)

at hudson.plugins.xvnc.Xvnc.doSetUp(Xvnc.java:98)

at hudson.plugins.xvnc.Xvnc.doSetUp(Xvnc.java:98)

at hudson.plugins.xvnc.Xvnc.doSetUp(Xvnc.java:98)

at hudson.plugins.xvnc.Xvnc.setUp(Xvnc.java:73)

at hudson.model.Build$BuildExecution.doRun(Build.java:154)

at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:499)

at hudson.model.Run.execute(Run.java:1488)

at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)

at hudson.model.ResourceController.execute(ResourceController.java:88)

at hudson.model.Executor.run(Executor.java:236)

解决:

在job执行的所在服务器上(job执行有可能是在master上,有可能在slave上运行),执行如下步骤:

  • 首先运行vncserver命令

  • 然后提示输入密码,你随便输入一个密码

  • 建立好了,再kill掉这个vnc session即可。(例如“vncserver -kill :1”)

 

Gitlab Webhook 点击Test时提示HTTP 500

解决:

先将URL设置为外网链接,例如baidu.com, 若测试成功,则证明是网络问题。

接下来确定你的网络与Gitlab服务器互通,而非单向连通。由于我在A市办公,Gitlab服务器在B市,我用公司提供的OpenVPN连接可以ping通Gitlab所在网段,而B市同事无法ping通我的ip,从而确定是网络问题。

改用公司提供的PPTP VPN后,问题解决。

 

 

待续...

posted @ 2017-01-09 11:04  严北  阅读(1856)  评论(0编辑  收藏  举报