如何搭建gerrit开源code review工具

Posted Rylan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何搭建gerrit开源code review工具相关的知识,希望对你有一定的参考价值。

搭建环境:Ubuntu 14.04

一、环境准备

1.Java环境

gerrit依赖,用于安装gerrit环境。

下载:jdk-7u79-linux-x64.tar.gz http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.html

安装:sudo tar zxvf ./jdk-7u79-linux-x64.tar.gz -C /opt

配置:vim ~/.bashrc

export JAVA_HOME=/opt/jdk1.7.0_79
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH
export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH

验证:java -version

java version "1.7.0_79"
Java(TM) SE Runtime Environment (build 1.7.0_79-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.79-b02, mixed mode)

2.git环境

gerrit依赖,用来操作git repository。

安装:sudo apt-get install git

验证:git

usage: git [--version] [--help] [-C <path>] [-c name=value]

3.gerrit环境

下载:Gerrit 2.12.4 https://www.gerritcodereview.com/download/gerrit-2.12.4.war

4.apache2环境

安装:sudo apt-get install apache2 

验证:sudo /etc/init.d/apache2 start

5.gerrit管理帐号

gerrit依赖,用来管理gerrit。

sudo adduser gerrit

sudo passwd gerrit

su gerrit

二、安装与配置gerrit

1.配置gerrit

默认安装:java -jar gerrit-2.12.4.war init --batch -d ~/review_site

更新配置文件:sudo vim ~/review_site/etc/gerrit.config

[gerrit]
        basePath = git #默认gerrit对应的git库
        canonicalWebUrl = http://localhost:8081/ #gerrit web管理界面
[database]
        type = h2 #h2数据库
        database = db/ReviewDB #数据库路径
[index]
        type = LUCENE
[auth]
        type = HTTP #auth模式,默认为OPENID,配置为HTTP,需要apache配置反向代理
[receive]
        enableSignedPush = false
[sendemail]
        smtpServer = localhost
[container]
        user = gerrit #linux user for gerrit
        javaHome = /opt/jdk1.7.0_79/jre #java home
[sshd]
        listenAddress = *:29418 #default gerrit port
[httpd]
        listenUrl = http://*:8081/
[cache]
        directory = cache
[http]
        proxy = http://localhost:8080 #proxy server
        proxyUsername = gerrit1 #proxy user & password
        proxyPassword = 123456789

2.配置apache2反向代理

开启SSL、代理等模块:

cd /etc/apache2/mods-enabled

ln -s ../mods-available/proxy.load

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

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

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

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

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

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

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

ln -s ../mods-available/socache_shmcb.load #

ln -s ../mods-available/slotmem_shm.load #

更新配置文件:sudo vim /etc/apache2/sites-enabled/gerrit-httpd.conf

ServerName 192.168.199.112 #your local ip
<VirtualHost *:8080>
    ProxyRequests Off
    ProxyVia Off
    ProxyPreserveHost On
    AllowEncodedSlashes On
    RewriteEngine On
    RewriteRule ^/(.*) http://192.168.199.112:8081/$1 [NE,P] #rewrite rule for proxy

    <Proxy *>
          Order deny,allow
          Allow from all
    </Proxy>

    <Location /login/>
        AuthType Basic
        AuthName "Gerrit Code Review"
        Require valid-user
        AuthBasicProvider file
        AuthUserFile /home/gerrit/review_site/etc/passwd #password file for gerrit
    </Location>

    ProxyPass / http://192.168.199.112:8081/

</VirtualHost>

3.配置gerrit账户密码

touch ~/review_site/etc/passwd

htpasswd -b ~/review_site/etc/passwd gerrit1 123456789

4.启动gerrit&启动apache2

sudo ~/review_site/bin/gerrit.sh start

sudo /etc/init.d/apache2 start

5.访问gerrit 管理界面 http://localhost:8080/

第一次访问,需要输入第3步设置的gerrit1及密码,该账户将作为gerrit管理员账户。进入后可设置FullName: GerritAdmin。

