最适合菜鸟的Gerrit搭建详解

1. 基础知识铺垫


2. 准备工作


3. 环境

服务器CentOS 7.5 64位
JDK1.8.0_232

3.1 安装JDK

yum install java 

如果是自己通过自己下载的tar.gz安装的JDK,需要手动配置好JAVA_HOME等环境变量。
安装好之后通过 java -version命令查看JDK是否安装成功。

3.2 安装Git

yum install git 

PS:安装过程中会有询问提示,输入y就行

安装完使用下面命令查看Git版本,注意此处是双中划线,之前笔者踩过坑以为是单划线,一直怀疑自己Git没有装好。

git --version

3.3. 安装Mysql


4. Gerrit安装

下载地址:https://www.gerritcodereview.com/index.html

q1.png

下载文件为gerrit-3.1.2.war

运行war包

java -jar gerrit-3.1.2.war init -d review_site

接下来就要小心了,安装的时候选择项很多,空白项回车就行

Using secure store: com.google.gerrit.server.securestore.DefaultSecureStore
[2020-01-07 16:53:32,854] [main] INFO  com.google.gerrit.server.config.GerritServerConfigProvider : No /usr/local/program/gerrit/review_site/etc/gerrit.config; assuming defaults

*** Gerrit Code Review 3.1.2
*** 

Create '/usr/local/program/gerrit/review_site' [Y/n]? y
*** Git Repositories
*** 

Location of Git repositories   [git]: /home/gerrit/gerrit_repo

*** Index
*** 

Type                           [lucene]: 

*** User Authentication
*** 

Authentication method          [openid/?]: http
Get username from custom HTTP header [y/N]? n
SSO logout URL                 : 
Enable signed push support     [y/N]? n

*** Review Labels
*** 

Install Verified label         [y/N]? n

*** Email Delivery
*** 

SMTP server hostname           [localhost]: smtp.163.com
SMTP server port               [(default)]: 25
SMTP encryption                [none/?]: 
SMTP username                  [root]: [email protected]
[email protected]'s password : 
              confirm password : 

*** Container Process
*** 

Run as                         [root]: 
Java runtime                   [/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64/jre]: 
Copy gerrit-3.1.2.war to review_site/bin/gerrit.war [Y/n]? y
Copying gerrit-3.1.2.war to review_site/bin/gerrit.war

*** SSH Daemon
*** 

