jenkins github和docker构建CICD

本章节主要介绍jenkins的使用

在前一章节中已经介绍可jenkins安装步骤,不了解的可以参考前文:[jenkins环境搭建](http://www.jianshu.com/p/1a0971bd8139)

* * *

# jenkins如何和github对接

jenkins是插件式的,所以需要对接github或svn时,需要下载相应的插件。同样,当需要对接github代码库时,就需要下载官方提供的git plugin和github plugin插件了,具体安装步骤如下:

#### **(1)在jenkins首页中,点击系统管理->管理插件**

![系统管理](http://upload-images.jianshu.io/upload_images/2446860-842ef28c2af7bbd2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/400 )

然后,在可选插件中选择需要下载的插件点击下载即可,如果安装失败,则需要在高级中手动上传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 “your_email@example.com”

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添加的表单中。

![github ssh配置界面](http://upload-images.jianshu.io/upload_images/2446860-c6604e6b3d99471e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

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

![成功界面](http://upload-images.jianshu.io/upload_images/2446860-30d2aab8fe56611e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

**最后一步**

执行ssh -T git@github.com

如果不提示输入密码,表示成功

### (3)jenkins对接github

新建jenkins jobs

![新建jobs](http://upload-images.jianshu.io/upload_images/2446860-f47002cff244789a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

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

![新建jobs-步骤1](http://upload-images.jianshu.io/upload_images/2446860-c9e151f052dbfcf4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

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

![新建jobs-步骤2](http://upload-images.jianshu.io/upload_images/2446860-9e1d3b129b7b7efc.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

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

![新建jobs-步骤3](http://upload-images.jianshu.io/upload_images/2446860-1f1c2172b85ff3e0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

*注意:Credentials是需要新添加,添加步骤如下*

*a.首页中点击Credentials*

![认证配置](http://upload-images.jianshu.io/upload_images/2446860-1d8ac145ea2ffc98.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

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

![image](http://upload-images.jianshu.io/upload_images/2446860-c4b8b5028d0c03fb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

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

![构建触发器](http://upload-images.jianshu.io/upload_images/2446860-753643c8e5488aff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

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

![构建脚本](http://upload-images.jianshu.io/upload_images/2446860-d3cf7abf71236844.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

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

![成功](http://upload-images.jianshu.io/upload_images/2446860-5fb916974c79b41f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

* * *

# 邮件功能

当然仅有上边的功能还是不够便捷,我想设置一个构建结果的通知,可以使用邮件通知的插件

* 首先需要再系统管理中配置邮件服务器信息

![系统管理](http://upload-images.jianshu.io/upload_images/2446860-5b54de45a9161348?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

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

![系统设置](http://upload-images.jianshu.io/upload_images/2446860-697325cf9644c332?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

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

![邮件配置](http://upload-images.jianshu.io/upload_images/2446860-bef3d2e2a8c16632.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

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

![邮件服务器配置](http://upload-images.jianshu.io/upload_images/2446860-f1d3e5a505fd90ae.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

其邮件的显示信息代码为

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

![构建后操作](http://upload-images.jianshu.io/upload_images/2446860-431a12817f274e82.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

在Advanced Settings中设置项目通知收件人的列表,主题,内容和附件

另外还有一个要注意的是要开启邮箱的smtp功能

![邮箱设置](http://upload-images.jianshu.io/upload_images/2446860-d45d04e2e6acbb19.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

# github merge代码的check功能

首先工程配置中”源码管理“配置需要修改

![image](http://upload-images.jianshu.io/upload_images/2446860-3367212c3615fba9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

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

![构建触发器](http://upload-images.jianshu.io/upload_images/2446860-0bae9c0c8b995954.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

构建环境配置如下

![构建环境](http://upload-images.jianshu.io/upload_images/2446860-48b1896375eb96b6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

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

![image](http://upload-images.jianshu.io/upload_images/2446860-23154e4b22936aeb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

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

![image](http://upload-images.jianshu.io/upload_images/2446860-fa964c79736d9daa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

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

![github证书配置](http://upload-images.jianshu.io/upload_images/2446860-30360a1554dc6196.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

github webhooks配置

![webhook](http://upload-images.jianshu.io/upload_images/2446860-46d25e9dcb54c674.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

似乎一切都结束了,让我们实验一下吧。

如果配置成功的话,其结果会是下图那样

![](http://upload-images.jianshu.io/upload_images/2446860-768656ac64c91433.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/600)

    原文作者:haishuiaa
    原文地址: https://www.jianshu.com/p/123243300707
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