linux下搭建Gerrit

6 篇文章 0 订阅
1 篇文章 0 订阅

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://**.***.***.**/    //配置成服务器地址,也就是我们的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的账户没有生成对,在访问前我们一定要先生成账户密码。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值