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

前提

  • 建立邮箱 user1@vzjc.compython

  • 建立邮箱 jenkins@vzjc.comgit

Gitlab配置

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

为Gitlab的root用户添加ssh密钥

系统上建立admin用户

$ sudo adduser adminvim

能够获得系统的用户名admin和用户本身设置的密码服务器

系统上登陆admin帐户,生成SSH密钥

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

这里写图片描述

在Gitlab系统上更新SSH公钥

  1. 用root帐号登陆http://gitlab.zjc.comssh

  2. 点击页面右上角的Profile Settings - 点击左侧的SSH Keys小钥匙图标 - 点击Add SSH Key。在Key对应的输入框中输入上段落$cat .ssh/id_rsa.pub显示的公钥全文,点击Title,应该会自动填充为admin@vzjc.com 
    这里写图片描述
    用了圈点Skitch,感受萌萌哒ide

建立用户user1,做为通常用户

Gitlab上建立用户user1

  1. 打开http://gitlab.zjc.com, 若是当前页面为登陆状态就退出登陆工具

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

    这里写图片描述

  3. 登陆邮箱点击验证链接,在Gitlab上成功建立user1帐号post

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并配置

  1. 使用htpasswd工具建立user1和密码

    $ sudo -u gerrit -H htpasswd /home/gerrit/gerrit/etc/htpasswd.conf user1

  2. 使用user1登陆gerrit,更新信箱为user1@vzjc.com

  3. 更新SSH公钥 (user1@zjc.com那个)

Jenkins配置

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

配置jenkins用户

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

  2. 为系统用户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 System

  • Jenkins 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 Server

  • Add 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.


  • 这里 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 提交一个更改

  1. 用 user1 登陆

  2. 删除目录 test-project1

  3. 克隆 test-project1 工程

  4. 进入 test-project1 目录

  5. 添加文件、提交

  6. 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/

相关文章
相关标签/搜索