ubuntu14 搭建gerrit服务器及使用说明

Gerrit安装使用指导

 

 

 

简介:本指导主要分三部分组成,一是Gerrit的安装配置,二是本地git的修改提交到Gerrit仓库,三是代码审查说明。

 

 

 

一、依赖组件

Gerrit依赖的组件主要有以下四个:

 

1、        jdk    --Gerrit主要由java实现其功能,所以需要jdk支持;

sudo apt-get install openjdk-7-jdk

查看版本java -version

2、        mysql    --Gerrit审查过程中存储信息的数据库,Gerrit本身自带H2数据库一般不用;

安装mysql

sudo apt-get install mysql-server

sudo apt-get install mysql-client

sudo apt-get install libmysqlclient-dev

3、        Apache2    --主要用到其反向代理功能;

sudo apt-get install apache2

  1.    Gerrit本身相当于是一个Git服务器。

wget https://gerrit-releases.storage.googleapis.com/gerrit-2.12.war

 

  • gerrit安装

 

  1. 添加gerrit用户

sudo adduser gerrit

sudo passwd gerrit

并将gerrit加入sudo权限

vim /etc/sudoers

增加gerrit  ALL=(ALL:ALL) ALL

 

  1. 安装gerrit 到/home/gerrit/review

java -jar gerrit-2.12.4.war init --batch -d ~/review

 

3、配置gerrit   vim /home/gerrit/review/etc/gerrit.config

 [gerrit]

        basePath = git

        #basePath = /home/git/repositories

        canonicalWebUrl = http://192.168.7.209:8080/

[database]

        type = h2

        database = /home/gerrit/review/db/ReviewDB

[index]

        type = LUCENE

[auth]

        type = HTTP

[receive]

        enableSignedPush = false

[oauth]

        allowEditFullName = true

        allowRegisterNewEmail = true

[sendemail]

        #smtpServer = localhost

        enable = true

        smtpServer = smtp.qiye.163.com

        smtpServerPort = 465

        smtpEncryption = SSL

        sslVerify = true

        smtpUser = [email protected]

        smtpPass = xxx

        from = [email protected]

[container]

        user = gerrit

        javaHome = /usr/lib/jvm/java-7-openjdk-amd64/jre

[sshd]

        listenAddress = *:29418

[httpd]

        listenUrl = http://*:8081/

[cache]

        directory = cache

 

  • 配置apache2反向代理

1、配置监听端口

vim /etc/apache2/ports.conf

Listen 80

Listen 808

2、配置反向代理:

cd /etc/apache2/sites-available/

sudo ln -s ../sites-available/000-default.conf

Vim 000-default.conf配置如下

 

<VirtualHost *:8080>

    ServerName 192.168.7.209

    ProxyPreserveHost On

    ProxyRequests Off

    <Proxy *>

        Order deny,allow

        Allow from all

    </Proxy>

    <Location />

      AuthType Basic

      AuthName "Welcomme to Gerrit Code Review Site!"

      Require valid-user

      AuthUserFile /etc/apache2/passwords

    </Location>

    ProxyPass / http://192.168.7.209:8081/

    proxyPassReverse / http://127.0.0.1:8081/

 

    ServerAdmin [email protected]

    DocumentRoot /var/www/html

    ErrorLog ${APACHE_LOG_DIR}/error.log

    CustomLog ${APACHE_LOG_DIR}/access.log combined

 

</VirtualHost>

 

 

3、需要WEB访问的话,使用htpasswd,htpasswd命令是Apache的Web服务器内置工具,用于创建和更新储存用户名、域和用户基本认证的密码文件。

cd /home/gerrit/review

touch /home/gerrit/review/passwords

htpasswd -b /etc/apache2/passwords admin 123456(管理员)

htpasswd -b /etc/apache2/passwords gerrit 123456

 

4、开启反向代理

cd /etc/apache2/mods-enabled
sudo ln -s ../mods-available/proxy.load

sudo ln -s ../mods-available/proxy.conf

sudo ln -s ../mods-available/proxy_http.load

sudo ln -s ../mods-available/proxy_balancer.conf

sudo ln -s ../mods-available/proxy_balancer.load

sudo ln -s ../mods-available/rewrite.load

sudo ln -s ../mods-available/ssl.conf

sudo ln -s ../mods-available/ssl.load

 

  1. 重启服务

sudo /home/gerrit/review/bin/gerrit.sh restart

sudo /etc/init.d/apache2 restart

 

  1. 访问gerrit 管理界面 http://192.168.7.209:8080/

 

 

ubuntu14 搭建gerrit服务器及使用说明

 

、使用gerrit

 

1.1 账号密码

 

从管理员处获取账号密码后,登录Gerrit:http://192.168.7.209:8080/

ubuntu14 搭建gerrit服务器及使用说明

 

1.2修改名字和邮箱

 

 

如邮箱为[email protected]four-faith.com,则“Full Name”填写为xxxx:

 

ubuntu14 搭建gerrit服务器及使用说明

注册邮箱:注册成功后会给邮箱发送一封邮件,点击链接进行验证(如果某些浏览器无法打开验证链接,复制到IE上打开):

 

 

1.3 添加用户

 

linux下的用户名须和管理员给的Gerrit账号名相同,如果没有,请添加新用户:

ubuntu14 搭建gerrit服务器及使用说明

[email protected]:~# adduser gerrit

 

 

1.4 添加ssh**

备份.ssh(如果存在的话):

ubuntu14 搭建gerrit服务器及使用说明

 

生成**:

