天天看点

CI CD系统整合 转载_CI 系统搭建:Git、Gerrit与Jenkins Gerrit 和 Jenkins 整合 GitLab 上为 openstack 项目的一些准备 Gerrit 上为 openstack 项目的一些准备 在 Jenkins 上对 openstack 项目创建构建任务 提交 Review 任务

CI CD系统整合 转载_CI 系统搭建:Git、Gerrit与Jenkins Gerrit 和 Jenkins 整合 GitLab 上为 openstack 项目的一些准备 Gerrit 上为 openstack 项目的一些准备 在 Jenkins 上对 openstack 项目创建构建任务 提交 Review 任务

分类:

软件集成和项目管理(3)

CI CD系统整合 转载_CI 系统搭建:Git、Gerrit与Jenkins Gerrit 和 Jenkins 整合 GitLab 上为 openstack 项目的一些准备 Gerrit 上为 openstack 项目的一些准备 在 Jenkins 上对 openstack 项目创建构建任务 提交 Review 任务

去年写的这五篇 CI 文章时候方便邮件测试,自己搞了一个 thstack.com 域名玩。当时也没在意,所有的文章里邮箱地址都是引用 @thstack.com 域名。让我没想到是,2014 年这个神奇的一年,thstack.com 会成为我们的公司名字和域名。

我想说的是,我们内部的邮件系统也在用 @thstack.com,和这几个文章里的邮箱会冲突,导致一些朋友完全照着我的文档测试。结果最近收到了很多垃圾邮件。

我还想说的是,不要让别人怀疑你的智商。

我最想说的一句是,搞 CI 是一个高尚的工作。

随着计算机的高速发展、各种时代变革的到来。百度、360、腾讯这些赚钱机器之间为了利益掐个没完没了。开源项目、社区的活跃进而引来持续集成 (CI)系统的诞生。也越发的听到更多的人在说协同开发、敏捷开发、迭代开发、持续集成和单元测试这些拉风的术语。但是也仅仅只是听到在说而已,也没有见 到国内有几家公司能有完整的 CI 体系流程。反之一些开源项目都有完整的 CI 体系。我觉得就这一点国内的公司还是要去学习的。

由于对上面那些生涩的词感冒,专门研究了下 CI 相关的系统,才有了这几篇文章

我用到的系统有:

Gitlab:代码托管

Gerrit:Code Review

Jenkins:代码测试

一开始测试使用了 Gitorious 来做代码的托管,发现界面的功能不全,比较蛋疼。虽然 Gerrit 本身有代码托管功能,Gerrit 的界面不敢恭维、也没有 Gitlab 的功能强大。so …

决定使用 Gitlab 还有一点重要原因就是它本身提供保护分支的功能,可以达到 Review 效果。这样和 Gerrit 结合的话,可以针对不同的用户群来分配 Review 方式。

强制 Review:在 Gitlab 上创建的项目,指定相关用户只有 Reporter 权限,这样用户没有权限使用 git push 功能,只能 git review 到 Gerrit 系统上,Jenkins 在监听 Gerrit 上的项目事件会触发构建任务来测试代码,Jenkins 把测试结果通过 ssh gerrit 个这个项目打上 Verified 成功或失败标记,成功通知其它人员 Review。

Gitlab 保护 Master 分支:在 Gitlab 上创建的项目可以把 Master 分支保护起来,普通用户可以自己创建分支并提交代码到自己的分支上,没有权限直接提交到 Master 分支,用户最后提交申请把自己的分支 Merge 到 Master,管理员收到 Merge 请求后,Review 后选择是否合并。

由于没有闲置服务器、公网 IP、域名让我去浪费。想到 qingcloud 上还有 2k 没花,所以需要把三套系统整合塞到一个 vps 中。在整合三套系统中遇到一些需要规划的小问题

每个系统都有发送邮件的功能,最好弄三个邮箱账号

三个系统都默认监听了 8080 端口,需要规划端口

规划了端口的同时随便规划下三个域名,后端做个端口转发方便用户访问

知道什么需要规划后,就来设置吧:

使用系统

修改 ssh 时候不需要输入 yes,如果不设置后面 Jenkins 连接 Gerrit 时候会报错

