您的位置:首页 > 其它

Ubuntu之Gitlab、Gerrit、Jenkins协调工作配置

2016-02-18 15:34 851 查看

前提

创建邮箱 user1@vzjc.com

创建邮箱 jenkins@vzjc.com

Gitlab配置

Gitlab已经配置了root账户的邮箱为admin@vzjc.com

为Gitlab的root用户添加ssh密钥

系统上创建admin用户

$ sudo adduser admin
可以得到系统的用户名admin和用户自己设置的密码

系统上登录admin账户,生成SSH密钥

$ su - admin
$ ssh-keygen -C admin@vzjc.com
$ cat .ssh/id_rsa.pub
$ exit1234


在Gitlab系统上更新SSH公钥

用root账号登录
http://gitlab.zjc.com


点击页面右上角的Profile Settings - 点击左侧的SSH Keys小钥匙图标 - 点击
Add SSH Key
。在Key对应的输入框中输入上段落
$cat .ssh/id_rsa.pub
显示的公钥全文,点击Title,应该会自动填充为admin@vzjc.com



用了圈点Skitch,感觉萌萌哒

创建用户user1,作为一般用户

Gitlab上创建用户user1

打开http://gitlab.zjc.com, 如果当前页面为登录状态就退出登录

进入注册页面,输入注册信息,点击Sign up按钮来注册用户user1。


登录邮箱点击验证连接,在Gitlab上成功创建user1账号

Ubuntu系统上创建user1账号,生成SSH秘钥

$ sudo adduser user1
$ su - user1
$ ssh-keygen -C user1@vzjc.com
$ cat .ssh/id_rsa.pub
$ exit12345




用user1登录Gitlab,更新SSH公钥


Gitlab创建组和项目,并配置user1身份

用root登录Gitlab,创建新Group为dev-group




在dev-group中创建新项目test-project1




点击 test-project1 工程界面左上的 dev-group

点击
Members


把 user1 作为 Reporter 身份添加到组 dev-group (Reporter可以下载代码但是没上传权限)

用user1登录Gitlab,查看新项目test-project1的git地址

地址为
git@gitlab.zjc.com:dev-group/test-project1.git




系统上登录user1,克隆工程,测试权限

$ su - user1
$ cd ~
$ git clone git@gitlab.zjc.com:dev-group/test-project1.git
$ exit1234


$ su - user1
$ cd ~/test-project1/
$ git config --global user.name 'user1'$ git config --global user.email 'user1@vzjc.com'$ touch testfile
$ git add .
$ git commit -m 'user1 add testfile'$ git push
$ exit123456789






Gerrit配置

已经有了第一个账户admin并配置信箱为admin@vzjc.com

系统上登录admin,并获取上步创建的公钥

$ su - admin
$ cat ~/.ssh/id_rsa.pub
$ exit123


用admin账户登录gerrit,更新SSH公钥




为Gerrit系统创建用户user1并配置

使用htpasswd工具创建user1和密码
$ sudo -u gerrit -H htpasswd /home/gerrit/gerrit/etc/htpasswd.conf user1

使用user1登录gerrit,更新信箱为user1@vzjc.com
更新SSH公钥 (user1@zjc.com那个)

Jenkins配置

Jenkins系统已经创建了管理员账户jenkins并安装了Gerrit Trigger插件

配置jenkins用户

为Gerrit系统创建用户jenkins
$ sudo -u gerrit -H htpasswd /home/gerrit/gerrit/etc/htpasswd.conf jenkins

为系统用户jenkins创建ssh秘钥,并查看公钥
也可以用前面的
su - jenkins
登录后再操作。这样就不用
sudo -u jenkins -H
前缀了
$ sudo -u jenkins -H ssh-keygen -C jenkins@vzjc.com
$ sudo -u jenkins -H cat /home/jenkins/.ssh/id_rsa.pub



1

2

用jenkins登录Gerrit更新邮箱并验证zjc@zjc:~$ id jenkins
uid=122(jenkins) gid=131(jenkins) groups=131(jenkins)
zjc@zjc:~$ sudo vim /etc/dovecot/conf.d/10-mail.conf然后修改first_valid_uid这个值1

2

3

此处有个问题,如果在安装Jenkins系统前没有创建jenkins用户(安装时候系统会自动创建)并且使用的Dovecot来接收邮件并且用默认配置。那么要先查一下是否系统用户jenkins的uid是否大于500,如果不是,需要修改配置文件

用jenkins登录Gerrit,更新SSH公钥为系统用户jenkins的公钥

Jenkins系统的SMTP设置 (根据具体情况配置)

主页面->Manage Jenkins->Configure SystemJenkins Location ->System Admin e-mail address : jenkins@vzjc.com
E-mail Notification勾选Test configuration by sending test e-mail,然后在出现的框中填
admin@vzjc.com
,测试如果出错(好吧,如果用vzjc.com的邮件服务器是肯定会出错)
Unrecognized SSL message, plaintext connection
,则参考这个设置

