【Gerrit】Gerrit在Windows系统下的安装

4,296 阅读2分钟

该过程基础官方2.14.6版本,文档连接:gerrit-documentation.storage.googleapis.com/Documentati…

前期工作:

  • 安装java运行环境,推荐1.8版本
  • 安装git,Gerrit是基于git版本管理工具开发的代码审查工具
  • 安装数据库,可选。Gerrit默认使用内置H2数据库,其他可选数据库见官方安装文档
  • 安装nginx,进行反向代理,可选。如果Gerrit使用http方式,需要服务器添加header内容。参考配置:

    #user  nobody;
    worker_processes  1;
    
    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;
    
    #pid        logs/nginx.pid;
    
    
    events {
        worker_connections  1024;
    }
    
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
    
        #gzip  on;
    
        server {
         #监听80端口
         listen *:80;
         server_name localhost;
         allow   all;
         deny    all;
    
         auth_basic "Welcomme to Gerrit Code Review Site!";
         #http模式下gerrit的用户文件,详情见下文
         auth_basic_user_file E:\gerrit\etc\gerrit.password;
    
         location / {
            #gerrit的默认端口为8080
            proxy_pass  http://127.0.0.1:8080;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
         }
       }
    
    }
    
    

  • 安装htpasswd,可选。如果Gerrit使用http方式,需要使用该工具生成用户和密码。windows系统的安装版本下载:www.htpasswdgenerator.com/download_ht…

1.下载

Gerrit官方下载连接:www.gerritcodereview.com/releases/2.…

选择2.16.4版本进行下载,下载文件为war包。

2.安装

使用window的shell执行器,切换至下载目录,执行如下命令:

# /path/to/your/gerrit_application_directory为gerrit的安装目录
java -jar gerrit.war init -d /path/to/your/gerrit_application_directory

执行上述命令后,除了Authentication method 需要填写http,其他可以都按照默认回车即可。

3.配置

打开Gerrit安装目录,进入etc目录,修改gerrit.config文件:

[gerrit]
	basePath = git
	serverId = cafccbde-e040-498f-a861-b910cb7fa3c5
	canonicalWebUrl = http://192.168.56.1:8080/
[database]
	type = h2
	database = E:\\gerrit\\db\\ReviewDB
[index]
	type = LUCENE
[auth]
	type = HTTP
[receive]
	enableSignedPush = false
[sendemail]
        # 邮箱配置
	smtpServer = smtp.example.com
        smtpServerPort = 465
        smtpEncryption = SSL
        smtpUser = example@example.com
        smtpPass = example
        from = example@example.com
[container]
	user = example
	javaHome = C:\\Program Files\\Java\\jdk1.8.0_131\\jre
[sshd]
	listenAddress = *:29418
[httpd]
        # 注意修改该部分,反向代理配置
	listenUrl = proxy-http://*:8080/
[cache]
	directory = cache

然后再etc目录下,执行以下命令,添加用户(需要安装htpasswd,并加入path):

# 注意,该文件目录需要和nginx中的授权路径相同
# 创建gerrit.password文件,添加admin用户
htpasswd -c gerrit.password admin
# 在gerrit.password文件中新添加用户test
gtpasswd gerrit.password test

4.启动

在Gerrit安装目录下执行以下命令:

java -jar bin\gerrit.war daemon --console-log

在nginx中按照“前期工作”编写配置文件,如conf/gerrit.conf,执行以下命令:

nginx -c conf/gerrit.conf

待Gerrit和nginx启动后,在浏览器中输入http://localhost,使用上述创建的用户登录(第一个登录用户为管理员用户)即可。

5.其他

  1. Gerrit中自带项目All-project,所有项目继承该项目的权限。
  2. Gerrit获取代码方式:

    git clone ssh://gerrithost:29418/RecipeBook.git RecipeBook

  3. Gerrit为了保证代码的审查,每次commit需要生成change-Id。可以使用Gerrit自带的钩子,需要在本地代码库中执行以下命令:

    scp -p -P 29418 john.doe@review.example.com:hooks/commit-msg .git/hooks/

  4. Gerrit默认不向用户开放直接合并到代码库的权限。假设想合入代码到master分支,需要推送到魔法分支refs/for/master,经过审查后才能合入代码库。

    git push origin HEAD:refs/for/master