Gitlab、Gerrit、Jenkins 版本和下载地址

<a href="https://github.com/gitlabhq/gitlabhq" target="_blank">Gitlab 6.4 Stable</a>

<a href="http://gerrit-releases.storage.googleapis.com/gerrit-2.8.war" target="_blank">Gerrit 2.8 War</a>

<a href="http://pkg.jenkins-ci.org/debian/binary/jenkins_1.544_all.deb" target="_blank">Jenkins 1.544 Deb</a>

设置主机名

注册四个邮箱账号,其中 [email protected] 用来管理三套系统

规划三个域名和端口

设置解析

简单规划完后就可以动手去搭建了,后面几篇文章会记录安装的过程。

目录

<a href="http://longgeek.com/2013/12/26/ci-system-structures-ii-gitlab-installation/#i" target="_blank">1 设置源</a>

<a href="http://longgeek.com/2013/12/26/ci-system-structures-ii-gitlab-installation/#i-2" target="_blank">2 安装依赖包</a>

<a href="http://longgeek.com/2013/12/26/ci-system-structures-ii-gitlab-installation/#i-3" target="_blank">3 系统用户</a>

<a href="http://longgeek.com/2013/12/26/ci-system-structures-ii-gitlab-installation/#GitLab_Shell" target="_blank">4 GitLab Shell</a>

<a href="http://longgeek.com/2013/12/26/ci-system-structures-ii-gitlab-installation/#Mysql" target="_blank">5 Mysql</a>

<a href="http://longgeek.com/2013/12/26/ci-system-structures-ii-gitlab-installation/#GitLab" target="_blank">6 GitLab</a>

<a href="http://longgeek.com/2013/12/26/ci-system-structures-ii-gitlab-installation/#Nginx" target="_blank">7 Nginx</a>

<a href="http://longgeek.com/2013/12/26/ci-system-structures-ii-gitlab-installation/#i-4" target="_blank">8 界面简单使用</a>

设置国内 163 apt 源

Gitlab 依赖包、库

安装 markdown 文档风格依赖包

安装 git,Gerrit 依赖 gitweb,同时 GitLab 依赖 git 版本 &gt;= 1.7.10,Ubuntu apt-get 默认安装的是 1.7.9.5,当然不升级也是没有问题的

升级 Git 版本(可选)

Gitlab 需要收发邮件,安装邮件服务器

如果安装了 ruby1.8,卸载掉,Gitlab 依赖 2.0 以上

下载编译 ruby2.0

修改 gem 源指向 taobao

安装 Bundel 命令

给 Gitlab 创建一个 git 用户

下载 Gitlab Shell,用来 ssh 访问仓库的管理软件

修改 gitlab-shell/config.yml

安装 GitLab Shell

安装 Mysql 包

给 Gitlab 创建 Mysql 数据库并授权用户访问

下载 GitLab 源代码,并切换到最新的分支上

配置 GitLab,修改 gitlab.yml,其中 host: 项和 gitlab-shell 中 gitlab_url 的主机一致

创建相关目录

修改相关目录权限

修改 unicorn.rb 监听端口为:8081

配置 GitLab 访问 mysql 数据库设置

设置 GitLab 使用指定邮箱发送邮件,注意 production.rb 的文件格式,开头空两格

安装 gem

修改 Gemfile 文件中源指向为 taobao

虽然在文件中指向了国内 taobao 源,但是依然会卡一会,耐心等待…

初始化数据库并激活高级功能

输入 yes 来初始化数据库、创建相关表,最后会输出 GitLab Web 管理员用来登录的账号和密码

设置 GitLab 启动服务

设置 GitLab 使用 Logrotate 备份 Log

检查GitLab及其环境的配置是否正确:

启动 GitLab 服务

最后编译一下

安装 Nginx 包

配置 Nginx

启动 Nginx

访问

点击保存更改后,系统会自动给刚才输入的邮箱地址发送一封确认修改信息,点击邮件内容中的连接后会自动用新账号邮箱登录。

创建一个 GROUP:

<a href="http://longgeek.com/wp-content/uploads/2013/12/gitlab-click-group.jpg" target="_blank"></a>