SMTP server :
mail.vzjc.com


Default user e-mail suffix :
@vzjc.com


勾选
Use SMTP Authentication


User Name :
jenkins@vzjc.com


Password : 输入您设置的密码

勾选
Use SSL


SMTP Port :
587


Reply-To Address :
jenkins@vzjc.com


Charset :
UTF-8






设置Gerrit Trigger

主页面->Manage Jenkins->Gerrit Trigger->Add New ServerAdd New Server : Check4Gerrit

勾选 Gerrit Server With Default Configurations


Hostname : review.zjc.com

Frontend URL : http://review.zjc.com

Username : jenkins

E-mail : jenkins@vzjc.com


如果点击Test Connection后出现如图所示的错误
User jenkins has no capability to connect to Gerrit event stream.
,这是因为因Jenkins 没有权限监听 Gerrit 的 ‘Stream Events’,可如此配置点击Test Connection,出现的字符串为 success 则代表配置成功点击下方的保存按钮来保存设置

Gerrit 和 Jenkins 整合

让 Gerrit 支持 Jenkins

如果安装Gerrit时没有或者没有选择添加Verified标签功能[‘lable Verified’],需要自己添加
$ su - admin
$ git init cfg; cd cfg
$ git config --global user.name 'admin'$ git config --global user.email 'admin@vzjc.com'$ git remote add origin ssh://admin@review.zjc.com:29418/All-Projects
$ git pull origin refs/meta/config
$ vim project.config1234567
在文件末添加
[label "Verified"]    function = MaxWithBlock    value = -1 Fails    value =  0 No score    value = +1 Verified12345
$ git commit -a -m 'Updated permissions'$ git push origin HEAD:refs/meta/config12




用admin登录Gerrit

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

