使用gerrit進行code review

shuilaner_發表於2017-10-21

搭建環境: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(針對當前使用者) or vim /etc/profile(針對所有使用者,推薦)
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 --version
git version 1.9.1


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依賴,用來管理gerrit。
sudo adduser gerrit
sudo passwd gerrit
並將gerrit加入sudo許可權
sudo visudo
gerrit  ALL=(ALL:ALL) ALL


二、安裝與配置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://192.168.199.112:8081/ #gerrit web管理介面
[database]
        type = h2 #h2資料庫
        database = db/ReviewDB #資料庫路徑
[index]
        type = LUCENE
[auth]
        type = HTTP #auth模式,預設為OPENID,配置為HTTP,需要apache配置反向代理
[receive]
        enableSignedPush = false
[sendemail]
     enable=false #關閉郵件提醒
[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://192.168.199.112:8080 #proxy server
        proxyUsername = gerrit1 #proxy user & password
        proxyPassword = 123456


2.配置apache2反向代理


如果apache目錄結構如下:
[username@hostname apache2]$ ls 
apache2.conf conf-enabled magic mods-enabled ports.conf sites-enabled conf-available envvars mods-available sites-available
開啟SSL、Proxy、Rewrite等模組:

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 server 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 /etc/apache2/passwords #password file for gerrit
    </Location>

    ProxyPass / http://192.168.199.112:8081/

</VirtualHost>

如果apache目錄結構如下:
[username@hostname apache2]$ ls
bin  build  cgi-bin  conf  error  htdocs  icons  include  lib  logs  man  manual  modules
開啟SSL、Proxy、Rewrite等模組:

[username@hostname apache2]$ vi conf/http.conf
# Open LoadModule
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule rewrite_module modules/mod_rewrite.so
# Gerrit config
Include conf/extra/gerrit-httpd.conf 

其中apache2/conf/extra/gerrit-httpd.conf內容同上,apache2/sites-enabled/gerrit-httpd.conf。

3.配置gerrit賬戶密碼


touch /etc/apache2/passwords
htpasswd -b /etc/apache2/passwords admin 123456(管理員)
htpasswd -b /etc/apache2/passwords gerrit1 123456(普通使用者)

4.啟動gerrit&啟動apache2
sudo ~/review_site/bin/gerrit.sh start
sudo /etc/init.d/apache2 start

5.訪問gerrit 管理介面 http://192.168.199.112:8080/


第一次訪問,需要輸入第3步設定的admin及密碼,該賬戶將作為gerrit管理員賬戶。進入後可設定FullName: GerritAdmin。

三、如何使用gerrit


前提:需要git使用端 / gerrit服務端配合使用。

1.新增專案(gerrit 服務端)

1.1使用gerrit新增新專案:(適用於開啟新專案並使用gerrit)
ssh -p 29418 gerrit1@192.168.199.112 gerrit create-project --empty-commit --name demo-project #建議採用管理介面新增
或者使用gerrit管理介面

1.2使用gerrit新增已有專案:(適用於已有專案下移植到gerrit中)
ssh -p 29418 gerrit1@192.168.199.112 gerrit create-project --name exist-project #建議採用管理介面新增
或者使用gerrit管理介面
然後將已有專案與gerrit上建立的exist-project關聯,即將已有程式碼庫程式碼push到gerrit中進行管理。
cd ~/gitcode/exist-project
git push ssh://gerrit1@192.168.199.112:29418/exist-project *:*


2.生成sshkey(git使用端)

在開發賬戶中生成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 gerrit@RylanYan-ThinkPad


3.新增sshkey到gerrit伺服器(gerrit 服務端)

此步驟與git流程類似,即將id_rsa.pub內容上傳到git repository,gerrit中幫我們管理git repository.

4.拉取程式碼&配置git hooks(git client端)


驗證sshkey是否配置成功:ssh gerrit1@192.168.199.112 -p 29418

The authenticity of host '[192.168.199.112]:29418 ([127.0.0.1]:29418)' can't 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 '[192.168.199.112]: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@192.168.199.112:29418/REPOSITORY_NAME.git

拉取程式碼: git clone ssh://gerrit1@192.168.199.112:29418/demo-project

更新githooks:gitdir=$(git rev-parse --git-dir); scp -p -P 29418 gerrit1@192.168.199.112: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://gerrit1@localhost:29418/demo-project
 * [new branch]      HEAD -> refs/for/master

五.使用gerrit website完成code review
當完成push後,可在gerrit管理介面看到當前提交code review的change。
檢視需要code review的提交:
檢視某次提交的詳細資訊(稽核者+2可通過本次提交,提交者可通過Abandon本次提交):
如果稽核者+2通過後,可提交該次commit.

六.gerrit注意事項

* 需要為每個使用者分配gerrit賬號,不要都使用admin賬號,因為admin賬號可直接push master
* pull程式碼後需要配置githooks檔案,以便在commit時自動生成change-id,否則無法push
* push程式碼時需要使用git push origin HEAD:refs/for/master(branch),gerrit預設關閉非admin賬號的push direct許可權
* push程式碼時需要commit email與gerrit account email一致,否則無法push成功,可選擇關閉email notify,並開啟forge user許可權,或者通過修改gerrit資料庫account email資訊
* gerrit資料庫與gitlab同步,需要安裝replication外掛,並開啟該功能 參考:http://www.cnblogs.com/tesky0125/p/5973642.html


參考連結:

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

gerrit程式碼Review入門實戰 http://geek.csdn.net/news/detail/85681

轉:http://www.360doc.com/content/16/0823/13/10058718_585325908.shtml

相關文章