linux下搭建Gerrit

Gerrit 是一個基於 web 的代碼評審工具, 它基於 git 版本控制系統。Gerrit 旨在提供一個輕量級框架, 用於在代碼入庫之前對每個提交進行審閱。‎Gerrit記錄每一次提交的代碼修改 , 但實際上並不成爲項目的一部分, 直到它們被審閱和接受。它是標準開源過程的一個簡單工具來支持提交補丁程序, 然後由項目成員在應用到代碼庫之前進行評審。‎Gerrit 首先是一個臨時區域, 在提交的代碼成爲代碼庫的一部分之前, 可以對其修改進行檢查。因此,在項目開發過程中我們爲了保證代碼的質量,我們就可以通過Gerrit來review別人的代碼了,等所有人review後代碼才能提交到倉庫。

下邊我們說下怎麼在linux環境下搭建gerrit,以及在搭建過程中遇見的坑。

大致步驟:

  • 安裝git
  • 安裝jdk
  • 安裝apach2
  • 安裝gerrit
  • 配置
  • 驗證

安裝git

apt-get install git

安裝JDK

1、下載鏈接: https://pan.baidu.com/s/1eKjmwNXL7i-BvG76kq_cag 提取碼: ijan 

2、安裝

在/usr/ 目錄下新建一個java文件夾,將下載好的jdk移動到/usr/java下。
解壓:tar -zxvf jdk-**.tar.gz
解壓完成後刪除jdk的安裝包

3、配置環境

(1)在終端中輸入命令

sudo vim /etc/profile

(2)在文件末尾加入

JAVA_HOME=/usr/java/jdk1.8.0_152
CLASSPATH=.:$JAVA_HOME/lib.tools.jar
PATH=$JAVA_HOME/bin:$PATH 
export JAVA_HOME CLASSPATH PATH

(3)保存退出後,執行下面命令,讓profile文件生效

source /etc/profile

(4)驗證是否安裝成功

java -version

安裝apache2

apache2服務器是用來做反向代理用的。由於使用我們後面在安裝Gerrit時選擇的認證方式爲http,需要用apache2來做一個反向代理。

1. 安裝apache2

sudo apt-get install apache2

2.驗證是否安裝成功

sudo /etc/init.d/apache2 start

如果出現下面的提示,說明以已經安裝啓動成功。

[ ok ] Starting apache2 (via systemctl): apache2.service.

安裝gerrit

下載鏈接: https://pan.baidu.com/s/1Zgk450iQP3ADmwLpXrLqGQ 提取碼: 7j4s,或者自行去官網下載

下面開始安裝。你可以創建一個單獨的賬戶來安裝gerrit,也可以直接在當前用戶環境下安裝。我選擇的是就在當前用戶環境下安裝。

選擇自己專門放安裝軟件的目錄,將下載好的war包複製進去,然後執行

java -jar gerrit-xxx.war init -d gerrit_site

gerrit_site是安裝後文件目錄名。
執行完這行命令,會出現安裝提示:

Using secure store: com.google.gerrit.server.securestore.DefaultSecureStore
[2017-12-06 16:34:16,022] [main] INFO  com.google.gerrit.server.config.GerritServerConfigProvider : No /home/huanglin/Software/test/etc/gerrit.config; assuming defaults

*** Gerrit Code Review 2.14.6
*** 

Create '/home/huanglin/Software/test' [Y/n]? y

*** Git Repositories
*** 

Location of Git repositories   [git]:  

*** SQL Database
*** 

Database server type           [h2]: 

*** Index
*** 

Type                           [lucene/?]: 

*** User Authentication
*** 

Authentication method          [openid/?]: http(選擇http的認證方式,避免使用默認openid,需要fq的)
Get username from custom HTTP header [y/N]? 
SSO logout URL                 : 
Enable signed push support     [y/N]? 

*** Review Labels
*** 

Install Verified label         [y/N]? y(這裏寫y,方便後續使用Jenkins做持續集成)

*** Email Delivery
*** 

SMTP server hostname           [localhost]: 
SMTP server port               [(default)]: 
SMTP encryption                [none/?]: 
SMTP username                  : 

*** Container Process
*** 

Run as                         [huanglin]: 
Java runtime                   [/usr/java/jdk1.8.0_152/jre]: 
Copy gerrit-2.14.6.war to test/bin/gerrit.war [Y/n]?  
Copying gerrit-2.14.6.war to test/bin/gerrit.war

*** SSH Daemon
*** 

Listen on address              [*]: 
Listen on port                 [29418]: 
Generating SSH host key ... rsa... dsa... ed25519... ecdsa 256... ecdsa 384... ecdsa 521... done

*** HTTP Daemon
*** 