三、如何使用gerrit

1.添加项目

使用gerrit添加新项目:(适用于开启新项目并使用gerrit)

ssh -p 29418 gerrit1@localhost gerrit create-project --empty-commit --name demo-project #建议采用管理界面添加

或者使用gerrit管理界面

技术分享

使用gerrit添加已有项目:(适用于已有项目下使用gerrit)

ssh -p 29418 gerrit1@localhost gerrit create-project --name exist-project #建议采用管理界面添加

或者使用gerrit管理界面

技术分享

然后将已有项目与gerrit上建立的exist-project关联,即将已有代码库代码push到gerrit中进行管理。

cd ~/gitcode/exist-project

git push ssh://[email protected]:29418/exist-project *:*

2.生成sshkey

在开发账户中生成sshkey,用作与gerrit服务器连接。

ssh-keygen -t rsa #生成sshkey

ls ~/.ssh/ #可查看sshkey

cat ~/.ssh/id_rsa.pub #查看sshkey

ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCj1XDqjNXbn39oeacJOxe8FklBJRpGS1CcHRThWBytZ4A5KXAaxYzcD94GUd9UxxZzKCr6y90mwuJ+jkKxCTlqUiwj73QIiPWQ3Re08M049W4XxdfGnu/jyTI9DptWBsF0dwFJlQquUtitS+b1Tkz7Jr7+WipbZ22aiHwRvY4VcvCCdIHy/BnCCbVcfgk9u8f+X+ROm+DkOGfUcBNXWEJydqF0wy/D13Q5gp9OAXMIOD05T3GToJRwYtx2cVFmK4jE9HtcudOrrZNFVHqeblbA7EMKpIuDpLho7esmpwJ/woB1wnKTwHoUzbMt0a6hHPMNWyP2WIJebEA1KzThLixt [email protected]

3.添加sshkey到gerrit服务器

此步骤与git流程类似,即将id_rsa.pub内容上传到git repository,gerrit中帮我们管理git repository.

 技术分享

3.拉取代码&配置git hooks

验证sshkey是否配置成功:ssh [email protected] -p 29418

The authenticity of host [localhost]:29418 ([127.0.0.1]:29418) cant be established.
  RSA key fingerprint is db:07:3d:c2:94:25:b5:8d:ac:bc:b5:9e:2f:95:5f:4a.
  Are you sure you want to continue connecting (yes/no)? yes
  Warning: Permanently added [localhost]:29418 (RSA) to the list of known hosts.

  ****    Welcome to Gerrit Code Review    ****

  Hi user, you have successfully connected over SSH.

  Unfortunately, interactive shells are disabled.
  To clone a hosted Git repository, use:

  git clone ssh://gerrit1@localhost:29418/REPOSITORY_NAME.git

拉取代码: git clone ssh://[email protected]:29418/demo-project

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

该过程用来在commit-msg中加入change-id,gerrit流程必备。

修改代码并提交,推送时与原有git流程不一致,采用 git push origin HEAD:refs/for/master 。

git push origin HEAD:refs/for/master
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 381 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: Processing changes: new: 1, refs: 1, done    
remote: 
remote: New Changes:
remote:   http://localhost:8081/4 Append date to testfile
remote: 
To ssh://[email protected]:29418/demo-project
 * [new branch]      HEAD -> refs/for/master

4.使用gerrit完成code review

当第3步完成push后,可在gerrit管理界面看到当前提交code review的change。

查看需要code review的提交:

技术分享

查看某次提交的详细信息(审核者+2可通过本次提交,提交者可通过Abandon本次提交):

技术分享

如果审核者+2通过后,可提交该次commit.

技术分享

 

 

参考链接:

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

Gerrit Code Review - Releases: 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开源code review工具的主要内容,如果未能解决你的问题,请参考以下文章

gerrit code review上的项目怎样删除??

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

如何修改gitreview.email

Jenkins Gerrit 报告值插件删除了 Code-review 的值

gerrit怎么看别人写的review

gerrit