很显然 longgeek 用户是没有 push 到 master 分支得权限。接下来会安装 Gerrit、Jenkins。以及它们三个如何整合成流程。请参考后面得文章。

重新安装 gerrit 需要特别注意:

如果有项目在使用记得备份 /etc/gerrit/git 目录

rm -fr /etc/gerrit

mysql -uroot -p

&gt;&gt;&gt;drop database gerritdb;

&gt;&gt;&gt;create database gerritdb;

重新安装吧。

如果 /etc/gerrit/logs/error_log 里出现 java.lang.IllegalStateException: Missing project All-Projects 这个错误,那就重新安装下,记得清除一下 db。

<a href="http://longgeek.com/2013/12/26/ci-system-structures-iii-gerrit-installation-configuration/#_Gerrit" target="_blank">1 下载 Gerrit 包</a>

<a href="http://longgeek.com/2013/12/26/ci-system-structures-iii-gerrit-installation-configuration/#_Gerrit-2" target="_blank">2 安装 Gerrit 依赖</a>

<a href="http://longgeek.com/2013/12/26/ci-system-structures-iii-gerrit-installation-configuration/#_Gerrit-3" target="_blank">3 创建 Gerrit 数据库</a>

<a href="http://longgeek.com/2013/12/26/ci-system-structures-iii-gerrit-installation-configuration/#i" target="_blank">4 开始安装</a>

<a href="http://longgeek.com/2013/12/26/ci-system-structures-iii-gerrit-installation-configuration/#Nginx" target="_blank">5 Nginx</a>

<a href="http://longgeek.com/2013/12/26/ci-system-structures-iii-gerrit-installation-configuration/#i-2" target="_blank">6 访问</a>

<a href="http://longgeek.com/2013/12/26/ci-system-structures-iii-gerrit-installation-configuration/#i-3" target="_blank">7 最后</a>

目前最新版为 2.8

Gerrit 的包是 java 格式,需要安装 jre

上一篇再安装 GitLab 时已经安装了 Mysql,直接创建库

把 gerrit 安装再 /etc/gerrit/ 下

在安装完后 Gerrit 默认会打开浏览器,由于我的系统是 Server 版没有桌面和浏览器,所以才会出现上面倒数第三行的 …FAILED 同时从上面的信息看出我使用了 http 方式验证、启用代理服务器,指定了 Gerrit 端口为 8082,URL 为 review.thstack.com。需要在 nginx 里设置下端口转发。

Gerrit 启动脚本

修改 Nginx 配置文件,给 Gerrit 做端口转发和访问控制,把下面内容写入到文件最后

创建 htpasswd.conf 文件,并添加 admin 用户、密码到文件中

默认第一个登录 Gerrit 的用户是 Admin。

使用 htpasswd 创建 longgeek 用户和密码

如果想 Gitlab 上创建的项目使用 Gerrit 的 Code Review 功能,两个系统的用户必须统一,也就是说不管哪个用户使用 Gerrit,前提是这个用户在 Gitlab 和 Gerrit 上都已注册,邮箱一致、sshkey 一致。当然 Nginx 访问控制用户的密码那就随意了。至于 Gitlab 上创建的项目如何同步到 Gerrit 上、Gitlab 如何使用 Gerrit 的 Code Review 功能等等都在 Jenkins 安装完之后会一起整合在一起。请关注后面的文章。

本文作为之前几篇文章的延续,在动手安装 Jenkins 之前,确保参考了如下文章:

<a href="http://longgeek.com/2013/12/24/ci-build-system-a-basic-environmental-setting-planning/" target="_blank">CI 系统搭建:一. 基础环境设置、规划</a>

<a href="http://longgeek.com/2013/12/26/ci-system-structures-ii-gitlab-installation/" target="_blank">CI 系统搭建:二. GitLab 的安装配置</a>

<a href="http://longgeek.com/2013/12/26/ci-system-structures-iii-gerrit-installation-configuration/" target="_blank">CI 系统搭建:三. Gerrit 的安装配置</a>

<a href="http://longgeek.com/2013/12/27/ci-system-structures-iv-jenkins-installation-configuration/#Jenkins" target="_blank">1 Jenkins</a>

