Skip to content

Gerrit 安装

2 min read ··· views #gerrit
Table of contents

Table of contents

工作台上一台打开机箱的服务器和一把螺丝刀

安装

安装 JDK

Gerrit 基于 Java 开发,依赖 Java 运行环境。当前最新的 3.13 版本要求至少 Java 21,因此需要先安装 JRE。

# 创建jdk存放目录
mkdir /usr/local/java

# 将压缩包解压到上述目录当中
tar -zxvf jdk-21_linux-64_bin.tar.gz -C /usr/local/java

# 创建软连接
# 需要先删除系统可能自带的软连接
# 然后重新创建 java 软链接到 /usr/bin/ 目录下
ln -s /usr/local/java/jdk-21.0.5/ /usr/bin/java

# 编辑系统配置文件
em /etc/profile

# 输入以下内容
export JAVA_HOME=/usr/bin/java
export PATH=$JAVA_HOME/bin:$PATH

# 激活配置文件
source /etc/profile

java --version
bash

安装 Gerrit

先创建 Gerrit 账号并切换到该用户:

sudo adduser gerrit

sudo su gerrit
cd /home/gerrit
bash

Gerrit 安装

java -jar gerrit-3.10.1.war init -d /home/gerrit
bash

安装过程中的初始参数设置如下:

  • Auth 方式输入 HTTP,并使用反向代理;
  • Plugin 相关选项全部选择 y。

配置用户:

htpasswd -c /home/gerrit/gerrit/etc/gerrit.password admin     # 创建第一个用户admin,同时会生成一个gerrit.password文件
htpasswd -m /home/gerrit/gerrit/etc/gerrit.password user1     # 在gerrit.password增加用户用 -m
bash

此时 gerrit/etc/gerrit.conf 配置如下:

[gerrit]
        basePath = git
        canonicalWebUrl = <http://192.168.52.46:8088>
        serverId = 3d9af676-03aa-4cc6-a75c-98d76682443e
[container]
        javaOptions = "-Dflogger.backend_factory=com.google.common.flogger.backend.log4j.Log4jBackendFactory#getInstance"
        javaOptions = "-Dflogger.logging_context=com.google.gerrit.server.logging.LoggingContext#getInstance"
        user = gerrit
        javaHome = /usr/local/java/jdk-25.0.1
[index]
        type = lucene
[auth]
        type = HTTP
        userNameCaseInsensitive = true
[receive]
        enableSignedPush = false
[sendemail]
        smtpServer = 192.168.52.46
        smtpServerPort = 25
        smtpUser = gerrit
[sshd]
        listenAddress = *:29418
[httpd]
        listenUrl = proxy-http://*:8088/
[cache]
        directory = cache
bash

配置反向代理

设置 nginx

em /etc/nginx/conf.d/gerrit.conf

server {
    listen 81;            # 代理监听81端口
    server_name 192.168.52.46;

    auth_basic "Welcome to Gerrit Code Review Site!";
    auth_basic_user_file /home/gerrit/gerrit/etc/gerrit.password;       # 代理使用 gerrit.password
    location / {
        proxy_pass  <http://192.168.52.46:8088>;        # 代理指向gerrit本地服务url,这个url要和gerrit.config中的canonicalWebUrl相同
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header Cache-Control "no-store";
        proxy_set_header Pragma "no-cache";
        proxy_set_header Expires "-1";
    }

    location = /logout {
        return 302 <http://logout:logout@192.168.52.46:81/>;
    }

}
bash

其中 location = /logout 这一段用于解决登出问题:

location = /logout {
        return 302 <http://logout:logout@192.168.52.46:81/>;
}
bash

原因如下:Gerrit 的 HTTP 验证通常依赖外部的反向代理(如 Nginx、Apache)来处理用户认证,客户端通过浏览器发送 HTTP basic authentication 头信息(Authorization: Basic <credentials>)来完成验证。

HTTP 验证的特点如下:

  1. 用户的登录状态由浏览器缓存的 Basic Authentication 凭据维持,不存储在 Gerrit 中。
  2. 用户点击 Sign out 后,Gerrit 本身无法清除浏览器上缓存的凭据。
  3. 现代浏览器的 HTTP Basic Auth 无法通过 JS 或页面行为主动触发清理,因此退出登录比较困难。

根本原因在于浏览器缓存了 Basic Authentication 凭证:即使点击 Sign out,浏览器仍会用之前缓存的凭据重新发送认证请求,导致用户无法登出。

基于火狐内核的解决方案如下:

  • 浏览器会自动处理包含 logout:logout@ 形式的 URL,直接向目标服务器发送带 Authorization 头的请求。
  • 由于提供的用户名和密码(logout:logout)在服务器端被拒绝,浏览器会认为当前凭据已失效,从而清除缓存的 HTTP Basic Authentication 会话。