Behind reverse proxy           [y/N]? y
Proxy uses SSL (https://)      [y/N]? 
Subdirectory on proxy server   [/]: 
Listen on address              [*]: 
Listen on port                 [8081]: 
Canonical URL                  [http://huanglin/]: 

*** Cache
*** 


*** Plugins
*** 
***注意這些插件最好都行選擇安裝,以防後續需要用到的時候沒有,還要手動安裝。
Installing plugins.
Install plugin commit-message-length-validator version v2.14.6 [y/N]? y
Installed commit-message-length-validator v2.14.6
Install plugin download-commands version v2.14.6 [y/N]? y
Installed download-commands v2.14.6
Install plugin hooks version v2.14.6 [y/N]?
...後面的插件也一直填 y ,按enter執行下一步

配置

1.修改gerrit_site/etc/ 下的gerrit.config文件 。把canonicalWebUrl配置爲本地服務器(這裏就是我的電腦)IP。listenUrl爲代理服務器的IP,此處端口配置爲了8090。

[gerrit]
        basePath = git
        serverId = ff12fc22-fde9-4db8-9397-81e111d947c2
        canonicalWebUrl = http://49.235.226.52/    //配置成服務器地址,也就是我們的ubuntu地址
[database]
        type = h2
        database = /home/ubuntu/gerrit_site/db/ReviewDB
[auth]
        type = HTTP
[receive]
        enableSignedPush = false
[sendemail]
        smtpServer = localhost
[container]
        user = root
        javaHome = /usr/lib/jvm/java-8-openjdk-amd64/jre
[sshd]
        listenAddress = *:29418
[httpd]
        listenUrl = http://*:8090/    //這個服務器端口名
[cache]
        directory = cache

然後創建Gerrit賬戶

首先定位到一個目錄中,然後,在當前目錄下創建賬號文件

htpasswd -c httppwd admin

這個的話,就會在當前目錄下創建pwd的文件,用戶名是admin,然後後邊,會在讓我們輸入密碼,首個賬號默認是管理員,後續再追加賬號我們可以使用:

htpasswd -b httppwd gerrti_1 ******

來創建。

3.配置Apache2反向代理

首先我們得簡單瞭解下Apache2的配置

我們進入到Apache2的根目錄(etc/Apache2/),裏邊有個apache2.conf,我們打開看下其中有一行:

# Include module configuration:
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

# Include list of ports to listen on
Include ports.conf
Include httpd.conf

# Sets the default security model of the Apache2 HTTPD server. It does
# not allow access to the root filesystem outside of /usr/share and /var/www.
# The former is used by web applications packaged in Debian,
# the latter may be used for local directories served by the web server. If
# your system is serving content from a sub-directory in /srv you must allow
# access here, or in any related virtual host.

Include ports.conf 這個是apache2監聽的端口號

Include httpd.conf 這個是我自己新增配置的,我們新增配置後,都要包含進來,要不就把新增的配置文件放在sites-enabled下,在apache2.conf末尾,把sites-enabled的下的*.conf全部包含進來了。

然後我們看下ports.conf:

# If you just change the port or add more ports here, you will likely also
# have to change the VirtualHost statement in
# /etc/apache2/sites-enabled/000-default.conf


Listen 80

Listen 8091

<IfModule ssl_module>
        Listen 443
</IfModule>

<IfModule mod_gnutls.c>
        Listen 443
</IfModule>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

這裏這個 8091 是我自己添加的

然後我們再看下httpd.conf:

<VirtualHost *:8091>    //apache2監聽的端口
    ServerName *******    //改成服務器的ip

    ProxyRequests Off
    ProxyVia Off
    ProxyPreserveHost On

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

    <Location /login/>
      AuthType Basic
      AuthName "Gerrit Code Review"
      Require valid-user
      AuthBasicProvider file
      AuthUserFile /home/ubuntu/httppwd    //這個配置成我們前邊生成的密碼文件
    </Location>


    AllowEncodedSlashes On
    ProxyPass / http://服務器ip:8090/    //這個纔是我們gerrit真正的端口
</VirtualHost>

完成以上步驟,基本就可以了,接下來我們重啓gerrit跟apache2

$ /home/ubuntu/gerrit_site/bin/gerrit.sh restart
$ /etc/init.d/apache2 restart

然後我們就打開瀏覽器輸入:ip:8091,這樣我們就能正常的訪問我們的gerrit了,首次進入輸入我們剛纔設置的賬號。

下邊說下爲什麼通過8091就能訪問到我們的Gerrit了呢,這是因爲我們使用了Apache2的反向代理的緣故。我們訪問ip:8091,這個我們的apache就能監聽到此信息,然後通過上邊我們配置的ProxyPass / http://服務器ip:8090,apache去訪問此地址,也就是我們Gerrit的真實地址,然後將請求到的信息返回給我們,這樣我們就能成功的訪問到我們的gerrit了。

注意:在配置的過程中,一直碰到這個

這個搞了我一大天的時間,究其原因,就是Gerrit的賬戶沒有生成對,在訪問前我們一定要先生成賬戶密碼。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章