设为首页 收藏本站
查看: 1318|回复: 0

[经验分享] CI 系统搭建:五. GitLab、Gerrit、Jenkins 三者整合

[复制链接]

尚未签到

发表于 2018-1-6 22:50:38 | 显示全部楼层 |阅读模式
  参考之前的文章:


  • CI 系统搭建:一. 基础环境设置、规划
  • CI 系统搭建:二. GitLab 的安装配置
  • CI 系统搭建:三. Gerrit 的安装配置
  • CI 系统搭建:四. Jenkins 的安装配置
  目录


  • 1 Gerrit 和 Jenkins 整合
  • 2 GitLab 上为 openstack 项目的一些准备

    • 2.1 .gitreview
    • 2.2 .testr.conf

  • 3 Gerrit 上为 openstack 项目的一些准备

    • 3.1 在 Gerrit 上创建 openstack 项目
    • 3.2 clone –bare Gitlab 上的仓库到 Gerrit
    • 3.3 同步 Gerrit 的 openstack 项目到 Gitlab 上的 openstack 项目目录中

  • 4 在 Jenkins 上对 openstack 项目创建构建任务
  • 5 提交 Review 任务

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

# cd /tmp  
# git init cfg; cd cfg
  
# git config user.name 'admin'
  
# git config user.email 'admin@thstack.com'
  
# git remote add origin ssh://admin@review.thstack.com:29418/All-Projects
  
# git pull origin refs/meta/config
  
# vim project.config        # 在文件末尾添加下面 5 行
  
[label "Verified"]
  
function = MaxWithBlock
  
value = -1 Fails
  
value =  0 No score
  
value = +1 Verified
  
# git commit -a -m 'Updated permissions'
  
# git push origin HEAD:refs/meta/config
  

  

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

# tail -f /var/log/jenkins/jenkins.log  
Dec 26, 2013 1:16:49 AM com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritHandler run
  
INFO: Ready to receive data from Gerrit
  
Dec 26, 2013 1:16:49 AM com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritHandler run
  
