Code Review 代码评审是指在软件开发过程中,对源代码的系统性检查,改进代码质量,查找系统缺陷,保证软件总体质量和提高开发者自身水平。
简单的说,Code Review是用来确认方案设计和代码实现的质量保证机制。
Gerrit 是建立在Git版本控制系统之上并且基于Web的一个免费开源的轻量级代码审查工具。
作为开发者和Git之间的一层屏障,不允许直接将本地修改内容同步到远程仓库中。
与Jenkins集成后,可以在每次提交代码后,人工审核代码前,通过Jenkins任务自动运行单元测试、构建以及自动化测试,如果Jenkins任务失败,会自动打回本次提交。
一般Git、Gerrit和Jenkins集成后的使用流程
1.开发者提交代码到Gerrit
2.触发对应的Jenkins任务,通过以后Verified加1
3.人工审核,审核通过后code review加2,触发对应的Jenkins任务
4.通过以后确认本次提交,Gerrit执行与Git仓库的代码同步操作
5.代码进入Git仓库
[root@localhost ~]# yum install java-11-openjdk.x86_64 git -y # jdk版本>1.8.0
[root@localhost ~]# java -version
openjdk version "11.0.14.1" 2022-02-08 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.14.1+1-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.14.1+1-LTS, mixed mode, sharing)
[root@localhost ~]# git --version
git version 1.8.3.1
htpasswd -cb /etc/httpd/passwords admin macos #注意,"-c"参数为创建,仅限第一次添加用户时使用,用户名和密码均为admin
htpasswd -b /etc/httpd/passwords lisi ubuntu #第二次创建时不要加"-c"参数。创建一个jason用户,密码为"zhaopengwei"
[root@localhost ~]# cat /etc/httpd/passwords #这个路径在初始化gerrit内要用到 #我们查看该文件的确有2个用户,但是密码时经过加密处理的!
admin:$apr1$WtgrCXeP$yT6VNPPNfy4NVlSvhxmot/
lisi:$apr1$jPTYg7H3$yxEEEgI6MIQSscFgm/5g3/
gerrit-3.5.1.war
除了"Authentication method [openid/?]: http“ 這裡輸入 http ,其餘全部輸入 "Enter",如下示例:
[root@localhost ~]# java -jar gerrit-3.5.1.war init -d gerrit_site
Using secure store: com.google.gerrit.server.securestore.DefaultSecureStore
[2022-04-06 17:27:12,846] [main] INFO com.google.gerrit.server.config.GerritServerConfigProvider : No /root/gerrit_site/etc/gerrit.config; assuming defaults
*** Gerrit Code Review 3.5.1
***
Create '/root/gerrit_site' [Y/n]?
*** Git Repositories
***
Location of Git repositories [git]:
*** JGit Configuration
***
Auto-configured "receive.autogc = false" to disable auto-gc after git-receive-pack.
*** 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]?
Use case insensitive usernames [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-11-openjdk-11.0.14.1.1-6.el8.x86_64]:
Copy gerrit-3.5.1.war to gerrit_site/bin/gerrit.war [Y/n]?
Copying gerrit-3.5.1.war to gerrit_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]?
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.5.1 [y/N]?
Install plugin commit-message-length-validator version v3.5.1 [y/N]?
Install plugin delete-project version v3.5.1 [y/N]?
Install plugin download-commands version v3.5.1 [y/N]?
Install plugin gitiles version v3.5.1 [y/N]?
Install plugin hooks version v3.5.1 [y/N]?
Install plugin plugin-manager version v3.5.1 [y/N]?
Install plugin replication version v3.5.1 [y/N]?
Install plugin reviewnotes version v3.5.1 [y/N]?
Install plugin singleusergroup version v3.5.1 [y/N]?
Install plugin webhooks version v3.5.1 [y/N]?
Initializing plugins.
No plugins found with init steps.
============================================================================
Welcome to the Gerrit community
Find more information on the homepage: https://www.gerritcodereview.com
Discuss Gerrit on the mailing list: https://groups.google.com/g/repo-discuss
============================================================================
Initialized /root/gerrit_site
Init complete, reindexing accounts,changes,groups,projects with: reindex --site-path gerrit_site --threads 1 --index accounts --index changes --index groups --index projects --disable-cache-statsReindexed 0 documents in accounts index in 0.0s (0.0/s)
Index accounts in version 11 is ready
Reindexing groups: 100% (2/2)
Reindexed 2 documents in groups index in 0.3s (8.0/s)
Index groups in version 8 is ready
Reindexing changes: Slicing projects: 100% (2/2), done
Reindexed 0 documents in changes index in 0.0s (0.0/s)
Index changes in version 71 is ready
Reindexing projects: 100% (2/2)
Reindexed 2 documents in projects index in 0.0s (46.5/s)
Index projects in version 4 is ready
Executing /root/gerrit_site/bin/gerrit.sh start
Starting Gerrit Code Review: OK
Waiting for server on localhost:8080 ... OK
Opening http://localhost:8080/#/admin/projects/ ...FAILED
Open Gerrit with a JavaScript capable browser:
http://localhost:8080/#/admin/projects/
//使用 curl 命令來檢查是否成功
[root@localhost ~]# curl http://localhost:8080
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Gerrit Code Review</title>
<script type="text/javascript">
var href = window.location.href;
var query = "";
var q = href.indexOf('?');
if (q >= 0) {
query = href.substring(q);
href = href.substring(0,q);
}
var p = href.indexOf('#');
var token;
if (p >= 0) {
token = href.substring(p);
href = href.substring(0, p);
} else {
token = '';
}
window.location.replace(href + 'login/' + token + query);
</script>
</head>
<body>
<p>Redirecting to <a href="login/">Gerrit Code Review</a>.</p>
</body>
</html>
[root@localhost ~]# pwd
/root
[root@localhost ~]# cd gerrit_site/
[root@localhost gerrit_site]# ls
bin cache data db etc git index lib logs plugins static tmp
./review_site/bin/gerrit.sh status #查看gerrit服务
./review_site/bin/gerrit.sh stop #停止gerrit服务
./review_site/bin/gerrit.sh status #再次查看gerrit服务
./review_site/bin/gerrit.sh start #启动gerrit
Starting Gerrit Code Review: OK #出现ok 表示启动成功
上下端口要统一切记不然访问不到gerrit
[root@localhost ~]# vi /root/gerrit_site/etc/gerrit.config
[gerrit]
basePath = git
canonicalWebUrl = http://localhost:8888/ #修改端口
serverId = d7c8fe1d-5f17-4a59-8daf-e7ccb1e6a72f
[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-11-openjdk-11.0.14.1.1-6.el8.x86_64
[index]
type = lucene
[auth]
type = HTTP
userNameCaseInsensitive = true
[receive]
enableSignedPush = false
[sendemail]
smtpServer = localhost
[sshd]
listenAddress = *:29418
[httpd]
listenUrl = http://*:8888/ #修改端口
[cache]
directory = cache
[root@localhost ]# ./review_site/bin/gerrit.sh restart
Stopping Gerrit Code Review: OK
Starting Gerrit Code Review: OK
yum -y install nginx
yum -y install gcc pcre pcre-devel zlib zlib-dev ssl-dev
vim /etc/nginx/nginx.conf
server {
listen 8090; #修改监听端口
server_name localhost;
location / {
#root html;
#index index.html index.htm;
auth_basic "Gerrit Code Review"; #添加
auth_basic_user_file /etc/httpd/passwords; #这里我们让认证方式是基于文件方式认证,而该文件的内容我们刚刚也看过了,你懂的。
proxy_pass http://192.168.129.159:8888; #此处的8888为咱们刚刚在安装Gerrit服务的端口号
proxy_set_header X-Forwarded-For $remote_addr; #添加
proxy_set_header Host $host; #添加
}
}
nginx #启动nginx
Killall nginx #结束进程
[root@localhost ~]# ss -anlt
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 5 192.168.122.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:631 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 *:8090 *:*
LISTEN 0 50 [::]:29418 [::]:*
LISTEN 0 128 [::]:111 [::]:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 128 [::1]:631 [::]:*
LISTEN 0 50 [::]:8888 [::]:*
LISTEN 0 100 [::1]:25 [::]:*