Listen on address              [*]: 
Listen on port                 [29418]: 
Generating SSH host key ... rsa... 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              [*]: 172.0.0.1
Listen on port                 [8081]: 
Canonical URL                  [http://172.0.0.1/]: http://localhost:8081

*** Cache
*** 


*** Plugins
*** 

Installing plugins.
Install plugin codemirror-editor version v3.1.2 [y/N]? y
Installed codemirror-editor v3.1.2
Install plugin commit-message-length-validator version v3.1.2 [y/N]? y
Installed commit-message-length-validator v3.1.2
Install plugin delete-project version v3.1.2 [y/N]? y
Installed delete-project v3.1.2
Install plugin download-commands version v3.1.2 [y/N]? y
Installed download-commands v3.1.2
Install plugin gitiles version v3.1.2 [y/N]? y
Installed gitiles v3.1.2
Install plugin hooks version v3.1.2 [y/N]? y
Installed hooks v3.1.2
Install plugin plugin-manager version v3.1.2 [y/N]? y
Installed plugin-manager v3.1.2
Install plugin replication version v3.1.2 [y/N]? y
Installed replication v3.1.2
Install plugin reviewnotes version v3.1.2 [y/N]? y
Installed reviewnotes v3.1.2
Install plugin singleusergroup version v3.1.2 [y/N]? y
Installed singleusergroup v3.1.2
Install plugin webhooks version v3.1.2 [y/N]? y
Installed webhooks v3.1.2
Initializing plugins.

Initialized /usr/local/program/gerrit/review_site
Reindexing projects:    100% (2/2) with: reindex --site-path review_site --threads 1 --index projects
Reindexed 2 documents in projects index in 0.5s (4.1/s)


5. 安装Nginx

这里不过多赘述,大家可以自行百度搜索,一大把,笔者推荐一个傻瓜式详细Nginx安装教程,没有弯路,全程复制粘贴命令就行,地址如下:
https://www.runoob.com/linux/nginx-install-setup.html

安装完成之后,修改Nginx安装目录:

vim /usr/local/webserver/nginx/conf/nginx.conf

内容如下:

 server {  
    listen *:8080; 
    server_name localhost;
    allow   all; 
    deny    all; 
    auth_basic "Welcomme to Gerrit Code Review Site!"; 
    auth_basic_user_file /usr/local/webserver/nginx/gerrit/htpasswd.conf; 
        
    location / {  
        proxy_pass  http://localhost:8081; 
    }  
}


6. 安装Apache

yum install httpd

在nginx目录下新建一个目录,后续用来存放gerrit的初始管理员密码文件

mkdir /usr/local/webserver/nginx/gerrit

然后使用htpasswd 命令。

htpasswd -c /usr/local/webserver/nginx/gerrit/htpasswd.conf admin

这个时候会提示你输入密码,然后输入你自己设置的管理员密码即可,如下

[root@VM_0_3_centos gerrit]# htpasswd -c /usr/local/webserver/nginx/gerrit/htpasswd.conf admin
New password:
Re-type new password:
Adding password for user admin
[root@VM_0_3_centos gerrit]#

此时会生成一个 gerrit/htpasswd.conf 密码文件,很多朋友在执行上述命令的时候可能会出现如下问题,主要是因为我们没有安装Apache,解决这个问题就是把Apache装好。

-bash: htpasswd: command not found


7. 启动

7.1. 重启Nginx

由于我们修改了nginx配置,所以需要重新加载一下配置文件

 /usr/local/webserver/nginx/sbin/nginx -s reload

然后重新启动Nginx

/usr/local/webserver/nginx/sbin/nginx -s reopen

7.2 启动 Gerrit

cd /usr/local/program/gerrit/review_site/bin/
./gerrit.sh start

这里可能会有小伙伴们出现启动失败的情况,修改一下gerrit的配置文件,把listenUrl 换成localhost,详情去参考文章的后面章节的踩坑合集。

启动成功之后,在浏览器输入:http://你服务器的ip:8080,成功进入gerrit页面,此时会弹框提示输入账号密码,输入之前我们通过htpasswd 命令创建的admin账号和密码登录即可。


微信截图_20200108171847.png

在这里,可能会有一部分新手小菜用的是云服务器,如阿里云、腾讯云,出现没有正常跳转到Gerrit页面,此时需要检查你的服务器是否开放了相应的端口。


踩坑分析

坑1

启动gerrit的时候,一直报错如下错

[root@VM_0_3_centos review_site]# ./bin/gerrit.sh restart
Stopping Gerrit Code Review: OK
Starting Gerrit Code Review: FAILED

查看日志发现

cat /usr/local/program/gerrit/review_site/logs/error_log

[2020-01-08 13:48:41,635] [main] INFO org.eclipse.jetty.server.handler.ContextHandler : Started o.e.j.s.ServletContextHandler@757f47f0{/,null,AVAILABLE}
[2020-01-08 13:48:41,637] [main] ERROR com.google.gerrit.pgm.Daemon : Unable to start daemon
java.lang.IllegalStateException: Cannot start HTTP daemon
at com.google.gerrit.pgm.http.jetty.JettyServer$Lifecycle.start(JettyServer.java:103)
at com.google.gerrit.lifecycle.LifecycleManager.start(LifecycleManager.java:95)
at com.google.gerrit.pgm.Daemon.start(Daemon.java:349)
at com.google.gerrit.pgm.Daemon.run(Daemon.java:255)
at com.google.gerrit.pgm.util.AbstractProgram.main(AbstractProgram.java:61)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.google.gerrit.launcher.GerritLauncher.invokeProgram(GerritLauncher.java:224)
at com.google.gerrit.launcher.GerritLauncher.mainImpl(GerritLauncher.java:120)
at com.google.gerrit.launcher.GerritLauncher.main(GerritLauncher.java:65)
at Main.main(Main.java:28)
Caused by: java.io.IOException: Failed to bind to /172.0.0.1:8081
at org.eclipse.jetty.server.ServerConnector.openAcceptChannel(ServerConnector.java:346)
at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:308)
at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.eclipse.jetty.server.Server.doStart(Server.java:396)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at com.google.gerrit.pgm.http.jetty.JettyServer$Lifecycle.start(JettyServer.java:89)
... 12 more
Caused by: java.net.BindException: Cannot assign requested address
at sun.nio.ch.Net.bind0(Native Method)
at sun.nio.ch.Net.bind(Net.java:433)
at sun.nio.ch.Net.bind(Net.java:425)
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
at org.eclipse.jetty.server.ServerConnector.openAcceptChannel(ServerConnector.java:342)
... 19 more

自己网上也搜了一下原因,大致说法有两个 gerrit_site 目录权限的问题,或者因为安装的 gerrit的是时候有一项 type = LUCENE; 然后系统缺少LUCENE导致的,但是笔者都试了一遍不行。经过我不断的尝试,发现后来改了一个地方就好了。

vim /usr/local/program/gerrit/review_site/etc/gerrit.config

然后把 listenUrl 中的172.0.0.1换成localhost就好了

[httpd]
    listenUrl = proxy-http://localhost:8081/

具体啥原因,希望大佬们来评论解析一下,毕竟笔者自己也是一只菜鸟。

你可能感兴趣的:(最适合菜鸟的Gerrit搭建详解)