Mac搭建review 代码工具Gerrit + Gitlab

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Java_HuiLong/article/details/82778425

Mac系统 搭建代码review工具 Gerrit + Gitlab


比较适合Java开发做代码review

一、安装前的 准备工作

1.已经安装JDK【1.7/1.8】
2.已经安装 mysql
3.nginx
4.git

二、安装Gerrit

官网Gerrit下载地址:https://www.gerritcodereview.com/

1.先创建gerrit启动过程需要的数据库

$ mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 35
Server version: 5.7.20-0ubuntu0.16.04.1 (Ubuntu)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> SELECT USER();
+----------------+
| USER()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)

mysql> create database reviewdb;
Query OK, 1 row affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on reviewdb.* to zhanghuilong@localhost identified by 'zhanghuilong';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| reviewdb           |
| sys                |
+--------------------+
8 rows in set (0.01 sec)

mysql>
 

2.安装gerrit
除了设置 http的那个稍微注意点,其他可以一路回车键按下去。后面还可以更改gerrit.config 配置

# init -d review_site 中的文件名称可以自定义
huilong-MackPro:~ zhanghuilong$ java -jar Downloads/gerrit-2.15.3.war init -d review_site
Using secure store: com.google.gerrit.server.securestore.DefaultSecureStore
[2018-09-18 14:37:03,839] [main] INFO  com.google.gerrit.server.config.GerritServerConfigProvider : No /Users/zhanghuilong/review_site/etc/gerrit.config; assuming defaults

*** Gerrit Code Review 2.15.3
*** 

Create '/Users/zhanghuilong/review_site' [Y/n]? Y #创建文件夹

*** Git Repositories
*** 

Location of Git repositories   [git]: GerritResource #GerritResource后续git项目仓库位置

*** SQL Database
*** 

Database server type           [h2]: mysql #根据自己本机安装的数据库进行设置,我本地是mysql

Gerrit Code Review is not shipped with MySQL Connector/J 5.1.43
**  This library is required for your configuration. **
Download and install it now [Y/n]? Y
Downloading https://repo1.maven.org/maven2/mysql/mysql-connector-java/5.1.43/mysql-connector-java-5.1.43.jar ... OK
Checksum mysql-connector-java-5.1.43.jar OK
Server hostname                [localhost]: 
Server port                    [(mysql default)]: 
Database name                  [reviewdb]: # 数据库名称 
Database username              [zhanghuilong]: root # 我本地用户名为root,所以这里更改为:root
root's password                :  #输入数据库访问密码
              confirm password :  #确认密码

*** NoteDb Database
*** 

Use NoteDb for change metadata?
  See documentation:
  https://gerrit-review.googlesource.com/Documentation/note-db.html
Enable                         [Y/n]? 

*** Index
*** 

Type                           [lucene/?]: 

*** User Authentication
*** 

Authentication method          [openid/?]: HTTP  #【核心点,以http认证方式】
Get username from custom HTTP header [y/N]? 
SSO logout URL                 : 
Enable signed push support     [y/N]? 

*** Review Labels
*** 

Install Verified label         [y/N]? 

*** Email Delivery
*** 

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

*** Container Process
*** 