INFO: Ready to receive data from Gerrit
  

  

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

  用 admin@thstack.com 用户登录 Gerrit 系统,添加 Jenkins@thstack.com 用户到 ‘Non-Interactive Users’ 组
  点击 -> People -> List Groups -> Non-Interactive Users
  现在提交的 Review 请求只有 Code Rivew 审核,我们要求的是需要 Jenkins 的 Verified 和 Code Review 双重保障,在 Projects 的 Access 栏里,针对 Reference: refs/heads/ 项添加 Verified 功能*
  点击 Projects -> Access -> Edit -> 找到 Reference: refs/heads/* 项 -> Add Permission -> Label Verified -> Group Name 里输入 Non-Interactive Users -> Add -> 在最下面点击保存更改

GitLab 上为 openstack 项目的一些准备

.gitreview
  之前在 CI 系统搭建:二. GitLab 的安装配置 文章中在 GitLab 上创建了一个叫 openstack 项目,并设置了权限,普通用户是没有办法去 push 的,只能使用 git review 命令提交. 而 git review 命令需要 .gitreview 文件存在于项目目录里,用 admin@thstack.com 用户添加 .gitreview 文件
  

git clone git@gitlab.thstack.com:devgroup/openstack.git  
cd openstack
  
git config user.name 'admin'
  
git config user.email 'admin@thstack.com'
  

  

  编辑 .gitreview 文件
  

vim .gitreview  
[gerrit]
  
host=review.thstack.com
  
port=29418
  
project=openstack.git
  

  

  添加到版本库
  

git add .gitreview  
git commit .gitreview -m 'Admin add .gitreview file.'
  
git push origin master
  

  

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

apt-get install python-pip  
pip install testrepository
  

  

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

cd openstack  
vim .testr.conf
  
[DEFAULT]
  
test_command=OS_STDOUT_CAPTURE=1 OS_STDERR_CAPTURE=1 OS_TEST_TIMEOUT=60 ${PYTHON:-python} -m subunit.run discover -t ./ ./ $LISTOPT $IDOPTION
  
test_id_option=--load-list $IDFILE
  
test_list_option=—list
  

  

  提交到版本库中
  

git add .testr.conf  
git commit .testr.conf -m 'Admin add .testr.conf file'
  
git push origin master
  

  

Gerrit 上为 openstack 项目的一些准备

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

ssh -p 29418 admin@review.thstack.com gerrit create-project openstack  

  

clone –bare Gitlab 上的仓库到 Gerrit
  在 Gerrit 上 clone Gitlab 的 openstack 项目
  

cd /etc/gerrit/git  
rm -fr openstack.git
  
git clone --bare git@gitlab.thstack.com:devgroup/openstack.git
  

  

同步 Gerrit 的 openstack 项目到 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 文件
  

vim /etc/gerrit/etc/replication.config  
[remote "openstack"]
  
# Gerrit 上要同步项目的名字
  
projects = openstack
  
url = root@gitlab.thstack.com:/home/git/repositories/devgroup/openstack.git
  
push = +refs/heads/*:refs/heads/*
  
push = +refs/tags/*:refs/tags/*
  
push = +refs/changes/*:refs/changes/*
  
threads = 3
  

  

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

ssh-copy-id -i gitlab.thstack.com  # 输入 root 用户密码  

  

  设置 ~/.ssh/config
  

vim ~/.ssh/config  
Host gitlab.thstack.com:
  
IdentityFile ~/.ssh/id_rsa
  
PreferredAuthentications publickey
  

  

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

> ~/.ssh/known_hosts  
ssh-keyscan -t rsa gitlab.thstack.com > ~/.ssh/known_hosts
  
ssh-keygen -H -f ~/.ssh/known_hosts
  

  

  重新启动 Gerrit 服务
  

/etc/init.d/gerrit restart  

  

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

在 Jenkins 上对 openstack 项目创建构建任务
  用 admin 用户登录 jenkins,针对 openstack 项目创建构建任务 点击 新建 -> 输入任务名 -> 选中构建一个自由风格的软件项目
  选中 ‘git’ -> 在 ‘Repository URL’ 里输入 openstack 项目在 Gerrit 上的地址(在 gerrit 的 project list 中 openstack 后面的 gitweb 里有具体 url,) -> 在 ‘Branches to build’ 里输入 ‘origin/$GERRIT_BRANCH’
  选中 Gerrit event -> 点击 Add 分别添加 ‘Patchset Created’ 和 ‘Draft Published’ -> 在第一个 plain 输入项目名字 openstack,第二个 plain 输入 master -> 点击 增加构建步骤 -> 选择 ‘Execute shell’ -> 写入要测试的脚本 -> 保存 同时也可以选择 ‘添加构建后操作步骤’ 来邮件通知一个群组等功能。

提交 Review 任务
  切换到 longgeek 用户,之前创建了一个 longgeek 用户,方便测试
  

su - longgeek  
cd openstack   # 之前添加过一个 testfile 文件,push 没有权限,所以用 git review 来代替 git push
  
git push
  
git review
  
Creating a git remote called gerrit that maps to:
  
ssh://longgeek@review.thstack.com:29418/openstack.git
  
Your change was committed before the commit hook was installed

  
Amending the commit to add a gerrit change>  
remote: Processing changes: new: 1, refs: 1, done
  
remote:
  
remote: New Changes:
  
remote:   http://review.thstack.com/1
  
remote:
  
To ssh://longgeek@review.thstack.com:29418/openstack.git
  
* [new branch]      HEAD -> refs/for/master/master
  

  

  用 admin 用户登录 http://review.thstack.com 就可以看到 longgeek 用户提交的 reivew 请求
  可以看到 jenkins 已经通过,并打上了 ‘绿勾’ 标记,接下来 admin 用户 +2 并提交就可以 Merge 代码
  在 Merge 代码后,Gerrit 会自动同步到 Gitlab 上,如下图
  同步后,使用 git pull 命令就可以从 Gitlab 上的 openstack 仓库下来代码
  

cd openstack  
git pull
  
From gitlab.thstack.com:devgroup/openstack
  
70c59ff..7948aae  master     -> origin/master
  
Already up-to-date.
  

  

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

back

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-432411-1-1.html 上篇帖子: 天涯之巅的地盘 下篇帖子: zhongxia
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表