谷歌的 Android 开源项目在 Git 的使用上有两个重要的创新,一个是为多版本库协同而引入的 repo,另外一个重要的创新就是 Gerrit —— 代码审核服务器。Gerrit 为 git 引入的代码审核是强制性的,就是说除非特别的授权设置,向 Git 版本库的推送(Push)必须要经过 Gerrit 服务器,修订必须经过代码审核的一套工作流之后,才可能经批准并纳入正式代码库中。

Gerrit工作原理和流程

首先贡献者的代码通过 git 命令(或git review封装)推送到 Gerrit 管理下的 Git 版本库,推送的提交转化为一个一个的代码审核任务,审核任务可以通过 refs/changes/下的引用访问到。代码审核者可以通过 Web 界面查看审核任务、代码变更,通过 Web 界面做出通过代码审核或者打回等决定。测试者也可以通过 refs/changes/引用获取(fetch)修订对其进行测试,如果测试通过就可以将该评审任务设置为校验通过(verified)。最后经过了审核和校验的修订可以通过 Gerrit 界面中提交动作合并到版本库对应的分支中。更详细的流程描述见下图所示: 

创建gerrit用户

 sudo adduser gerrit
#给用户添加sudo权限
chmod u+w /etc/sudoers
sudo vi /etc/sudoers
#在root ALL=(ALL) ALL添加下面一行
gerrit ALL=(ALL) ALL
su gerrit

Gerrit安装与配置

安装Gerrit需要装有最低1.6版本的JDK:

sudo apt-get install default-jre
sudo apt-get install git

https://code.google.com/p/gerrit/ 
https://gerrit-releases.storage.googleapis.com/gerrit-2.12.war

安装Gerrit