rm -rf ~/.ssh

ssh-****** -t rsa -C “自己邮箱地址”   ###一直按回车键即可

 

ubuntu14 搭建gerrit服务器及使用说明

然后公钥添加到Gerrit:

把 ~/.ssh/id_rsa.pub 内容复制好

 

ubuntu14 搭建gerrit服务器及使用说明

粘贴到

ubuntu14 搭建gerrit服务器及使用说明

 

2.1 下载代码

正确配置好邮箱和ssh公钥后,根据Gerrit管理员给出的地址下载代码:

git clone http://192.168.7.209:8080/test_gerrit.git

git clone ssh://[email protected]:29418/test_gerrit.git

 

2.2 下载commit-msg脚本

【重要:使用Gerrit必须要有此脚本】

提交前在本地仓库执行

gitdir=$(git rev-parse --git-dir); scp -p -P 29418 [email protected]:hooks/commit-msg ${gitdir}/hooks/

 

git commit --amend

 

 

2.3提交代码审核

Gerrit不允许用户直接push代码到分支上(特别授权用户除外),否则报错:

ubuntu14 搭建gerrit服务器及使用说明

 

正确的做法是所有提交都push到 HEAD:refs/for/分支名 :

格式如:git push origin HEAD:refs/for/分支名

例1:

当前处于本地分支master 

ubuntu14 搭建gerrit服务器及使用说明

 

代码提交到远程分支master上,则:git push origin HEAD:refs/for/master

ubuntu14 搭建gerrit服务器及使用说明

 

例2:

当前处于本地分支developer

ubuntu14 搭建gerrit服务器及使用说明

 

代码提交到远程分支deleloper上,则: git push origin HEAD:refs/for/developer

ubuntu14 搭建gerrit服务器及使用说明

 

2.4 添加审核者

代码正确提交到分支 refs/for/* 后,需要登录Gerrit界面为刚才的提交添加相应的审核者:

 

登录Gerrit:http://192.168.7.209:8080/, 在“changes”下可以看到刚刚的提交,名称显示为刚刚提交所写的注释

ubuntu14 搭建gerrit服务器及使用说明

 

 

点击进入,在“Need Code-Review”下添加相应的代码审核者(请根据自己项目情况添加指定的审核者,此处仅作演示)

ubuntu14 搭建gerrit服务器及使用说明

 

如果审核通过,会收到提示邮件(Code-Review为正数,说明审核通过,但代码未合入分支):

ubuntu14 搭建gerrit服务器及使用说明

 

 

代码被合入后,会收到邮件提示(说明代码已经正确提合入到分支):

ubuntu14 搭建gerrit服务器及使用说明

 

2.5 审核失败再提交

当收到审核失败的邮件时(Code-Review为负数,需要记下Change-Id,下一步提交时会用上),需要修改代码重新提交:

ubuntu14 搭建gerrit服务器及使用说明

 

修改好代码后,重新提交代码:

【重要:代码审核失败后,重新提交时必须使用 git commit –amend,否则会生成新的评审任务,无法与上一次审核任务关联】

ubuntu14 搭建gerrit服务器及使用说明

 

【重要:注释可以重新写,但是其中“Change-Id”必须为上一次审核任务失败的Change-Id,可在Gerrit或者上一步的邮件中查看得到】

ubuntu14 搭建gerrit服务器及使用说明

 

 

 

再push代码到Gerrit:

ubuntu14 搭建gerrit服务器及使用说明

 

最后无需重复步骤“3.3 提交代码审核”,Gerrit会自动给原来的审核者发送相应邮件

  

3.1 收到审核邮件

 

当有审核任务邮件时,点击链接打开Gerrit

ubuntu14 搭建gerrit服务器及使用说明

 

3.2 查阅审核任务

打开审核任务如下:

ubuntu14 搭建gerrit服务器及使用说明

 

3.3 添加其他审核者

如果需要,这里可以添加其他人员审核(需要有审核权限)

ubuntu14 搭建gerrit服务器及使用说明

 

3.4 查看修改内容

点击“Side-by-Side”按钮,查看修改内容:

ubuntu14 搭建gerrit服务器及使用说明

 

点击“next”查阅下一项修改:

ubuntu14 搭建gerrit服务器及使用说明

 

3.5 审核通过/失败

点击“Review”

 

ubuntu14 搭建gerrit服务器及使用说明

审核通过:

ubuntu14 搭建gerrit服务器及使用说明

 

审核失败:

ubuntu14 搭建gerrit服务器及使用说明

 

代码提示被blocked:

ubuntu14 搭建gerrit服务器及使用说明

 

 

 

 

 

 

 

Java SDK Download: http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

Gerrit Code Review - Releases Download: https://gerrit-releases.storage.googleapis.com/index.html

Gerrit Code Review -  Quick get started guide: https://git.eclipse.org/r/Documentation/install-quick.html

Gerrit代码审核服务器搭建全过程 http://blog.csdn.net/ganshuyu/article/details/8978614

Gerrit代码审核流程 http://www.worldhello.net/gotgit/images/gerrit-workflow.png

Gerrit代码审核原理 http://www.worldhello.net/gotgit/05-git-server/055-gerrit.html

Gerrit代码审核权限管理 https://gerrit-review.googlesource.com/Documentation/access-control.html#category_forge_committer

Gerrit修改数据库email信息 http://www.cnblogs.com/kevingrace/p/5624122.html

Gerrit安装replication插件 https://gerrit-review.googlesource.com/Documentation/cmd-plugin-install.html