<a href="http://longgeek.com/2013/12/27/ci-system-structures-iv-jenkins-installation-configuration/#i" target="_blank">2 下载包</a>

<a href="http://longgeek.com/2013/12/27/ci-system-structures-iv-jenkins-installation-configuration/#Jenkins-2" target="_blank">3 Jenkins</a>

<a href="http://longgeek.com/2013/12/27/ci-system-structures-iv-jenkins-installation-configuration/#Nginx" target="_blank">4 Nginx</a>

<a href="http://longgeek.com/2013/12/27/ci-system-structures-iv-jenkins-installation-configuration/#i-2" target="_blank">5 访问</a>

一个可扩展的开源持续集成服务器,可扩展 Jenkins 系统集群方式、大量的插件方式扩展,显然 Jenkins

自己已经形成了一个小生态圈。还有一点不得不说的是 Jenkins 本身的迭代开发非常猛,看了下版本发布周期平均一周一个版本。具体的

在写这篇文章时候,Jenkins 最新版本为 1.544

安装依赖包

安装 Jenkins

jenkins 默认监听了 8080 端口,修改为 8083

重新 jenkins 服务

配置 Nginx 端口转发,在文件末尾加入下面配置

重启 Nginx,就可以用 jenkins.thstack.com 访问 Jenkins 了

在系统中给 Jenkins 用户生成 ssh 密钥,Jenkins 用户在安装包的时候自动创建了,家目录在 /var/lib/jenkins

用 htpasswd 创建 jenkins 用户访问控制密码

继续换个浏览器或者清除浏览器记录,用 jenkins 用户访问 Gerrit

回到 Jenkins,设置 Gerrit Trigger 插件,填写刚才在 Gerrit 上注册的 jenkins 用户信息 点击 -&gt; 系统管理 -&gt; Gerrit Trigger -&gt; 填写 Gerrit Server 中的信息,最后点击 Test Connection,会在左侧出现 Success,如果失败检查输入的信息是否有误*

2014.04.05 update:

点击 ‘Test Connection’ 如果出现下面内容,不要着急,继续跟着我的博客往下做。这个问题会在第五篇文章里解决掉。

<a href="http://longgeek.com/wp-content/uploads/2013/12/jenkins-gerrit-trigger-ssh1.jpg" target="_blank"></a>

如果上面一步出现下图错误,是因为 jenkins ssh Gerrit 时候需要输入 yes,卡住了 可以手工 ssh 输入一下 也是。在返回界面 多刷新几次就没有错误信息了。 su – jenkins ssh -p 29418 [email protected] gerrit

<a href="http://longgeek.com/wp-content/uploads/2013/12/jenkins-gerrit-conn-err.jpg" target="_blank"></a>

OK,Jenkins 设置完毕,接下来就需要针对项目在 jenkins 上创建构建任务,放在下一篇文章里。

参考之前的文章:

<a href="http://longgeek.com/2013/12/27/ci-system-structures-iv-jenkins-installation-configuration/" target="_blank">CI 系统搭建:四. Jenkins 的安装配置</a>

<a href="http://longgeek.com/2014/01/07/ci-system-structures-five-gitlab-gerrit-jenkins-three-integration/#Gerrit_Jenkins" target="_blank">1 Gerrit 和 Jenkins 整合</a>

<a href="http://longgeek.com/2014/01/07/ci-system-structures-five-gitlab-gerrit-jenkins-three-integration/#GitLab_openstack" target="_blank">2 GitLab 上为 openstack 项目的一些准备</a>

<a href="http://longgeek.com/2014/01/07/ci-system-structures-five-gitlab-gerrit-jenkins-three-integration/#gitreview" target="_blank">2.1 .gitreview</a>

<a href="http://longgeek.com/2014/01/07/ci-system-structures-five-gitlab-gerrit-jenkins-three-integration/#testrconf" target="_blank">2.2 .testr.conf</a>

<a href="http://longgeek.com/2014/01/07/ci-system-structures-five-gitlab-gerrit-jenkins-three-integration/#Gerrit_openstack" target="_blank">3 Gerrit 上为 openstack 项目的一些准备</a>