Projects -> Access -> Edit -> 找到 Reference: refs/heads/* 项 -> Add Permission -> Label Verified-> Group Name 里输入 Non-Interactive Users -> 回车 或者 点击Add 按钮 -> 在最下面点击 Save Changes 保存更改

Gitlab上设置
test-project1
工程

前面我们在Gitlab上搭建了一个 test-project1 的工程,普通用户是没有办法去 push 的,只能使用 git review 命令提交. 而 git review 命令需要 .gitreview 文件存在于项目目录里。

用 admin用户添加
.gitreview
文件

$ su - admin
$ git clone git@gitlab.zjc.com:dev-group/test-project1.git
$ cd test-project1
$ vim .gitreview1234
文件内容为:
[gerrit]host=review.zjc.comport=29418project=test-project1.git1234


添加
.gitreview
到版本库

$ git add .gitreview
$ git config --global user.name 'admin'$ git config --global user.email 'admin@vzjc.com'$ git commit .gitreview -m 'add .gitreview file by admin.'$ git push origin master
$ exit123456


用 admin用户添加
.testr.conf
文件

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

$ sudo apt-get install python-pip
$ sudo pip install testrepository12

test-project1
这个项目中添加
.testr.conf
文件

$ su - admin
$ cd test-project1
$ vim .testr.conf123
文件内容为:
[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=—list1234

提交到版本库中

$ git add .testr.conf
$ git commit .testr.conf -m 'add .testr.conf file by admin'$ git push origin master
$ exit1234


Gerrit上设置 test-project1工程

在 Gerrit 上创建 test-project1 项目

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

用 admin 用户在 Gerrit 上创建 test-project1 项目

$ su - admin
$ ssh -p 29418 admin@vzjc.com gerrit create-project test-project1
$ exit123


clone –bare Gitlab 上的仓库到 Gerrit

因为gerrit用户无访问gitlab的权限。所以要先看是否gerrit用户下已经存在了id_rsa密钥,如果没有则创建,然后把公钥加入到gitlab的admin账户上(这里加到 admin 账户上,是因为后面Gerrit系统还会有个复制 git 库到 Gitlab的功能,需要管理员权限)赋予gerrit用户访问 test-project1 的权限
$ sudo -u gerrit -H ls /home/gerrit/.ssh
$ sudo -u gerrit -H ssh-keygen -C gerrit@vzjc.com
$ sudo -u gerrit -H cat /home/gerrit/.ssh/id_rsa.pub123



用 root 登录Gitlab,把公钥添加到 admin 的SSH Key上 - 额,这是因为,Gerrit使用gerrit用户身份运行的,而Gerrit得git库复制功能需要权限复制到Gitlab上
gerrit
身份克隆 test-project1
$ cd /home/gerrit/gerrit/git
$ sudo -u gerrit -H rm -fr test-project1.git
$ sudo -u gerrit -H  git clone --bare git@gitlab.zjc.com:dev-group/test-project1.git123


同步 Gerrit 的 test-project1 项目到 Gitlab 上的 test-project1 项目目录中

当用户 git review 后,代码通过 jenkins 测试、人工 review 后,代码只是 merge 到了 Gerrit 的 test-project1 项目中,并没有 merge 到 Gitlab 的 test-project1 项目中,所以需要当 Gerrit test-project1 项目仓库有变化时自动同步到 Gitlab 的 test-project1 项目仓库中。Gerrit 自带一个 Replication 功能,同时我们在安装 Gerrit 时候默认安装了这个 Plugin。现在只需要添加一个 replication.config 给 Gerrit
$ sudo -u gerrit -H vim /home/gerrit/gerrit/etc/replication.config1
文件内容为:
[remote "test-project1"]  # Gerrit 上要同步项目的名字
projects = test-project1
url = git@gitlab.zjc.com:dev-group/test-project1.git
push = +refs/heads/*:refs/heads/*
push = +refs/tags/*:refs/tags/*
push = +refs/changes/*:refs/changes/*
threads = 312345678
设置gerrit用户的 ~/.ssh/config
$ sudo -u gerrit -H vim /home/gerrit/.ssh/config1
文件内容为:
Host gitlab.zjc.com:
IdentityFile ~/.ssh/id_rsa
PreferredAuthentications publickey123
在gerrit用户的~/.ssh/known_hosts 中,给 gitlab.zjc.com 添加 rsa 密钥
$ sudo -u gerrit -H sh -c "ssh-keyscan -t rsa gitlab.zjc.com >> /home/gerrit/.ssh/known_hosts"$ sudo -u gerrit -H sh -c "ssh-keygen -H -f /home/gerrit/.ssh/known_hosts"12
重新启动 Gerrit 服务
$ sudo /etc/init.d/gerrit restart1
Gerrit 的复制功能配置完毕在 gerrit 文档中有一个 ${name} 变量用来复制 Gerrit 的所有项目,这里并不需要。如果有多个项目需要复制,则在 replication.config 中添加多个 [remote ….] 字段即可。务必按照上面步骤配置复制功能。

在 Jenkins 上对 test-project1 项目创建构建任务

Jenkins上安装git插件

安装 Git Plugin

用 jenkins 登录 http://jenkins.zjc.com

Manage Jenkins -> Manage Plugins -> Avalabile -> 安装 Git Plugin

Jenkins上创建项目

添加 test-project1工程

用 jenkins 登录 Jenkins
http://jenkins.zjc.com


New Item页面设置

Item name : test-project1

Freestyle project



Git页面设置

To get the Git Plugin to download your change; set Refspec to $GERRIT_REFSPEC and the Choosing strategy to Gerrit Trigger. You may also need to set 'Branches to build' to $GERRIT_BRANCH.   If this does not work for you set Refspec to refs/changes/*:refs/changes/* and 'Branches to build' to $GERRIT_REFSPEC
Note: Be aware that $GERRIT_BRANCH and $GERRIT_REFSPEC are not set in the Ref Updated case. If you want to trigger a build, you can set Refspec and 'Branches to build' to $GERRIT_REFNAME.

Source Code Management : Git

Respostories : http://review.zjc.com:8082/p/test-project1.git

Branches to build : origin/$GERRIT_BRANCH

Build Triggers : Gerrit event



按下文来配置,上面的无效 - 来自 这里

这里 Choose a Server 选的是 Any Server 因为只有一个服务器 Check4Gerrit, 所以要确保这个服务器是Enable 状态。可以去服务器那里去检查一下看看



Gerrit Event页面设置

Pattern : test-project1

Branches : master

Trigger on : 点击 Add 分别添加 Patchset Created 和 Draft Published

Dynamic Trigger Configuration



Build页面设置

Add build step -> Execute shell
cd $WORKSPACE

[ ! -e .testrepository ] && testr init

testr run




测试

系统上 用 user1 提交一个更改

用 user1 登录

删除目录 test-project1

克隆 test-project1 工程

进入 test-project1 目录

添加文件、提交

git review
增加 review 到Gerrit

$ su - user1
$ rm -rf test-project1/
$ git clone git@gitlab.zjc.com:dev-group/test-project1.git
$ cd test-project1/
$ touch testfile
$ git add .
$ git commit -m 'add by user1'$ git review
$ exit123456789
我的环境还有一个问题 第一次
git review
没有问题,但是如果用
git commit --amend
修改了提交信息,再次
git reivew
机会出现
error: unpack failed: error Missing tree
的错误,折中解决方案看这里









用 admin 登录 Gerrit 系统,通过验证,提交






登录 Gitlab

查看 test-project1 工程,可以看到新增加文件操作已经同步过来了

参考

http://longgeek.com/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  密码 用户名 钥匙