本章节主要介绍jenkins的使用
在前一章节中已经介绍可jenkins安装步骤,不了解的可以参考前文:[jenkins环境搭建](http://www.jianshu.com/p/1a0971bd8139)
* * *
# jenkins如何和github对接
jenkins是插件式的,所以需要对接github或svn时,需要下载相应的插件。同样,当需要对接github代码库时,就需要下载官方提供的git plugin和github plugin插件了,具体安装步骤如下:
#### **(1)在jenkins首页中,点击系统管理->管理插件**

然后,在可选插件中选择需要下载的插件点击下载即可,如果安装失败,则需要在高级中手动上传phi文件下载。大部分下载失败的原因多由于网络或是依赖导致。
如果是下载成功后,发现还会出现错误,建议是重启jenkins服务。
#### **(2)保证从github下载代码,免密码输入(可选)**
从github代码库下载工程代码,要达到免密码登录,就需要设置SSH key给github,使用公钥私钥认证
我们jenkins是安装在unbuntu机器,相应的git代码也是要下载到这台机器上,在这台机器中设置证书
参考:[如何设置证书](http://jingyan.baidu.com/article/a65957f4f0acc624e67f9bc1.html?spm=5176.100239.blogcont33036.4.Os9o7b)
**第一步**
ssh-keygen -t rsa -C "[email protected]"
Enter passphrase (empty for no passphrase):
当你看到这个的时候, 输入你的密码,重复输入密码就可以了,这时会在/root/.ssh目录下包含需要的证书
**第二步**
执行以下命令将KEY加入到ssh-agent:
$ eval "$(ssh-agent -s)"
# Agent pid 59566
$ ssh-add ~/.ssh/id_rsa
**第三步**
打开 ~/.ssh/id_rsa.pub文件, 复制其中所有的内容。然后把它粘贴到github个人账号settings配置项ssh key添加的表单中。

添加成功后,会显示如下图

**最后一步**
执行ssh -T [email protected]
如果不提示输入密码,表示成功
### (3)jenkins对接github
新建jenkins jobs

输入job名称,新建一个自由风格的软件项目,点击ok

最主要的是配置选择Github project,如果是没有ssh认证,就需要配置为https://github.com/geneseeq/auototest.git

这里需要配置git源码库地址和登录账户,已经构建的是master库代码

*注意:Credentials是需要新添加,添加步骤如下*
*a.首页中点击Credentials*

*添加页面中注意选择下图选中的就可以了*

然后在构建触发器配置中需要配置触发构建的条件,和检查间隔时间,以及每日构建定时时间

最后一步是设置了执行脚本,顺便写了一个测试执行的动作

最后就可以点击构建了,可以构建成功

* * *
# 邮件功能
当然仅有上边的功能还是不够便捷,我想设置一个构建结果的通知,可以使用邮件通知的插件
* 首先需要再系统管理中配置邮件服务器信息

点击左边的“系统管理”菜单,选择右边的“系统设置”

在系统设置中找到Jenkins Location项填入Jenkins URL和系统管理员邮件地址,系统管理员一定要配置,是用来转发邮件的。

找到Extended E-mail Notification项,填入smtp服务器信息及用户名、密码等认证信息

其邮件的显示信息代码为
然后邮件配置测试成功后,设置工程中要发送的成员邮件信息,需要在构建后中设置

在Advanced Settings中设置项目通知收件人的列表,主题,内容和附件
另外还有一个要注意的是要开启邮箱的smtp功能

# github merge代码的check功能
首先工程配置中”源码管理“配置需要修改

然后配置”构建触发器“中Github Pull Request Builder项

构建环境配置如下

注意,要pull request需要再全局变量中设置web hook,对接github

以及下边的配置是连接github的,可以测试是否可以连接

当然上边的证书配置内容,是需要github提供Org Webhooks Secret的

github webhooks配置

似乎一切都结束了,让我们实验一下吧。
如果配置成功的话,其结果会是下图那样