<a href="http://longgeek.com/2014/01/07/ci-system-structures-five-gitlab-gerrit-jenkins-three-integration/#_Gerrit_openstack" target="_blank">3.1 在 Gerrit 上创建 openstack 项目</a>

<a href="http://longgeek.com/2014/01/07/ci-system-structures-five-gitlab-gerrit-jenkins-three-integration/#clone_8211bare_Gitlab_Gerrit" target="_blank">3.2 clone –bare Gitlab 上的仓库到 Gerrit</a>

<a href="http://longgeek.com/2014/01/07/ci-system-structures-five-gitlab-gerrit-jenkins-three-integration/#_Gerrit_openstack_Gitlab_openstack" target="_blank">3.3 同步 Gerrit 的 openstack 项目到 Gitlab 上的 openstack 项目目录中</a>

<a href="http://longgeek.com/2014/01/07/ci-system-structures-five-gitlab-gerrit-jenkins-three-integration/#_Jenkins_openstack" target="_blank">4 在 Jenkins 上对 openstack 项目创建构建任务</a>

<a href="http://longgeek.com/2014/01/07/ci-system-structures-five-gitlab-gerrit-jenkins-three-integration/#_Review" target="_blank">5 提交 Review 任务</a>

让 Gerrit 支持 Jenkins,Gerrit 在 2.7 版本后去掉了 ‘lable Verified’,需要自己添加

查看 Jenkins 的 log 发现会一直出现下面的信息,是因为 Jenkins 没有权限监听 Gerrit 的 ‘Stream Events’

在 Gerrit 全局设置中能看到 Gerrit 的 ‘Stream Events’ 动作权限默认对 ‘Non-Interactive Users’ 组开放

<a href="http://longgeek.com/wp-content/uploads/2013/12/gerrit-Non-Interactive-Users.jpg" target="_blank"></a>

[email protected] 用户登录 Gerrit 系统,添加 [email protected] 用户到 ‘Non-Interactive Users’ 组

现在提交的 Review 请求只有 Code Rivew 审核,我们要求的是需要 Jenkins 的 Verified 和 Code Review 双重保障,在 Projects 的 Access 栏里,针对 Reference: refs/heads/ 项添加 Verified 功能*

编辑 .gitreview 文件

添加到版本库

Python 代码我使用了 testr,需要先安装 testr 命令

在 openstack 这个项目中添加 .testr.conf 文件

提交到版本库中

要知道 review 是在 gerrit 上,而 gerrit 上现在是没有项目的,想让 gitlab 上的项目能在 gerrit 上 review 的话,必须在 gerrit 上创建相同的项目,并有相同的仓库文件.

用 admin 用户在 Gerrit 上创建 openstack 项目

在 Gerrit 上 clone Gitlab 的 openstack 项目

当用户 git review 后,代码通过 jenkins 测试、人工 review 后,代码只是 merge 到了 Gerrit 的 openstack 项目中,并没有 merge 到 Gitlab 的 openstack 项目中,所以需要当 Gerrit openstack 项目仓库有变化时自动同步到 Gitlab 的 openstack 项目仓库中。Gerrit 自带一个 Replication 功能,同时我们在安装 Gerrit 时候默认安装了这个 Plugin。现在只需要添加一个 replication.config 给 Gerrit。

添加 replication.config 文件

上面的 url 是用 root 用户来做 Gerrit 的 openstack 项目复制到 Gitlab 的 openstack 项目中,需要免密码登录,生成密钥

设置 ~/.ssh/config

在 ~/.ssh/known_hosts 中,给 gitlab.thstack.com 添加 rsa 密钥

重新启动 Gerrit 服务

Gerrit 的复制功能配置完毕,在 gerrit 文档中有一个 ${name} 变量用来复制 Gerrit 的所有项目,这里并不需要。如果有多个项目需要复制,则在 replication.config 中添加多个 [remote ....] 字段即可。务必按照上面步骤配置复制功能。

切换到 longgeek 用户,之前创建了一个 longgeek 用户,方便测试

同步后,使用 git pull 命令就可以从 Gitlab 上的 openstack 仓库下来代码

如此便是一个完整的 CI 体系流程了,剩下的最重要的是如何用、真正的用起来。

上一篇: AC自动机
下一篇: AC自动机