本文共 5636 字,大约阅读时间需要 18 分钟。
目录
让 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 runINFO: Ready to receive data from GerritDec 26, 2013 1:16:49 AM com.sonyericsson.hudson.plugins.gerrit.gerritevents.GerritHandler runINFO: 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 项目,并设置了权限,普通用户是没有办法去 push 的,只能使用 git review 命令提交. 而 git review 命令需要 .gitreview 文件存在于项目目录里,用 admin@thstack.com 用户添加 .gitreview 文件
git clone git@gitlab.thstack.com:devgroup/openstack.gitcd openstackgit config user.name 'admin'git config user.email 'admin@thstack.com'
编辑 .gitreview 文件
vim .gitreview[gerrit]host=review.thstack.comport=29418project=openstack.git
添加到版本库
git add .gitreviewgit commit .gitreview -m 'Admin add .gitreview file.'git push origin master
Python 代码我使用了 testr,需要先安装 testr 命令
apt-get install python-pippip install testrepository
在 openstack 这个项目中添加 .testr.conf 文件
cd openstackvim .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 $IDOPTIONtest_id_option=--load-list $IDFILEtest_list_option=—list
提交到版本库中
git add .testr.confgit commit .testr.conf -m 'Admin add .testr.conf file'git push origin master
要知道 review 是在 gerrit 上,而 gerrit 上现在是没有项目的,想让 gitlab 上的项目能在 gerrit 上 review 的话,必须在 gerrit 上创建相同的项目,并有相同的仓库文件.
用 admin 用户在 Gerrit 上创建 openstack 项目
ssh -p 29418 admin@review.thstack.com gerrit create-project openstack
在 Gerrit 上 clone Gitlab 的 openstack 项目
cd /etc/gerrit/gitrm -fr openstack.gitgit clone --bare git@gitlab.thstack.com:devgroup/openstack.git
当用户 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/configHost gitlab.thstack.com: IdentityFile ~/.ssh/id_rsa PreferredAuthentications publickey
在 ~/.ssh/known_hosts 中,给 gitlab.thstack.com 添加 rsa 密钥
> ~/.ssh/known_hostsssh-keyscan -t rsa gitlab.thstack.com > ~/.ssh/known_hostsssh-keygen -H -f ~/.ssh/known_hosts
重新启动 Gerrit 服务
/etc/init.d/gerrit restart
Gerrit 的复制功能配置完毕,在 gerrit 文档中有一个 ${name} 变量用来复制 Gerrit 的所有项目,这里并不需要。如果有多个项目需要复制,则在 replication.config 中添加多个 [remote ....] 字段即可。务必按照上面步骤配置复制功能。
用 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’ -> 写入要测试的脚本 -> 保存 同时也可以选择 ‘添加构建后操作步骤’ 来邮件通知一个群组等功能。
切换到 longgeek 用户,之前创建了一个 longgeek 用户,方便测试
su - longgeekcd openstack # 之前添加过一个 testfile 文件,push 没有权限,所以用 git review 来代替 git pushgit pushgit reviewCreating a git remote called gerrit that maps to: ssh://longgeek@review.thstack.com:29418/openstack.gitYour change was committed before the commit hook was installedAmending the commit to add a gerrit change idremote: Processing changes: new: 1, refs: 1, done remote: remote: New Changes:remote: http://review.thstack.com/1remote: 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 openstackgit pullFrom gitlab.thstack.com:devgroup/openstack 70c59ff..7948aae master -> origin/masterAlready up-to-date.
如此便是一个完整的 CI 体系流程了,剩下的最重要的是如何用、真正的用起来。
转载地址:http://jguoi.baihongyu.com/