环境:html
gerrit server :176.23.47.118nginx
管理员机器用户名: su admingit
gerrit 的管理员帐号:admingithub
gerrit 工做流程
gerrit工做流程的图:web
使用过git的同窗,都知道,当咱们git add --> git commit --> git push
以后,你的代码会被直接提交到repo,也就是代码仓库中,就是图中橘红色箭头指示的那样。shell
那么gerrit就是上图中的那只鸟,普通成员的代码是被先push到gerrit服务器上,而后由代码审核人员,就是左上角的integrator在web页面进行代码的审核(review),能够单人审核,也能够邀请其余成员一同审核,当代码审核经过(approve)以后,此次代码才会被提交(submit)到代码仓库(repo)中去。vim
不管有新的代码提交待审核,代码审核经过或被拒绝,代码提交者(Contributor)和全部的相关代码审核人员(Integrator)都会收到邮件提醒。
gerrit还有自动测试的功能,和主线有冲突或者测试不经过的代码,是会被直接拒绝掉的,这个功能彷佛就是右下角那个老头(Jenkins)的任务。api
整个流程就是这样。 在使用过程当中,有两点须要特别注意下:服务器
- 当进行commit时,必需要生成一个Change-Id,不然,push到gerrit服务器时,会收到一个错误提醒。
-
提交者不能直接把代码推到远程的master主线(或者其余远程分支)上去。这样就至关于越过了gerrit了。 gerrit必须依赖于一个
refs/for/*
的分支。app假如咱们远程只有一个master主线,那么只有当你的代码被提交到
refs/for/master
分支时,gerrit才会知道,我收到了一个须要审核的代码推送,须要通知审核员来审核代码了。
当审核经过以后,gerrit会自动将这条分支合并到master主线上,而后邮件通知相关成员,master分支有更新,须要的成员再去pull就行了。并且这条refs/for/master
分支,是透明的,也就是说普通成员实际上是不须要知道这条线的,若是你正确配置了sourceTree,你也应该是看不到这条线的。
这两点很重要!!这两点很重要!!这两点很重要!!
为管理员admin配置SSH Public Keys:
第一个Gerrit 帐户自动成为权限最高的管理员,由于Gerrit 安装完毕后第一件事就是当即注册或者登陆,以便初始化管理员帐号
这里须要把你的公钥钥匙拷贝出来,而后粘贴在对话中
生成公私钥对
首先切换到 admin用户下 ,在命令行中输入:
[root@localhost home]# su admin
[admin@localhost home]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/admin/.ssh/id_rsa):
Created directory '/home/admin/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/admin/.ssh/id_rsa.
Your public key has been saved in /home/admin/.ssh/id_rsa.pub.
找到公钥钥匙:
默认公私钥的是放置在~/.ssh
目录下的,默认的名称是id_rsa
和id_rsa.pub
。其中.pub
文件就是公钥,私钥你本身须要保存好。咱们要作的,就是把公钥的内容拷贝出来,而后粘贴到页面上去。注意拷贝时,要从RSA开始拷贝,若是格式不对,页面上会有提示。拷贝完成以后以下图所示:
测试SSH key是否提交成功,在client 服务器上执行如下命令:
#ssh -p 29418 admin@ip地址
ps:必定要切换到你生成 公钥私钥的用户下 gerrit, 29418为gerrit上SSH服务器监听的端口,admin为登陆的gerrit帐号(admin 帐号为个人管理员帐号),而且该帐号已经登陆了gerrit服务器,并且公钥已经上传。
176.23.47.118为gerrit服务器的IP
[root@localhost .ssh]# su gerrit [gerrit@localhost .ssh]$ ssh -p 29418 admin@176.23.47.118 **** Welcome to Gerrit Code Review **** Hi admin, you have successfully connected over SSH. Unfortunately, interactive shells are disabled. To clone a hosted Git repository, use: git clone ssh://admin@localhost:29418/REPOSITORY_NAME.git
在gerrit服务器上建立普通用户:
Linux下的用户名须和管理员给的Gerrit帐号名相同,若是没有,请添加新用户:
Gerrit服务器使用的是HTTP认证类型,并用nginx作反向代理,建立用户使用如下命令:
[root@localhost .ssh]# htpasswd -m /home/gerrit/gerrit.password tana New password: Re-type new password: Updating password for user tana
建立普通用户 tana ,在gerrit网页登陆进去,按照配置admin的路径 给tana用户配置ssh keys。
[tana@localhost ~]$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/tana/.ssh/id_rsa): Created directory '/home/tana/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/tana/.ssh/id_rsa. Your public key has been saved in /home/tana/.ssh/id_rsa.pub.
配置成功后下图所示:
管理员访问SSH的管理接口
在gerrit我的配置界面中设置了公钥以后,就能够链接Gerrit的ssh服务器执行命令。注意远程gerrit服务器的端口不要被防火墙拦截,gerrit的ssh服务器使用的特殊的端口,默认是29418,任何用户均可以经过 ssh链接执行 ls-projects 命令查看项目列表
[tana@localhost root]$ ssh -p 29418 176.23.47.118 gerrit ls-projects All-Projects All-Users projec1 test3
经过ssh 建立新项目:
管理员admin 可使用命令行建立新项目 test3:
[admin@localhost root]$ ssh -p 29418 176.23.47.118 gerrit create-project test3
建立group并建立一个project
图片是gerrit默认的分组,以后咱们还能够创建新的分组,加入新的成员
注意,只有管理员组的用户才能建立group和Project
建立一个group
登陆Gerrit管理员帐号admin,建立一个新的组为test1。并将tana帐号加入该组
建立一个project
建立一个project1 :BROWSE->Repositores->CREATE NEW
BROWSE->Repositores->Access->ADD REFERENCE->ADD
tana用户clone工程
clone工程到本地
切换到tana用户上,下拉刚刚建立的 projec1工程:
执行命令:#git clone ssh://tana@176.23.47.118:29418/projec1.git
[tana@localhost ~]$ git clone "ssh://tana@172.23.57.119:29418/project5" && scp -p -P 29418 tana@172.23.57.119:hooks/commit-msg "project5/.git/hooks/"
Initialized empty Git repository in /home/tana/project5/.git/
remote: Counting objects: 2, done
remote: Finding sources: 100% (2/2)
remote: Total 2 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (2/2), done.
commit-msg 100% 1791 1.8KB/s 00:00
[tana@localhost ~]$ ls
project5
进入到/home/tana/中,咱们发现多了一个 projec1的文件夹。这个文件夹就是咱们下拉下来的projec1 工程了
下面新建一个 test.md文件 ,尝试着提交:
提交
首先咱们经过下面两个命令首先commit到本地仓库:
[tana@localhost ~]$ cd /home/tana/projec1/ [tana@localhost projec1]$ vim test.md just for test "test.md" [New] 1L, 14C written [tana@localhost projec1]$ git add test.md [tana@localhost projec1]$ git commit
咱们把此次提交命名为 test 20190419 commit
而后咱们经过git push
命令来推送,发送推送失败了,错误以下:
咱们设置了push命令以后,从新进行push,结果报错了。。此次是报 邮箱没有注册,下面咱们来配置邮箱。
配置邮箱
修改gerrit.config配置文件
注意: smtppass是邮箱的受权码密码,不是你的登陆密码,受权码须要在邮件网页端登陆设置,容许第三方客户端登陆
from 是发送注册邮箱的
用户绑定邮箱
登陆到 gerrit上面去,settings->Email Addresses 去设置注册邮箱:
输入你的邮箱地址,注意每一个用户的邮箱地址都是惟一的,不能多个用户用同一个邮箱注册
而后登陆邮箱,点击连接进行注册:
点击连接注册成功之后,咱们能够在 settings->Profile 看到有邮箱地址了
配置commit 的名称和email
在commit的时候,发现提交者的名称和email都是错误的,咱们须要先配置成咱们当前的tana用户,以及对应的email
[tana@localhost projec1]$ git config user.name tana [tana@localhost projec1]$ git config user.email ytyt@cypress.com
经过git config --list
来查看一下当前git仓库的配置,发现已经把用户名和密码正确设置了。
而后咱们经过git push
命令来推送:
Gerrit拒绝了咱们直接提交到master的推送!
这就是咱们在文章开头提到的问题,咱们须要push到refs/for/master那条线上!!
咱们在命令行写入下面的命令:
git config remote.origin.push refs/heads/*:refs/for/*
这行命令的意思是,当执行push
命令时,将会推送到refs/for/当前head所在的分支
上。
而后再执行git push命令:
此次的错误是:
这个是提到的第2个问题,commit必定要有Change-Id
而后咱们看到了命令行中给了咱们提示,咱们能够从hooks文件中拷贝commit-msg文件下来,这样commit时,会自动帮咱们生成Change-Id
.
咱们在命令行写入下面的命令:
咱们能够看一下 git rev-parse --git-dir
就是指向的当前git配置的文件,就是.git
文件夹
因此咱们直接用scp命令从gerrit服务器上拉取当前用户的hooks文件。
注意这个命令要在project5文件夹上一层目录作:/home/tana
[tana@localhost ~]$ scp -p -P 29418 tana@172.23.57.119:hooks/commit-msg "project5/.git/hooks/"
commit-msg 100% 1791 1.8KB/s 00:00
而后后咱们从新push发现同样的错误,由于咱们还停留在上次commit,上次的commit是没有生成Change-Id
的! 可是咱们回退一下,而后从新提交。
回退命令是先用git log
找到上一次的commit id, 而后用git reset --hard 找到的id
命令回退,仍是不行,不知道什么缘由,我把整个工程删除,直接在 clone的时候就拉取当前用户的hooks文件。