Run as                         [zhanghuilong]: 
Java runtime                   [/Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre]: 
Copy gerrit-2.15.3.war to review_site/bin/gerrit.war [Y/n]? 
Copying gerrit-2.15.3.war to review_site/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]? N
Subdirectory on proxy server   [/]: 
Listen on address              [*]: 
Listen on port                 [8080]: #默认是8080 可能会跟其他工程 端口冲突,如 tomcat 和 nginx 
Canonical URL                  [http://10.240.237.222/]: 

*** Cache
*** 


*** Plugins
*** 

Installing plugins.
Install plugin commit-message-length-validator version v2.15.3 [y/N]? 
Install plugin download-commands version v2.15.3 [y/N]? 
Install plugin hooks version v2.15.3 [y/N]? y
Installed hooks v2.15.3
Install plugin replication version v2.15.3 [y/N]? y
Installed replication v2.15.3
Install plugin reviewnotes version v2.15.3 [y/N]? y
Installed reviewnotes v2.15.3
Install plugin singleusergroup version v2.15.3 [y/N]? y
Installed singleusergroup v2.15.3
Initializing plugins.

*** Experimental features
*** 

Enable any experimental features [y/N]? y
Default to PolyGerrit UI       [Y/n]? n

Tue Sep 18 14:41:11 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Tue Sep 18 14:41:13 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
Initialized /Users/zhanghuilong/review_site
Executing /Users/zhanghuilong/review_site/bin/gerrit.sh start
Starting Gerrit Code Review: OK
Waiting for server on 10.240.237.222:80 ... OK         
Opening http://10.240.237.222/#/admin/projects/ ...OK

3.安装过程,可以假定自己有一个域名 绑定本地host
如:127.0.0.1 g.hrsstd.com 这样方便后面更改配置

4.初次生成的配置跟下面贴出来的少有不同

huilong-MackPro:~ zhanghuilong$ cd review_site/etc/
huilong-MackPro:etc zhanghuilong$ vi gerrit.config 
[gerrit]
	basePath = GerritResource
	serverId = ed595493-e878-47ac-8efb-676a9eb0a774
	canonicalWebUrl = http://g.hrsstd.com/ #此处第一次生成的是本机ip:8080
[database]
	type = mysql
	hostname = localhost
	database = reviewdb
	username = root
[index]
	type = LUCENE
[auth]
	type = HTTP
[receive]
	enableSignedPush = false
[sendemail]
	smtpServer = localhost
[container]
	user = zhanghuilong
	javaHome = /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre
[sshd]
	listenAddress = *:29418
[httpd]
	listenUrl = http://127.0.0.1:8099/ # 此处第一次生成的是*:8080
[cache]
	directory = cache

5.修改配置之后重启即可

huilong-MackPro:bin zhanghuilong$ sh gerrit.sh restart
Stopping Gerrit Code Review: OK
Starting Gerrit Code Review: OK

6.访问:g.hrsstd.com:8099 可以看到如图:

在这里插入图片描述

三、修改认证方式和反向代理

为了通过更为强大的 Web 服务器来对外提供服务,同时方便 Gerrit Server 的 HTTP 用户认证方式可以正常工作,需要设置反向代理。这里使用 nginx 作为 Web 服务器。

1.设置第一个 Gerrit 用户的帐号和密码

$ touch ./review_site/etc/passwd
$ htpasswd -b ./review_site/etc/passwd admin admin
Adding password for user admin

2.修改 nginx 的配置文件 /etc/nginx/nginx.conf,在它的 http 块中加入

 #gerrit;
    server {
      listen 80;
      server_name g.hrsstd.com;

      location ^~ / {
            auth_basic "Restricted";
            auth_basic_user_file  /Users/zhanghuilong/review_site/etc/passwd;
        proxy_pass        http://127.0.0.1:8099;
        proxy_set_header  X-Forwarded-For $remote_addr;
        proxy_set_header  Host $host;
      }
    }

3.启动nginx

zhanghuilong$ sudo nginx 

4.访问: g.hrsstd.com ,输入刚刚设置的用户+密码

在这里插入图片描述
在这里插入图片描述

四、Replication 配置,用于同步代码到gitlab

所谓的 replication,是 Gerrit 的一个插件,它可以自动地将 Gerrit Code Review 对它所管理的 Git 仓库创建的任何 changes push 到另外一个系统里。Gerrit 本身提供了两大功能:一是 Code Review;二是 Git 仓库。Replication 插件通常用于提供 changes 的镜像,或热备份。

此外,许多现有的项目可能是用另外一套系统来管理 Git 代码仓库的,比如 GitLab,或者 GitHub。需要引入 Gerrit 做 Code Review,同时对接这些已有的 Git 仓库系统时,replication 插件比较有用。

配置 replication 将代码同步到 GitLab 的方法如下。

1.如果通过 SSH 来从 Gerrit 同步代码到 GitLab,需要确保远程系统,也就是 GitLab 服务器的主机密钥已经在 用户的 ~/.ssh/known_hosts 文件中了。

首先,如果还没有生成过 SSH key 的话,需要生成 SSH Key:

$ ssh-keygen -t rsa
Generating public/private rsa key pair.

然后将 SSK Key 的公钥,即 ~/.ssh/id_rsa.pub 文件的内容,添加到 GitLab 中具有权限的用户的 SSH key 列表里。

2.添加主机密钥,即通过 ssh-keyscan 命令,像下面这样:

#使用官网gitlab,直接添加即可
ssh-keyscan  -t rsa gitlab.com >> /Users/zhanghuilong/.ssh/known_hosts
#如果是公司搭建的gitlab通常情况下要指定端口,像这样添加
ssh-keyscan -p 22222 -t rsa g.hz.netease.com >> /Users/zhanghuilong/.ssh/known_hosts

如果 GitLab 的 SSH 服务不是在标准的 22 端口上提供的,需要通过 -p 参数给上面执行的 ssh 命令指定 SSH 服务的端口号。GitLab 的 SSH 服务监听的端口号,可以从项目的 URL 中看出来 —— SSH URL 中主机名后面的冒号之后的是端口号。

3.接下来,在review_site/etc 目录下创建: replication.config 文件。

这是一个 Git风格的配置文件,它用来控制 replication 插件的设置。这个文件由一个或多个 remote 段组成,每个 remote 段都为一个或多个目标 URIs 提供公共配置设置。如:

[remote "gitlab"]
    #这里的[email protected]:ZhangHLong 就是官网gitlab ssh 地址
    url = [email protected]:ZhangHLong/${name}.git
    push = +refs/heads/*:refs/heads/*
    push = +refs/tags/*:refs/tags/*
    push = +refs/changes/*:refs/changes/*
    timtout = 30
    threads = 3               

配置: ~/.ssh/config 文件,Gerrit 将在启动的时读取并缓存它,并支持大部分 SSH 配置。

Host gitlab.com
    User git
    IdentityFile ~/.ssh/id_rsa
    StrictHostKeyChecking no
    UserKnownHostsFile /dev/null
    PreferredAuthentications publickey

然后重新启动 Gerrit Server:

sh ~/review_site/bin/gerrit.sh restart

确保上面流程正常执行,才能确保同步代码。

4.添加sshkey+创建新工程,注意 工程名要和gitlab上的保持一致

在这里插入图片描述

创建工程注意名称一定要和gitlab一致
在这里插入图片描述
gitlab工程名称;
在这里插入图片描述

创建好以后可以点击branch看下结构,
在这里插入图片描述
在 review_site的仓库中也可以看到:在这里插入图片描述

这里在 Gerrit 上创建了工程之后,还需要用 GitLab 上已有的代码替换 Gerrit 中的空工程:

huilong-MackPro:GerritResource zhanghuilong$ rm -rf hrs-portal.git/
huilong-MackPro:GerritResource zhanghuilong$ git clone --bare [email protected]:ZhangHLong/hrs-portal.git
Cloning into bare repository 'hrs-portal.git'...
Warning: Permanently added 'gitlab.com' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 237, done.
remote: Counting objects: 100% (237/237), done.
remote: Compressing objects: 100% (166/166), done.
remote: Total 1357 (delta 39), reused 0 (delta 0)
Receiving objects: 100% (1357/1357), 25.16 MiB | 747.00 KiB/s, done.
Resolving deltas: 100% (296/296), done.

再次点击 branch会发现之前gitlab里创建的分支都会同步到此。

5.从gerrit上clone代码

$ git clone ssh://[email protected]:29418/hrs-portal && scp -p -P 29418 [email protected]:hooks/commit-msg hrs-portal/.git/hooks/
Cloning into 'hrs-portal'...
remote: Counting objects: 1357, done
remote: Finding sources: 100% (1357/1357)
remote: Total 1357 (delta 296), reused 1357 (delta 296)
Receiving objects: 100% (1357/1357), 25.16 MiB | 20.51 MiB/s, done.
Resolving deltas: 100% (296/296), done.
commit-msg                                         

注意这种clone 会多出来一个文件在工程的.git/hooks里

在这里插入图片描述

五、实际开发中如何使用。

介绍一下idea的一种插件
在这里插入图片描述

校验链接是否OK
在这里插入图片描述

打开工程改动一段代码提交:
a.commit过程和普通git 一样
在这里插入图片描述

b.不同的是push 的时候 ,勾选 Push to Gerrit
使用插件Gerrit提交
在这里插入图片描述

提交成功可以看到变更:
1.提交的代码
在这里插入图片描述

2.点击进去可以进行添加review者 和 review。+2代表通过,注意多人+1 结果并不能通过。
在这里插入图片描述
3. 双击变更区域 进行添加评论
在这里插入图片描述

在这里插入图片描述

问题汇总:

0.安装replication过程:

如果安装过程出现问题,可以手动安装插件。方法如下:

 java -jar Downloads/gerrit-2.14.12.war init -d review_site --batch --install-plugin replication

1.fatal: remote plugin administration is disabled
解决方法:

/etc/gerrit.config文件里添加下面内容,然后重启:
[plugins]
allowRemoteAdmin = true

2.安装过程:出现.lock,之前的服务进程并未关闭,需要暂停服务: sh gerrit.sh stop

Caused by: org.apache.lucene.store.LockObtainFailedException: Lock held by another program: /Users/zhanghuilong/review_site/index/accounts_0004/write.lock

3.如果你安装过程出现问题,可以尝试重新安装一遍war包。
记住如果你想彻底重新开始重新安装,一定要把之前的数据库先删除,再创建。

如果你也在搭建,过程有问题可以找我帮你看下

参考资料:
https://blog.csdn.net/Michaelwubo/article/details/81066169
https://www.cnblogs.com/kevingrace/p/5651447.html

猜你喜欢

转载自blog.csdn.net/Java_HuiLong/article/details/82778425