以centos为例介绍下如何搭建gerrit服务器以及如何进行管理。
搭建 Apache 服务
Apache为gerrit服务提供http支持,在centos上也叫作httpd。
1. 首先开启防火墙对http及https的服务支持
[root@localhost ~]# sudo firewall-cmd --permanent --zone=public --add-service=http
success
[root@localhost ~]# sudo firewall-cmd --permanent --zone=public --add-service=https
success
[root@localhost ~]# sudo firewall-cmd --reload
success
2. 安装并启动 httpd 服务
[root@localhost ~]# yum install -y httpd
[root@localhost ~]# systemctl start httpd
3. 获取服务器 IP
[root@localhost ~]# hostname -I | awk '{print $1}'
192.168.230.131
4. 然后在浏览器中输入 192.168.230.131 ,如果能出现以下页面,说明 Apache 服务已工作正常
开启邮箱 SMTP 功能
需要开启该功能,gerrit 服务才可以通过自定义邮箱向各用户发送通知邮件。
登录网易邮箱,进入SMTP设置,将SMTP服务开启。
搭建 gerrit 服务
1. 安装 git 及 java
[root@localhost ~]# yum install -y java
[root@localhost ~]# yum install -y git
2. 下载 gerrit 安装包
[root@localhost ~]# mkdir -p git_repo
[root@localhost ~]# cd git_repo/
[root@localhost git_repo]# wget https://gerrit-releases.storage.googleapis.com/gerrit-3.2.3.war
--2021-12-13 01:15:52-- https://gerrit-releases.storage.googleapis.com/gerrit-3.2.3.war
Resolving gerrit-releases.storage.googleapis.com (gerrit-releases.storage.googleapis.com)... 172.217.163.48
Connecting to gerrit-releases.storage.googleapis.com (gerrit-releases.storage.googleapis.com)|172.217.163.48|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 72678480 (69M) [application/octet-stream]
Saving to: ‘gerrit-3.2.3.war’
gerrit-3.2.3.war 100%[===============================================================================================>] 69.31M 380KB/s in 4m 24s
2021-12-13 01:20:17 (269 KB/s) - ‘gerrit-3.2.3.war’ saved [72678480/72678480]
3. 安装 gerrit
[root@localhost git_repo]# java -jar gerrit-3.2.3.war init -d gerrit
Using secure store: com.google.gerrit.server.securestore.DefaultSecureStore
[2021-12-13 02:17:33,923] [main] INFO com.google.gerrit.server.config.GerritServerConfigProvider : No /root/git_repo/gerrit/etc/gerrit.config; assuming defaults
*** Gerrit Code Review 3.2.3
***
Create '/root/git_repo/gerrit' [Y/n]? y
*** Git Repositories
***
Location of Git repositories [git]: gitRepo
*** Index
***
Type [lucene]:
*** User Authentication
***
Authentication method [openid/?]: 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 [root]:
Java runtime [/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-2.el8_5.x86_64/jre]:
Copy gerrit-3.2.3.war to gerrit/bin/gerrit.war [Y/n]?
Copying gerrit-3.2.3.war to gerrit/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]?
Use SSL (https://) [y/N]?
Listen on address [*]:
Listen on port [8080]:
Canonical URL [http://localhost:8080/]:
*** Cache
***
*** Plugins
***
Installing plugins.
Install plugin codemirror-editor version v3.2.3 [y/N]? y
Installed codemirror-editor v3.2.3
Install plugin commit-message-length-validator version v3.2.3 [y/N]? y
Installed commit-message-length-validator v3.2.3
Install plugin delete-project version v3.2.3 [y/N]? y
Installed delete-project v3.2.3
Install plugin download-commands version v3.2.3 [y/N]? y
Installed download-commands v3.2.3
Install plugin gitiles version v3.2.3 [y/N]? y
Installed gitiles v3.2.3
Install plugin hooks version v3.2.3 [y/N]? y
Installed hooks v3.2.3
Install plugin plugin-manager version v3.2.3 [y/N]? y
Installed plugin-manager v3.2.3
Install plugin replication version v3.2.3 [y/N]? y
Installed replication v3.2.3
Install plugin reviewnotes version v3.2.3 [y/N]? y
Installed reviewnotes v3.2.3
Install plugin singleusergroup version v3.2.3 [y/N]? y
Installed singleusergroup v3.2.3
Install plugin webhooks version v3.2.3 [y/N]? y
Installed webhooks v3.2.3
Initializing plugins.
Initialized /root/git_repo/gerrit
Init complete, reindexing accounts with: reindex --site-path gerrit --threads 1 --index accountsReindexed 0 documents in accounts index in 0.0s (0.0/s)
Index accounts in version 11 is ready
Warning: InterruptedException
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.Thread.join(Thread.java:1260)
at org.h2.util.MathUtils.getSecureRandom(MathUtils.java:105)
at org.h2.util.MathUtils.secureRandomBytes(MathUtils.java:307)
at org.h2.store.FileLock.setUniqueId(FileLock.java:315)
at org.h2.store.FileLock.lockFile(FileLock.java:345)
at org.h2.store.FileLock.lock(FileLock.java:136)
at org.h2.engine.Database.open(Database.java:622)
at org.h2.engine.Database.openDatabase(Database.java:260)
at org.h2.engine.Database.<init>(Database.java:254)
at org.h2.engine.Engine.openSession(Engine.java:57)
at org.h2.engine.Engine.openSession(Engine.java:164)
at org.h2.engine.Engine.createSessionAndValidate(Engine.java:142)
at org.h2.engine.Engine.createSession(Engine.java:125)
at org.h2.engine.Engine.createSession(Engine.java:27)
at org.h2.engine.SessionRemote.connectEmbeddedOrServer(SessionRemote.java:331)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:107)
at org.h2.jdbc.JdbcConnection.<init>(JdbcConnection.java:91)
at org.h2.Driver.connect(Driver.java:74)
at com.google.gerrit.server.cache.h2.H2CacheImpl$SqlHandle.<init>(H2CacheImpl.java:637)
at com.google.gerrit.server.cache.h2.H2CacheImpl$SqlStore.acquire(H2CacheImpl.java:605)
at com.google.gerrit.server.cache.h2.H2CacheImpl$SqlStore.buildBloomFilter(H2CacheImpl.java:331)
at com.google.gerrit.server.cache.h2.H2CacheImpl$SqlStore.open(H2CacheImpl.java:303)
at com.google.gerrit.server.cache.h2.H2CacheImpl.start(H2CacheImpl.java:189)
at com.google.gerrit.server.logging.LoggingContextAwareRunnable.run(LoggingContextAwareRunnable.java:110)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Collecting projects: 2 changes with: reindex --site-path gerrit --threads 1 --index changes
Reindexing changes: project-slices: 100% (2/2), done
Reindexed 0 documents in changes index in 0.0s (0.0/s)
Index changes in version 60 is ready
Reindexing groups: 100% (2/2)ith: reindex --site-path gerrit --threads 1 --index groups
Reindexed 2 documents in groups index in 0.1s (38.5/s)
Index groups in version 8 is ready
Reindexing projects: 100% (2/2) with: reindex --site-path gerrit --threads 1 --index projects
Reindexed 2 documents in projects index in 0.1s (30.8/s)
Index projects in version 4 is ready
Executing /root/git_repo/gerrit/bin/gerrit.sh start
Starting Gerrit Code Review: OK
Waiting for server on localhost:8080 ... OK
Opening http://localhost:8080/#/admin/projects/ ...OK
4. 修改 gerrit 配置文件
[root@localhost git_repo]# vi ./gerrit/etc/gerrit.config
[gerrit]
basePath = gitRepo
canonicalWebUrl = http://192.168.230.131:8080/
serverId = 2815b11f-d744-4850-b1aa-8148e2ccf88c
[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 = root
javaHome = /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-2.el8_5.x86_64/jre
[index]
type = lucene
[auth]
type = HTTP
[receive]
enableSignedPush = false
[sendemail]
smtpServer = smtp.163.com
smtpServerPort = 25
smtpUser = ******@163.com # 邮箱地址
from = CodeReview<******@163.com> # 邮箱地址
smtpPass = ****** # 邮箱密码
[sshd]
listenAddress = 192.168.230.131:29418
[httpd]
listenUrl = http://192.168.230.131:8080/
[cache]
directory = cache
5. 添加 gerrit 的 httpd 启动文件
[root@localhost git_repo]# cd /etc/httpd/conf.d/
[root@localhost conf.d]# touch gerrit.conf
[root@localhost conf.d]# vi gerrit.conf
<VirtualHost *:80>
ServerName 192.168.230.131
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
AuthUserFile /etc/httpd/passwords
</Location>
AllowEncodedSlashes On
ProxyPass / http://192.168.230.131:8080/
</VirtualHost>
6. 添加管理员账户以及普通成员账户
[root@localhost ~]# htpasswd -cb /etc/httpd/passwords admin admin
[root@localhost ~]# htpasswd -b /etc/httpd/passwords user1 123456
7. 设置 SELinux 为宽容模式
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
8. 重启 httpd 服务
[root@localhost ~]# systemctl restart httpd
9. 重启 gerrit 服务
[root@localhost ~]# ./git_repo/gerrit/bin/gerrit.sh stop
Stopping Gerrit Code Review: OK
[root@localhost ~]# ./git_repo/gerrit/bin/gerrit.sh start
Starting Gerrit Code Review: OK
10. 浏览器再次进入 192.168.230.131, 输入管理员账户密码 admin/admin,可以看到 gerrit 界面,说明 gerrit 服务启动成功。
配置 gerrit 服务
1. 完成邮箱注册,点击页面右上角 Settings -> Email Address,填写邮箱(不能和之前的SMTP邮箱重复),点击SEND VERIFICATION,收到邮件后点击里面的链接,完成用户注册。
2. 添加ssh key,点击 Settings -> SSH Keys,添加 ssh key,点击 ADD NEW SSH KEY
3. 进行项目设置,点击BROWSE -> Repositories -> All-Projects -> Access -> EDIT,设置 Code-Review,Verified, Submit权限,并将按钮显示展示。
4. 创建测试代码分支,点击 BROWSE -> Repositories -> CREATE NEW 创建 test 代码仓库,并通过Clone with commit-msg hook 下载代码
5. 下载代码,进入代码路径,配置用户密码
[root@localhost test]# git config user.name your_name
[root@localhost test]# git config user.email your_email_address
[root@localhost test]# git config remote.origin.push refs/heads/*:refs/for/*
6. 新建一个文件,并上传到 gerrit
[root@localhost test]# touch readme.txt
[root@localhost test]# vi readme.txt
[root@localhost test]# git add readme.txt
[root@localhost test]# git commit -m "init"
[master 2a11193] init
1 file changed, 1 insertion(+)
create mode 100644 readme.txt
[root@vcpe7 test]# git commit --amend
[master 594fd17] init
Date: Mon Dec 13 16:47:35 2021 +0800
1 file changed, 1 insertion(+)
create mode 100644 readme.txt
[root@vcpe7 test]# git push
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Writing objects: 100% (3/3), 277 bytes | 277.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
remote: Processing changes: refs: 1, new: 1, done
remote:
remote: SUCCESS
remote:
remote: http://192.168.230.131:8081/c/test/+/99 init [NEW]
remote:
To ssh://192.168.230.131:29418/test
* [new reference] master -> refs/for/master
7. 提交代码,点击 CHANGES -> Open,打开刚刚 commit 的提交,在 REPLY 中点击 Code Review +2, Verified +1, 之后会出现 Submit 按钮,点击提交即可完成你的首个代码提交。