java -jar gerrit-2.11.war init -d review_site
*** Git Repositories
***
Location of Git repositories [git]: /home/gerrit/repositories *** SQL Database
*** Database server type [h2]: postgresql
Server hostname [localhost]:
Server port [(postgresql default)]:
Database name [reviewdb]:
Database username [gerrit]:
gerrit's password :
confirm password : *** User Authentication
***
Authentication method [OPENID/?]: http
Get username from custom HTTP header [y/N]?
SSO logout URL *** Review Labels
*** Install Verified label [y/N]? y *** Email Delivery
*** SMTP server hostname [localhost]: smtp.163.com
SMTP server port [(default)]: 25
SMTP encryption [NONE/?]:
SMTP username [gerrit]: your_name
gerrit's password :
confirm password : *** SSH Daemon
***
Listen on address [*]:
Listen on port [29418]:
*** HTTP Daemon
*** Behind reverse proxy [y/N]y
Use SSL (https://) [y/N]?
Listen on address [*]:
Listen on port [8080]: 8081
Canonical URL [http://learnLinux:8081/]: http://localhost:8080 *** Plugins
*** Installing plugins.
Install plugin download-commands version v2.11 [y/N]? y
Install plugin reviewnotes version v2.11 [y/N]? y
Install plugin singleusergroup version v2.11 [y/N]? y
Install plugin replication version v2.11 [y/N]? y
Install plugin commit-message-length-validator version v2.11 [y/N]? y
Initializing plugins.
  • 59

Gerrit支持H2(内置) / MySQL / PostgreSQL数据库,简单使用默认数据库H2,mysql和postgreSQL数据库在认证人数比较多时选用. 
Gerrit支持OpenID / HTTP / LDAP, 认证方式没有选择OpenId, 而是http, 因为这样会使得gerrit对外部系统有依赖, 目前gerrit支持google和yahoo提供的openid. 
选择http需要反向代理支持, 这和http认证有关. 
LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP 
配置文件review_site/etc/gerrit.config,邮箱密码存在review_site/etc/secure.config文件中.

vi ./review_site/etc/gerrit.config
#将canonicalWebUrl修改为代理服务器地址
[gerrit]
basePath = /home/gerrit/repositories
canonicalWebUrl = http://localhost:8090/
[database]
type = postgresql
hostname = localhost
database = reviewdb
username = gerrit
[index]
type = LUCENE
[auth]
type = HTTP
[sendemail]
enable = true
smtpServer = smtp.163.com
smtpServerPort = 25
smtpUser = your_name@163.com
from = gerrit<your_name@163.com>
[sshd]
listenAddress = *:29418
[httpd]
listenUrl = proxy-http://*:8081/
[cache]
directory = cache
vi etc/secure.config
[database]
password = your_password
[auth]
registerEmailPrivateKey = your_password
restTokenPrivateKey = your_password
[sendemail]
smtpPass = your_password

配置nginx代理服务器

nginx作为代理服务器更加方便,在/etc/nginx/sites-enabled添加一个server模块

server {
listen *:8090;
server_name localhost;
location / {
auth_basic "Welcomme to Gerrit Code Review Site";
#确保passwd路径正确
auth_basic_user_file /home/gerrit/review_site/etc/passwd;
proxy_pass http://localhost:8081;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
}
location /login/ {
proxy_pass http://localhost:8081;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
}
}
touch ./review_site/etc/passwd
#添加gerrit账号
htpasswd -b ./review_site/etc/passwd yourname yourpassword
#重启gerrit,账号才会生效
./review_site/bin/gerrit.sh restart

账户配置

第一次成功登录的用户会被gerrit作为管理员用户。登录后点击右上角的”匿名懦夫”Anonymous Coward -> Settings来配置账户。 
 
添加SSH公钥 
要使用gerrit必须要提供用户的公钥。选择页面左侧的SSH Public Keys为当前用户添加公钥。直接将公钥粘贴到Add SSH Public Key框内,然后点击add即可。

添加其他普通账户

如果采用http认证,那么添加其他账户时,需要现添加http认证账户。用htpasswd创建的用户时,并没有往gerrit中添加账号,只有当该用户通过web登陆gerrit服务器时,该账号才会被添加进gerrit数据库中。

为什么不能Sign Out

也行你会发现用gerrit+HTTP认证,通过web登陆后,点击右上角的Sign Out无法登出。要么是依然保持登陆的状态,要么就是直接出错。 
不要以为怎么了,其实这是正常现象,以下这段话是从网上看到的:You are using HTTP Basic authentication. There is no way to tell abrowser to quit sending basic authentication credentials, to logout with basicauthentication is to close the Webbrowser.

SSH访问

#默认使用.ssh/id_rsa.pub公钥
ssh -p 29418 -i admin@localhost **** 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@learnLinux:29418/REPOSITORY_NAME.git Connection to localhost closed.

git仓库

新建一个gerritRepo仓库,git clone http://127.0.0.1:8080/gerritRepo 
在推送时

remote: Unauthorized
fatal: Authentication failed for 'http://admin@127.0.0.1:8080/gerritRepo/'

改用ssh方式push

git remote remove origin
git remote add origin ssh://admin@127.0.0.1:29418/gerritRepo
git push origin master

将commit提交到服务器接受代码审核。

remote: Branch refs/heads/master:
remote: You are not allowed to perform this operation.
remote: To push into this reference you need 'Push' rights.
remote: User: member
remote: Please read the documentation and contact an administrator
remote: if you feel the configuration is incorrect
remote: Processing changes: refs: 1, done
To ssh://member@127.0.0.1:29418/hello1
! [remote rejected] master -> master (prohibited by Gerrit)
error: 无法推送一些引用到 'ssh://member@127.0.0.1:29418/hello1'

这就是gerrit的精髓所在了。原因是gerrit不允许直接将本地修改同步到远程仓库。客户机必须先push到远程仓库的refs/for/*分支上,等待审核。这也是为什么我们需要使用gerrit的原因。gerrit本身就是个代码审核工具。

提交changes

gerrit项目分支权限

#提交master分支
git push origin HEAD:refs/for/master
#提交所有分支
git push origin refs/heads/*:refs/for/*
#修改.git/config文件,添加push时的引用
[remote "origin"]
url = ssh://chenjianhua@127.0.0.1:29418/hello1
fetch = +refs/heads/*:refs/remotes/origin/*
push = HEAD:refs/for/*

下载hook

再次推送到服务器

remote: Processing changes: refs: 1, done
remote: ERROR: missing Change-Id in commit message footer
remote:
remote: Hint: To automatically insert Change-Id, install the hook:
remote: gitdir=$(git rev-parse --git-dir); scp -p -P 29418 root@ubuntu:hooks/commit-msg ${gitdir}/hooks/
remote: And then amend the commit:
remote: git commit --amend
remote:
To ssh://member@127.0.0.1:29418/hello1
! [remote rejected] master -> refs/for/master (missing Change-Id in commit message footer)
error: 无法推送一些引用到 'ssh://member@127.0.0.1:29418/gerritRepo'

push时提示需要Change-Id在提交信息中, 需要从gerrit server上下载一个脚本 
钩子的目的是在提交信息中自动创建 ‘Change-Id:’ 标签

scp -p -P 29418 admin@127.0.0.1:hooks/commit-msg gerritRepo/.git/hooks/
#修改上次提交记录,或者再次提交修改
git commit --amend
remote: Processing changes: new: 1, refs: 1, done
remote:
remote: New Changes:
remote: http://localhost:8081/2 vi README
remote:
To ssh://member@127.0.0.1:29418/gerritRepo
* [new branch] master -> refs/for/master

审查代码

配置项目权限

 
 
给refs/head/*分支Label Verified权限添加用户分组,这里分配Administrators组. 
项目评审过程中,需要几个条件,代码才能最终提交到分支

  • Review >=+2
  • Verify >=+1

评审过程通常有三个人参与,代码提交,代码验证(Verify),代码审查(Review). 通常由自动测试工具jenkins完成代码验证(Verify).

Needs Verified , Needs Code-Review

验证和审查通过后,显示Ready to Submit状态,现在就可以合并代码到head/*分支中

查看合并结果

jenkins自动验证

patch补丁集

开发者的代码需要先提交到refs/for/master分支上,变动的代码称作补丁集,保存在 refs/changes/* 命名空间下.

git ls-remote
From ssh://admin@localhost:29418/gerrit_ci
5f8ed98b0f88787c22e705595e2818db62874f56 HEAD
eeaef9da4ea27d7c23bfb5f9a2ed1b5357ebbba8 refs/changes/01/1/1
5f8ed98b0f88787c22e705595e2818db62874f56 refs/changes/02/2/1
bfdb700f4aab3afc32ec79a29b0e25f8be758f8f refs/changes/03/3/1
5f8ed98b0f88787c22e705595e2818db62874f56 refs/heads/master
887107fcb25c48d1a1eb116ec466fc4f9b298a5c refs/meta/config
21be8fce8a38d9437363128d214739c64bdd5710 refs/notes/review #下载补丁
git fetch ssh://admin@localhost:29418/gerrit_ci refs/changes/03/3/1

Draft草案

Topic主题

使用postgreSQL数据库

安装postgreSQL

sudo apt-get install postgresql
#次安装后,会默认生成名为postgres的Linux系统用户、数据库和数据库用户(作为数据库管理员),首先修改postgres数据库用户的密码,然后增加Gerrit需要的数据库
#切换到postgres用户
sudo su postgres
#登录postgres数据库
psql postgres
#修改postgres用户登录密码
ALTER USER postgres with PASSWORD 'password'
#输入密码
postgres=#
#输入第二遍密码
postgres=# \q #创建gerrit用户
CREATE USER gerrit WITH PASSWORD 'password';
#创建数据库
CREATE DATABASE reviewdb OWNER gerrit;
#将reviewdb所有权限赋予gerrit
GRANT ALL PRIVILEGES ON DATABASE reviewdb to gerrit;
#vi etc/gerrit.config
[database]
type = postgresql
hostname = localhost
database = reviewdb
username = gerrit
#vi etc/secure.config
[database]
password = password

使用mysql数据库

#连接数据库
mysql -u root -p
#查看帮助
help contents;
help Administration;
#创建gerrit用户和reviewdb数据库
CREATE USER 'git'@'localhost' IDENTIFIED BY 'git';
CREATE DATABASE reviewdb;
ALTER DATABASE reviewdb charset=latin1;
GRANT ALL ON reviewdb.* TO 'git'@'localhost';
FLUSH PRIVILEGES;
#查看所有数据库
SHOW DATABASES;
#查看所有用户
SELECT DISTINCT CONCAT('User: ''',user,'''@''',host,''';') AS query FROM mysql.user;
*** SQL Database
*** Database server type [h2]: mysql Gerrit Code Review is not shipped with MySQL Connector/J 5.1.21
** This library is required for your configuration. **
Download and install it now [Y/n]? y
Downloading http://repo2.maven.org/maven2/mysql/mysql-connector-java/5.1.21/mysql-connector-java-5.1.21.jar ... OK
Checksum mysql-connector-java-5.1.21.jar OK
Server hostname [localhost]:
Server port [(mysql default)]: 3306
Database name [reviewdb]: reviewdb
Database username [gerrit]: gerrit
gerrit's password :
confirm password :

也可以将mysql-connector-Java-5.1.21.jar放入lib目录下

ubuntu搭建Gerrit代码审核服务器的更多相关文章

  1. Gerrit代码审核服务器搭建全过程

    Gerrit代码审核服务器搭建全过程 转载请标明出处:http://blog.csdn.net/ganshuyu/article/details/8978614 环境:Ubuntu12.xx 1.建立 ...

  2. gerrit代码审核工具之“error unpack failed error Missing unknown”错误解决思路

    使用gerrit代码审核工具时遇到error: unpack failed: error Missing unknown d6d7c89bd1d77f44c5c8e99437aaffbfc0684e7 ...

  3. ubuntu搭建gerrit+gitweb代码审核系统

    一.Gerrit的简介 Gerrit是Google开源的一套基于web的代码review工具,它是基于git的版本管理系统.Google开源Gerrit旨在提供一个轻量级框架,用于在代码入库之前对每个 ...

  4. 实战搭建SVN代码版本服务器

    前言:公司要求搭建一台SVN代码版本管理服务器,用于管理所有代码资产: 项目架构图 1.环境安装 [root@host_centos ~]#yum –y install subversion mod_ ...

  5. 最新ubuntu搭建公网个人邮件服务器(基于postfix,dovecot,mysql)

      最近做了一个应用,需要用邮件发通知,但是免费的邮箱每天发信数量是有限制的,所以呢就想着搭建一个自己的邮件服务器,能够实现邮件的发送和接收即可,其中大概花了一个星期找资料,测试,终于成功了,写个教程 ...

  6. ubuntu搭建、安装gitlab服务器以及初始化密码

    本为14.04 在搭建之前要确定其网络环境是没有问题.用root身份进行操作 1.安装和配置必要的依赖关系 apt-get update apt-get install -y curl openssh ...

  7. 搭建phabricator代码审核工具

    phabricator 依赖环境 系统centos,mysql,php,nginx 1.下载安装脚本 https://secure.phabricator.com/source/phabricator ...

  8. gerrit代码简单备份方案分享

    由于前期部署了gerrit代码审核系统,开发调整后的线上代码都放到gerrit上,这就要求我们要保证代码的安全.所以,对gerrit代码的备份至关重要! 备份的策略是:1)先首次将gerrit项目代码 ...

  9. [阿里云部署] Ubuntu+Flask+Nginx+uWSGI+Mysql搭建阿里云Web服务器

    部署地址:123.56.7.181 Ubuntu+Flask+Nginx+uWSGI+Mysql搭建阿里云Web服务器 这个标题就比之前的"ECS服务器配置Web环境的全过程及参考资料&qu ...

随机推荐

  1. hihoCoder#1141 二分·归并排序之逆序对

    原题地址 又是一道WA成狗的题,最后发现原来是结果溢出了.. 代码: #include <iostream> #include <cstring> using namespac ...

  2. Can you answer these queries(spoj 1043)

    题意:多次查询区间最长连续字段和 /* 用线段树维护区间最长子段和,最长左子段和,最长右子段和. */ #include<cstdio> #include<iostream> ...

  3. BZOJ2059: [Usaco2010 Nov]Buying Feed 购买饲料

    数轴上n<=500个站可以买东西,每个站位置Xi,库存Fi,价格Ci,运东西价格是当前运载重量的平方乘距离,求买K<=10000个东西到达点E的最小代价. f[i,j]--到第i站不买第i ...

  4. maven 编译出错 Failed to execute goal org.apache.maven.plugins:maven-clean-plugin:2.5:clean

    eclipse在使用maven的tomcat控件编译java程序时,报错 Failed to execute goal org.apache.maven.plugins:maven-clean-plu ...

  5. django学习之- 动态验证码学习

    实例:通过前台和后台,实现用户登录页面动态图片验证码校验,图片验证码部分使用Pillow模块实现,作为单独学习部分记录. 前端: <!DOCTYPE html> <html lang ...

  6. 大数c++模板 超级好用

    只用输入用cin 输出  cout  每个数学符号都可以用   超级强大 #include <iostream> #include <queue> #include <c ...

  7. CD-----UVa624(01背包+输出路径)

      CD  You have a long drive by car ahead. You have a tape recorder, but unfortunately your best musi ...

  8. zookeeper原理浅析(二)

    参考:https://www.cnblogs.com/leocook/p/zk_1.html 代码:https://github.com/littlecarzz/zookeeper 1. 数据模型 1 ...

  9. swiper插件制作轮播图swiper2.x和3.x区别

    swiper3.x仅仅兼容到ie10+.比較适合移动端. swiper3.x官网  http://www.swiper.com.cn/ swiper2.x能够兼容到ie7+.官网是http://swi ...

  10. ScrollView阻尼效果

    activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...